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!!
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.
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)