LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-02-2008, 12:17 PM   #1
itzfritz
Member
 
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:
/mnt/testserver/test1
/mnt/testserver/test22
/mnt/prodserver/production

However, thee results are:
test1testserver
test22estserver
productionerver

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.

Thanks!

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, 12:50 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
Code:
$ sed 's/.*\,[[:space:]]*\(.*\)\,[[:space:]]*\(.*\)/\2\/\1/g' text
/mnt/testserver/test1
/mnt/testserver/test22
/mnt/prodserver/production
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, 02:50 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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):
Code:
sed 's#.*, \(.*\), \(.*\)#\2/\1#' filename
[edit]
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 02:55 PM.
 
  


Reply


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 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Sed Help noodle123 Programming 8 06-05-2002 10:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:29 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration