Computing.Net > Forums > Unix > sed comand

sed comand

Reply to Message Icon

Original Message
Name: fan
Date: March 25, 2005 at 23:58:21 Pacific
Subject: sed comand
OS: windows XP
CPU/Ram: 256MB
Comment:

Hi:
how to write a sed command to swap the first and second word in each line in a file?
Thanks


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: March 27, 2005 at 00:22:14 Pacific
Subject: sed comand
Reply: (edit)

Hi:

sed is not a good command for dealing with words or fields. awk is better:

#!/bin/ksh

awk ' {
saveone=$1
$1=$2
$2=saveone
print
} ' myfile

It might be possible with sed, but it'll take a better programmer than I am to do it.

Regards,

Nails


Report Offensive Follow Up For Removal

Response Number 2
Name: David Perry
Date: March 28, 2005 at 04:43:38 Pacific
Subject: sed comand
Reply: (edit)

echo "I have a red pencil box" | sed -e "s/\(^[^ ]*\) \([^ ]*\)/\2 \1/"


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: March 28, 2005 at 08:33:55 Pacific
Subject: sed comand
Reply: (edit)

David:

Good solution! Unfortunately, it doesn't work on my solaris 7 box. Even the POSIX compliant version of sed doesn't work.

However, it DOES work on my Red Hat Linux 7.1 box. Must be a GNU sed upgrade.

Regards,

Nails


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed comand

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge