LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2012, 05:10 PM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
sed help


Code:
$ echo -en '* [[hello world1]]\n** [[hello world2]]\n'
* [[hello world1]]
** [[hello world2]]

I would like the output to be :

hello world1
hello world2

I've been working on the following regex, but for some reason, it does'nt match. I understand that * is treated as a special character in sed, which has to be escaped.

It does'nt match the 2 x **, but I don't understand why.

Code:
$ echo -en '* [[hello world1]]\n** [[hello world2]]\n' | sed 's/[\* [[|\*\* [[] (.*)\]\]/\1/g'

Last edited by dazdaz; 07-07-2012 at 05:32 PM.
 
Old 07-07-2012, 09:07 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
According to REGEX, * is only a special character when followed by a character set.
Code:
^*    # Match any line starting with *, note it is not escaped.
[a-zA-z]*   # Here the * has the special meaning 0 or more duplicates of the char set a-z and A-Z
Code:
sed 's/[\* [[|\*\* [[] (.*)\]\]/\1/g'
Yo have specifoed a back-reference without escaping the ( or ). To do that run sed with the -r option or escape the ( and ).
Code:
sed -r 's,^[a-z]*(.*),\1,'    # or
sed 's,^[a-z]*\(.*\),\1,'
For your example, I could only get it to work with two substitute command:
Code:
echo -en '* [[hello world1]]\n** [[hello world2]]\n' | sed -e 's,^[* []*,,' -e 's,\]\+,,'
Or using the -r option to SED:
Code:
echo -en '* [[hello world1]]\n** [[hello world2]]\n' | sed -re 's,^[* []*,,' -e 's,\]+,,'
Use one substitute command:
Code:
echo -en '* [[hello world1]]\n** [[hello world2]]\n' | sed -r 's,^[* []*(.*)\]\],\1,'
Hope it helps.

Last edited by towheedm; 07-07-2012 at 09:13 PM. Reason: One substitute command
 
Old 07-08-2012, 04:35 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
echo -en '* [[hello world1]]\n** [[hello world2]]\n' | sed -r 's/.*\[\[|]]$//g'
echo -en '* [[hello world1]]\n** [[hello world2]]\n' | awk -F"[][]*" '{print $(NF-1)}'
 
1 members found this post helpful.
Old 07-08-2012, 06:08 AM   #4
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Does it have to be sed
Code:
grep -o "hello world[12]"
 
Old 07-08-2012, 02:17 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by whizje View Post
Does it have to be sed
Code:
grep -o "hello world[12]"
I assumed that hello world represented any character string, though that was not explicitly stated. What say you, OP?

Daniel B. Martin
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
sed and regexp matching (GNU sed version 4.2.1) Ashkhan Programming 8 02-27-2012 09:12 AM
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration