|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
compare 2 files and replace
Hi,
I have a file which contains names with counts for eg: 0622 0031 JOHN MAX 20080622003104. STAT 1. 0622 0031 BILL MAX 20080622003104. STAT 7. and I have an exception file containing. BILL Can anyone help to write a script using this exception files to replace the STAT *(could be any number) to STAT 0 for the names that are in this list the output should be and the format remains the same 0622 0031 JOHN MAX 20080622003104. STAT 0. any comments or help appriciated. I got a script which can do this, but it changes the format. #!/usr/bin/ksh nawk ' FNR==NR {ex[$1]; next} { $9 = ($1 in ex) ? "0." : $2; print } ' a2 a1 > a3 any help appriciated. thanks Last edited by antointoronto; 06-25-2008 at 01:32 AM.. |
| Sponsored Links | ||
|
|
|
|||
|
Quote:
0622 0031 JOHN MAX 20080622003104. STAT 1. 0622 0031 BILL MAX 20080622003104. STAT 0. for BILL which is found in the exception file the STAT should change from STAT 7. to STAT 0. also the length of the line should not change during the replace.. your help is appriciated thanks Antony |
|
|||
|
This worked except the output printed only the ones that were found on exception file..all the records were not printed.
|
|
|||
|
using grep and sed with a while loop
Try this, Code:
#! /bin/ksh
cp inp.txt tmp_1.txt
while read line
do
sed "/$line/s/STAT [0-9][0-9]*/STAT 0/g" tmp_1.txt > tmp_2.txt
cp tmp_2.txt tmp_1.txt
done < exp.txt
cat tmp_2.txt
rm tmp_2.txt tmp_1.txtinp.txt Code:
0622 0031 JOHN MAX 20080622003104. STAT 1 0622 0031 PETER MAX 20080622003104. STAT 3 0622 0031 BILL MAX 20080622003104. STAT 7 0622 0031 MARY MAX 20080622003104. STAT 2 exp.txt Code:
BILL PETER Regards, Chella |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| compare files | antointoronto | Shell Programming and Scripting | 8 | 06-24-2008 06:13 PM |
| compare files | prashanth.spl | Shell Programming and Scripting | 0 | 06-18-2008 05:22 PM |
| Compare & replace contents within a file | kaustubh137 | Shell Programming and Scripting | 2 | 05-27-2008 07:56 AM |
| compare files | spt | Shell Programming and Scripting | 1 | 05-09-2008 11:57 AM |
| compare files | ingunix | UNIX for Dummies Questions & Answers | 3 | 05-24-2001 12:44 PM |