compare 2 files and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare 2 files and replace
# 1  
Old 06-24-2008
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..
# 2  
Old 06-24-2008
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'NR==FNR{_[$1];next}$NF=$3 in _?0".":$NF' exceptions names

# 3  
Old 06-25-2008
Quote:
Originally Posted by radoulov
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'NR==FNR{_[$1];next}$NF=$3 in _?0".":$NF' exceptions names

sorry I made a mistake in the output. the output should be

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
# 4  
Old 06-25-2008
using grep and sed

You can also do this using grep and sed

Code:
grep -f exp.txt inp.txt | sed 's/STAT [0-9][0-9]*/STAT 0/g'

exp.txt is the exception file.

Regards,
Chella
# 5  
Old 06-25-2008
Quote:
Originally Posted by antointoronto
sorry I made a mistake in the output. the output should be

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..
[...]
Since in your first post ti was JOHN and not BILL in the exception file the code works as expected ...
1. If an entry in the names file matches an entry in the exception file should the stat change only if the current value is 7?
2. You mentioned the record length and I suppose it's because of the recalculation of the current record; could you post sample of your input data using the code tags.
# 6  
Old 06-26-2008
Quote:
Originally Posted by chella
You can also do this using grep and sed

Code:
grep -f exp.txt inp.txt | sed 's/STAT [0-9][0-9]*/STAT 0/g'

exp.txt is the exception file.

Regards,
Chella
This worked except the output printed only the ones that were found on exception file..all the records were not printed.
# 7  
Old 06-26-2008
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.txt

inp.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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare and replace two columns from two files

Hello, I have two text tab delimited files File 1 has 30 columns. I am pasting only first 9 Chr Position Ref Alt Score Gene HGVS_C HGVS_P Coding_Consequence dbSNP 1 17312743 C T 1 Gene1 - ... (2 Replies)
Discussion started by: nans
2 Replies

2. Solaris

XML value compare and replace

I need a way to to check if a value in a file that has this XML format is less than or equal to current system date/time. if it is I need to override it with a future date/time: Here is the data sample: <?xml version="1.0"... (1 Reply)
Discussion started by: mrn6430
1 Replies

3. Shell Programming and Scripting

Compare first column of 2 files and replace

Hi All, I have 2 files in the following format : File 1 S00999999|BHANU|TEST|007 JOHN DOE APT 999||VENGA HIGHWAY|MA|09566|SCO DUAL|20140201|20140331|20140401|20140630|20140327| S00888888|BU|TES|009 JOHN DOE APT 909||SENGA HIGHWAY|MA|08566|SCO... (1 Reply)
Discussion started by: nua7
1 Replies

4. Shell Programming and Scripting

Compare File & Copy Replace if Successful

Hi All, I have written a shell script that creates a backup of my MySQL database. The script performs the following functions: Creates a Backup of the MySQL database Compresses the Backup Copies the Backup to a Remote Server Send an E-Mail displaying the size of the Backup Removes any... (6 Replies)
Discussion started by: SalientAnimal
6 Replies

5. Shell Programming and Scripting

Compare and Replace

Hi, I'm running a shell script to do a daily backup of my mysql database. The backup is done using: mysqldump db1 > db1.sql --user=rootuser --password=rootpassword && \ mysqldump db2 > db2.sql --user=rootuser --password=rootpassword This file is saved in my root directory replacing the... (7 Replies)
Discussion started by: SalientAnimal
7 Replies

6. Shell Programming and Scripting

awk compare column n replace with in one file

hi Friends need to compare columns in one file where the data looks like below laptop,IBM phone,samsung car,rental user1,laptop user2,laptop user3,phone want to get output as laptop,IBM phone,samsung car,rental user1,IBM user2,IBM user3,samsung need to seach $2 in array of $1 and... (4 Replies)
Discussion started by: arun1401
4 Replies

7. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

8. UNIX for Dummies Questions & Answers

How to compare 2 files & get specific value & replace it in other file.

Hiiii Friends I have 2 files with huge data. I want to compare this 2 files & if they hav same set of vales in specific rows & columns i need to get that value from one file & replace it in other. For example: I have few set data of both files here: a.dat: PDE-W 2009 12 16 5 29 11.11 ... (10 Replies)
Discussion started by: reva
10 Replies

9. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

10. Shell Programming and Scripting

Compare & replace contents within a file

I have 2 files file1 1 TMQUEUE QUE1 STMW633A 100 DMADM DOMGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633AA STMW633A 100 GWADM GWTGRPSTMW638A STMW638A 100 TMSYSEVT EVTGRPSTMW633A STMW633A 100 TMSYSEVT ... (2 Replies)
Discussion started by: kaustubh137
2 Replies
Login or Register to Ask a Question