Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple substitution 1

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
Hi guys
We receive s.w.i.f.t files daily, contents are like :-

{1:F01FRED3322AXXX7279625440}{2:O8011651031208RNNSZAJJAXXX29021728160312081451N}{4:
:20:5998RBRB00064AUD
:28:00064/01
:51C:/405605
:23:F188-148011
:30:031205
:26A:AF4419070/9075
:26A:ZD8193201/3222
:33B:AUD2500,
:34B:AUD2500,
:16A:1
:51C:/407513
:23:F172-33074
:30:031205
:26A:CG2365554/5564
:33B:AUD1100,
:34B:AUD1100,
:16A:1


On the :51C: line the reference code provided has to be changed for some senders, I'm thinking of using a lookup table e.g. mylookuptab, contents like :

:51C:/405605 :51C:/ABC1
:51C:/407513 :51C:/ABC3
:51C:/405612 :51C:/ABC2
:51C:/405615 :51C:/ABD1
:51 .. etc etc

This is a small sample, there could be a hundred to change.
I could do a for loop, and sed for each code in the lookup table......
But can anyone suggest a neater solution - awk? sed? tr?
Thanks in advance

Dickie Bird (:)-)))
 
Hi if you mofify a little bit your lookup table, sed if the command you need :

Create your lookup table (look.sed) like this :

#--- Lookup table ---
s :51C:/405605 :51C:/ABC1 ;t
s :51C:/407513 :51C:/ABC3 ;t
s :51C:/405612 :51C:/ABC2 ;t
s :51C:/405615 :51C:/ABD1 ;t
#--- End of table ----


Entries are in the form :
s<separator><text to replace><separator><replacement text><separator>;t

In my example, <separator> = space.

And use sed to modify your file

sed -f look.sed input_file > output_file





Jean Pierre.
 
Excellent solution, J-P, have a star !

Dickie Bird (:)-)))
 
And the awk solution:
Code:
awk '
BEGIN{while((getline<&quot;mylookuptab&quot;)>0)a[$1]=$2}
{for(i in a)if(sub(i,a[i]))break;print}
' /path/to/old >newfile

Hope This Help
PH.
 
Thanks PH. Surprisingly, though ( to me ) the awk solution takes about twice as long ( about 3 seconds - no big deal )

Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top