SED/AWK maybe PERL in a WHILE loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED/AWK maybe PERL in a WHILE loop
# 1  
Old 05-08-2012
SED/AWK maybe PERL in a WHILE loop

Hi,

I have a file containing numerous string values, for example:

Code:
AMFU8849636
CCLU7120334
GESU5784065
TEMU3070096

I have a second file with multiple records, such as the one below that I would like to manipulate based on the strings from the first file:

Code:
CATOS2EDI DSTOW     ABC            EGLV           20120426141639  4180000000161D
CONTAINER EGLVAMFU8849636F31305   LBG4506I540916 EMCFHY8260             
TRACE     END
CATOS2EDI DSTOW     ABC            EGLV           20120426141639  4180000000161D
CONTAINER EGLVAMFU8849636F31305   LBG4506I540916 EMCFHY8260             
TRACE     END
CATOS2EDI DSTOW     ABC            EGLV           20120426141639  4180000000161D
CONTAINER EGLVAMFU8849636F31305   LBG4506I540916 EMCFHY8260             
TRACE     END

I would like to take each value from the first file and search for it in the second file. If found, I would like to change the 'EGLV' to 'CHNJ'.

For example:

Code:
while read line
do
    sed s/EGLV${line}/CHNJ${line}/g second_file
done < first_file

Just not getting the output that I would like:

Desired output example:

Code:
CATOS2EDI DSTOW     ABC            EGLV           20120426141639  4180000000161D
CONTAINER CHNJAMFU8849636F31305   LBG4506I540916 EMCFHY8260             
TRACE     END
CATOS2EDI DSTOW     ABC            EGLV           20120426141639  4180000000161D
CONTAINER EGLVAMFU1234567F31305   LBG4506I540916 EMCFHY8260             
TRACE     END
CATOS2EDI DSTOW     ABC            EGLV           20120426141639  4180000000161D
CONTAINER EGLVAMFU8910112F31305   LBG4506I540916 EMCFHY8260             
TRACE     END

As you can see, only the first CONTAINER segment line matched up and was changed.

Any advice?

Chris
# 2  
Old 05-08-2012
Each time you run sed, you're processing the entire file, it doesn't wait for all your expressions, i.e. sed 's/a/b/g;s/c/d/g' and so forth. You could assemble the file into one long string to do that, if it's short enough.
# 3  
Old 05-08-2012
Unfortunately the file is quite lengthy. I tried to use perl, but it did not perform the match correctly:

Code:
perl -pi -e 's/^CONTAINER EGLV${line}/^CONTAINER CHNJ${line}/g' $1

This simply changed all the values.
# 4  
Old 05-08-2012
Why should perl, awk, sed, anything understand that you want the 99,9999 regexes you didn't give it instead of the single regex you did? You'll have to load them all into awk or perl at once somehow.

Working on something.
# 5  
Old 05-09-2012
try this

Code:
 awk 'NR==FNR{a[$0];next;} {for(i in a) {b="EGLV"i;gsub(b,"CHNJ"i,$0)}}1  ' file1 file2

# 6  
Old 05-09-2012
Assuming (based on the sample given) only one pattern will match any record in the second file, this should be a bit more efficient as it breaks as soon as a match is found. It also doesn't spend time looping through the entire list of patterns when the record does not contain the target string to change.

Code:
awk '
    FNR == NR { pats[$1]; next; }   # collect patterns from first file

    /EGLV/ {                        # loop through patterns only if record contains target for change
        for( i in pats )
            if( sub( "EGLV" i, "CHNJ" i, $0 ) )
                break;              # assuming one match per record short circuit
    } 
   { print; }                      # my preference to 1 -- less confusing
'  file1 file2 >output-file

# 7  
Old 05-09-2012
Agama, your solution worked, thanks!

tarun_agrawal, I did not have a chance to try your solution, but it looks similar to the one I tried. Thanks for your input as well!

Thanks again everyone!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace my perl with awk or sed

My code below will print only the email address from all lines. I want to convert it with sed or awk.. also what if i just want to find only filenames. cat LIS_EMAIL | perl -wne'while(/+@+\w+/g){print "$&\n"}' Hoping to extract the filename such us .exe, .bin. From file that has scrambled... (8 Replies)
Discussion started by: invinzin21
8 Replies

2. Shell Programming and Scripting

Modify the file with awk,sed or perl

Hi All, I need help from any of you.Would be so thankful for your help. I/P DDDD,1045,161,1557,429,1694,800,1911,1113,2460,1457,2917> 1609,3113,1869,3317,2732,3701,3727,4132,5857,5107> 9004,6496 DDDD,1125,157,1558,429,1694,800,1911,1117,2432,1444,2906>... (2 Replies)
Discussion started by: Indra2011
2 Replies

3. Shell Programming and Scripting

awk sed perl??

Hi guys First of all a salute to this wonderful platform which has helped me many a times. Im now faced with something that I cannot solve. I have data like this 11:14:18 0.46975 11:14:18 0.07558 11:14:18 0.00020 11:14:18 0.00120 11:14:18 0.25879 11:14:19 0.00974 11:14:19 0.05656... (13 Replies)
Discussion started by: jamie_123
13 Replies

4. Shell Programming and Scripting

Deleting characters with sed,perl,awk

Input: :: gstreamer :: xine-lib :: xine-lib-extras Output should be: gstreamer xine-lib xine-lib-extras How can it be done with sed or perl? (12 Replies)
Discussion started by: cola
12 Replies

5. Shell Programming and Scripting

Deleting the first column with sed,awk or perl

336 brtr 256 hello Output: brtr hello How can i do this with sed,awk or perl? (5 Replies)
Discussion started by: cola
5 Replies

6. Shell Programming and Scripting

Getting the first word using sed,awk or perl

Input: root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: lp:x:7: mail:x:8: Output: root daemon bin sys adm tty (8 Replies)
Discussion started by: cola
8 Replies

7. Shell Programming and Scripting

How to remove spaces using awk,sed,perl?

Input: 3456 565 656 878 235 8 4 8787 3 7 35 878 Expected output: 3456 565 656 878 235 8 4 8787 3 7 35 878 How can i do this with awk,sed and perl? (10 Replies)
Discussion started by: cola
10 Replies

8. Shell Programming and Scripting

Question related to sed or awk or perl

Hi All, I have a big file of 100,000 lines with the following format: 1,736870,736870,1,2,5,547,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253889445 1,881246,881246,1,2,6,402,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253889445... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

Use loop in sed or awk?

I know a little bit of sed and awk, but not too much. can loop be done in sed. Baiscally what I want is: file1: aaa 111 bbb 222 ccc 333 ddd 444 eee 555 fff 666 Note: the space in between is "tab" file2: bbb ddd eee I want delete any entries in file1... (4 Replies)
Discussion started by: redstone
4 Replies

10. Shell Programming and Scripting

sed with awk in loop

Hi Guys, I am using following code to do some task. Can somebody please help me to use a for loop for this ? ___________________________________________________ sed s/DATA2/`awk '/ / {print $1}' formated1`/ EOD_Batch > EOD_Batch1 sed s/DATA3/`awk '/ / {print $2}' formated1`/ EOD_Batch1 >... (4 Replies)
Discussion started by: manchau
4 Replies
Login or Register to Ask a Question