Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 09-02-2008, 01:17 PM   #1
Registered: Oct 2004
Location: Babylon, New York
Distribution: debian lenny, ubuntu intrepid
Posts: 70

Rep: Reputation: 15
sed help - PLEASE!

I have a sed script that is not behaving as expected; in a regex substitution, it extracts 2 matches from each input line and then outputs those matches in reverse order, concatenated. However, it is outputting them in their original positions, instead of concatenating them.

The input file contains lines, all of which are in the format "id-number, short-name, path":
12345, test1, /mnt/testserver
12346, test22, /mnt/testserver
12347, production, /mnt/prodserver

My sed script is supposed to generate a full path, e.g. "path + / + short-name".

The script:

cat filename | sed 's/.*/, \(.*\)\, \(.*\)/\2\/\1/g'

The script saves 2 matches, and attempts to concatenate them in reverse order for the output.
You would expect the results to be:

However, thee results are:

As you can see, it is preserving the original positions of each match (relatively), rather than concatenating them.
Every example that I have seen, where two matches are reversed (e.g. google "sed \2\1"), it concatenates.


Debian GNU/Linux 4.0 (etch) via vnc4server
GNU bash, version 3.1.17(1)-release (x86_64-pc-linux-gnu)
GNU sed version 4.1.5
Old 09-02-2008, 01:50 PM   #2
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
$ sed 's/.*\,[[:space:]]*\(.*\)\,[[:space:]]*\(.*\)/\2\/\1/g' text
1) The "cat" is a waste of a process
2) You neglected to escape the first comma in your sample text (which, I suspect, was a typo).

Otherwise it worked fine for me.
Old 09-02-2008, 03:50 PM   #3
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
When dealing with slashes, I think it is convenient to use another character than a / for sed pattern,
eg (sed with a # as pattern delimiter):
sed 's#.*, \(.*\), \(.*\)#\2/\1#' filename
Also, don't need the g option in s//g pattern as sed works on only one pattern match for each line here

Last edited by keefaz; 09-02-2008 at 03:55 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
Sed Help noodle123 Programming 8 06-05-2002 11:07 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:45 AM.

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