Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newbie - howto replace text from two files with sed?

5 views
Skip to first unread message

mickd

unread,
Jun 2, 2006, 5:56:04 PM6/2/06
to
I am hoping a sed expert can help me here.

I'm trying to replace a number from one file with text to a matching
numbered list from another file. For example:

description.txt
July 1 [25] Vegas Show
June 5 [1] Miami Run
Sep 20 [120] Houston

list.txt
1. JohnMalvick
2. GabeCron
3. JaneDoe
...
25. JoeGuber
...
120. ElinZoe

output.txt
July 1 [JoeGuber] Vegas Show
June 5 [JohnMalvick] Miami Run
Sep 20 [ElinZoe] Houston

I've done basic scripts with grep and sed but I am having a little bit
of trouble putting something like this together. Any help will be
greatly appreciated!!

Pascal Bourguignon

unread,
Jun 2, 2006, 8:08:09 PM6/2/06
to
"mickd" <micha...@adelphia.net> writes:

This is a job for awk:

[pjb@thalassa tmp]$ ./s.awk


July 1 [JoeGuber] Vegas Show
June 5 [JohnMalvick] Miami Run
Sep 20 [ElinZoe] Houston

[pjb@thalassa tmp]$ cat s.awk
#!/bin/bash
awk '
BEGIN {
FS=". ";
while(getline <"list.txt"){
type[$1]=$2;
}
FS="[][]";
}
{
printf "%s[%s]%s\n",$1,type[$2],$3;
}
'<description.txt
[pjb@thalassa tmp]$ cat description.txt

July 1 [25] Vegas Show
June 5 [1] Miami Run
Sep 20 [120] Houston

[pjb@thalassa tmp]$ cat list.txt

1. JohnMalvick
2. GabeCron
3. JaneDoe

25. JoeGuber
120. ElinZoe
[pjb@thalassa tmp]$

--
__Pascal Bourguignon__ http://www.informatimago.com/

HEALTH WARNING: Care should be taken when lifting this product,
since its mass, and thus its weight, is dependent on its velocity
relative to the user.

John Savage

unread,
Jun 3, 2006, 11:01:07 PM6/3/06
to

Use sed (or some other editor) to massage your file "list.txt" into
file "list.scr" comprising a bunch of commands for a later run of sed,
viz.,
s/\[1]/[JohnMalvick]/
s/\[2]/[GabeCron]/
s/\[3]/[JaneDoe]/
s/\[25]/[JoeGuber]/
s/\[120]/[ElinZoe]/

Then you will have what you want when you run:
sed -f list.scr description.txt

If your database was huge it would be worth a bit of extra effort to make
this more efficient, but for what you illustrated this code is just fine.
--
John Savage (my news address is not valid for email)

0 new messages