Powered by Invision Power Board


  Reply to this topicStart new topicStart Poll

> Sed Command, How to copy a string usin sed
eveaug
  Posted: May 6 2005, 04:52 PM
Quote Post


Newbie
*

Group: Members
Posts: 1
Member No.: 977
Joined: 6-May 05



Hi

Using sed is it possible to copy a string from a line in a file to another location on the same line:

Example file:

aaa bbb ccc
eee fff ggg

to become

aaa bbb ccc > ccc
eee fff ggg > ggg
?

Any help would be great

Thanks

PMEmail Poster
Top
andyb1ack
Posted: May 10 2005, 04:01 PM
Quote Post


User Level: 4
****

Group: Members
Posts: 98
Member No.: 513
Joined: 9-September 04



Uh-oh. This isn't going to be easy to explain, but here's the code for your example:

CODE

$ cat /var/tmp/asb
aaa bbb ccc
eee fff ggg

$ sed 's/\(.*\) \(.*\)/\1 \2 > \2/' /var/tmp/asb
aaa bbb ccc > ccc
eee fff ggg > ggg


I've split up each line into two parts separated by a space.
. = any character
* = 0 or more of the preceeding character
hence .* = 0 or more of any character

\(.*\) means save anything into named space 1

Repeat it with a space in between
ie the \(.*\) \(.*\)
means, put everything up to the last space into named space 1, and from the last space to the end of the line in named space 2.

I could have done it the following way too:
CODE

$ sed 's/\(.*\) \(.*\) \(.*\)/\1 \2 \3 > \3/' /var/tmp/asb  
aaa bbb ccc > ccc
eee fff ggg > ggg



Alternately, you could have used the following shell commands:
CODE

cat /var/tmp/asb |while read A B C
do
 echo $A $B $C \> $C
done
PMEmail Poster
Top
vibhor_agarwalin
Posted: May 11 2005, 02:47 PM
Quote Post


User Level: 3
***

Group: Members
Posts: 34
Member No.: 788
Joined: 1-February 05



Hi Andy,

I remember you and your sed explanation.

I am able to solve most of my problems with the sed, but i am a bit new to your while and read one.

Can you help me a bit in that.
PMEmail Poster
Top
andyb1ack
Posted: May 11 2005, 03:21 PM
Quote Post


User Level: 4
****

Group: Members
Posts: 98
Member No.: 513
Joined: 9-September 04



The while read contruct is really useful.
You can put configuration info into a file and have it read by a script.
To change the script you just then update the config file, which is better practice than updating code.

The important thing to ensure is that each line in the config file has the number of fields as you expect, or things start going a bit funny!

Here's an example of a simple while read in action:
CODE

$ cat /var/tmp/asb.dirs      
/var/tmp/logs/big_accounts_job      4
/var/tmp/logs/small_accounts_job   10
/var/tmp/logs/backups              30

$ cat /var/tmp/asb.hk_dirs.sh
#/bin/ksh
cat /var/tmp/asb.dirs |while read DIR DAYS
do
 echo find ${DIR} -mtime +${DAYS} -exec rm {} \;
done

$ ksh /var/tmp/asb.hk_dirs.sh
find /var/tmp/logs/big_accounts_job -mtime +4 -exec rm {};
find /var/tmp/logs/small_accounts_job -mtime +10 -exec rm {};
find /var/tmp/logs/backups -mtime +30 -exec rm {};
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | Shells & Scripting | Next Newest »

Topic Options Reply to this topicStart new topicStart Poll

 



[ Script Execution time: 0.1389 ]   [ 12 queries used ]   [ GZIP Enabled ]




Partners: Cambridge Plus :: <Link Available> :: 3D Mechanical Design :: <Link Available>
Unix Man Pages / Linux Man Pages :: HiFi Forum :: SIP VoIP Phone & Provider Reviews :: UNIX/Linux Forum Archives

More info on advertising on Unix/Linux Forum