Shell Scripting: Compare pattern in two files and merge the o/p in one.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Scripting: Compare pattern in two files and merge the o/p in one.
# 1  
Old 10-26-2010
Question Shell Scripting: Compare pattern in two files and merge the o/p in one.

one.txt
Code:
ONS.1287677000.820.log 20Oct2010
ONS.1287677000.123.log 21Oct2010
ONS.1287677000.456.log 22Oct2010

two.txt
Code:
ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.123.log:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]

In file two.txt
i have to look for pattern which column one "ONS...."of file "one.txt" and want to make one file(either new or update one.txt) with merge o/p. for example
Code:
ONS.1287677000.820.log 20Oct2010:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.123.log 21Oct2010:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.820.log 20Oct2010:V[4.1.2] AC[ONS] CC[73] EN[]

Please please help here.Thanks in advace.

Last edited by vgersh99; 10-26-2010 at 11:22 AM.. Reason: code tags, please!
# 2  
Old 10-26-2010
Hi, hat have you tried so far?
# 3  
Old 10-26-2010
Code:
nawk 'FNR==NR{f1[$1]=$2;next} $1 in f1 {$2=f1[$1] FS $2;print}' one.txt FS=: two.txt

# 4  
Old 10-26-2010
Code:
paste 1.txt 2.txt | sed 's|^  .*:|:|'

note that this ^ is a nested display, in fact it correspond to the default separator of paste (here <tab> key so since it is considered as special char to enter, i had to use <Ctrl>+<V> keys and then <tab> key

Code:
# cat 1.txt
ONS.1287677000.820.log 20Oct2010
ONS.1287677000.123.log 21Oct2010
ONS.1287677000.456.log 22Oct2010
# cat 2.txt
ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.123.log:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]
# paste 1.txt 2.txt | sed 's|^  .*:|:|'
ONS.1287677000.820.log 20Oct2010:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.123.log 21Oct2010:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.456.log 22Oct2010:V[4.1.2] AC[ONS] CC[73] EN[]
#

You also may consider a man join

Last edited by ctsgnb; 10-26-2010 at 11:37 AM..
# 5  
Old 10-26-2010
@ ctsgnb
Dear, this is murging both the line . Thanks for ur help here.
ONS.1287677000.820.log 20Oct2010 ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]
ONS.1287677000.123.log 21Oct2010 ONS.1287677000.123.log:V[4.1.2] AC[ONS] CC[73] EN[]

@ vgersh99
Sir, comand mentioned by u not working , facing error while executing
nawk 'FNR==NR{f1[$1]=$2;next} $1 in f1 {$2=f1[$1] FS $2;print}' one.txt FS=: two.txt
awk: 0602-533 Cannot find or open file one.txt.
The source line number is 1.
# 6  
Old 10-26-2010
Quote:
Originally Posted by saluja.deepak
@ ctsgnb
@ vgersh99
Sir, comand mentioned by u not working , facing error while executing
nawk 'FNR==NR{f1[$1]=$2;next} $1 in f1 {$2=f1[$1] FS $2;print}' one.txt FS=: two.txt
awk: 0602-533 Cannot find or open file one.txt.
The source line number is 1.
Why do you think this is?
In your original post you quoted 2 files: one.txt and two.txt
Do you have these files? Are they named one.txt and two.txt as posted by you?
# 7  
Old 10-26-2010
Code:
nawk -F: '{getline x<f;$0=x FS $2}1' f=file1 file2

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 merge two big CSV files

Hi all, i need help. I have two csv files with a huge amount of data. I need the first column of the first file, to be compared with the data of the second, to have at the end a file with the data not present in the second file. Example File1: (only one column) profile_id 57036226... (11 Replies)
Discussion started by: SirMannu
11 Replies

2. Shell Programming and Scripting

Compare two files and merge into third

Hello: Newbie with Awk. Trying to compare two files and merge data based on CID. Please see the input file format and desired output. Any help is appreciated. TIA Input File1 CID1 --- TYP1 --- DCN1 --- INDATE1 --- IN-DATA1 CID2 --- TYP2 --- DCN2 --- INDATE2 --- IN-DATA2 CID3 ---... (6 Replies)
Discussion started by: wincrazy
6 Replies

3. Shell Programming and Scripting

Compare and Merge files

Hi All, I have two different files as shown below separated by a "|". I need to compare the first column from both the files and if they match merge both the columns. File 1 "S00172012"|"CHRONIC RENAL FAILURE"|""|"I" "S00159962"|"SUBENDO INFRC-INIT EPISD"|""|"I" "S00255303"|"BENIGN... (6 Replies)
Discussion started by: nua7
6 Replies

4. Shell Programming and Scripting

Compare files using Unix scripting

I have a file containing the below data obtained after running a diff command > abc 10 < abc 15 > xyz 02 <xyz 05 ..... Does anyone know how i can obtain output like : previous value of abc is 10 and present value is 15 similarly for all the comparisons in the text file (10 Replies)
Discussion started by: amithpatrick1
10 Replies

5. Shell Programming and Scripting

Compare two files using shell scripting

Hi, I need to compare two files using shell scripting. Say like: File1 AAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCCCC eeeeeeeeeeeeeeeeeeeeeeeee DDDDDDDDDDDDDDDDDDDDDDDDDDDD File2 BBBBBBBBBBBBBBBBBBBBB DDDDDDDDDDDDDDDDDDDDDDDDDDDD AAAAAAAAAAAAAAAAAAAA ... (6 Replies)
Discussion started by: roshParab
6 Replies

6. Shell Programming and Scripting

Shell scripting for this sequence to compare

I have two input files (given below) and to compare each line of the File1 with each line of File2 starts with '>sample1'. If a match occurs and that matched line in the File2 contains another line or sequence of lines starting with "Chr" they have to be displayed in output file with that sample.... (4 Replies)
Discussion started by: hravisankar
4 Replies

7. UNIX for Dummies Questions & Answers

compare columns from 2 files and merge

Dear all, Being new to Unix i have a problem. I have 2 files: File 1: 118,1,0,2,3,0,5,0.3,0,0.3,0.6,1 118,2,1,2,2,0,5,0.4,0,0.4,0.4,1 118,4,2,0,3,0,5,0.7,0,0.3,0.6,1 118,6,4,1,0,0,5,0.8,0,0.2,0,1 File 2: 118,1,BFGL-NGS-109695,3610326,0,18,1,0.556,0.389,0.056,0.25,0.8183... (2 Replies)
Discussion started by: samwilkinson
2 Replies

8. Shell Programming and Scripting

How to compare a command line parameter with -- in shell scripting

Hi, I need to check if a parameter provided at the command line is equal to --.How can i do that ? Please help me. Thanks and Regards, Padmini (4 Replies)
Discussion started by: padmisri
4 Replies

9. Shell Programming and Scripting

compare the column from 3 files and merge that line

I have 3 file, each of has got 80000 records. file1.txt ----------------------- ABC001;active;modify;accept; ABC002;notactive;modify;accept; ABC003;notactive;no-modify;accept; ABC004;active;modify;accept; ABC005;active;no-modify;accept; file2.txt ---------------------------... (8 Replies)
Discussion started by: ganesh_mak
8 Replies

10. Shell Programming and Scripting

Compare two files and merge columns in a third

Hi, I'm working with snmp, with a little script I'm able to obtain from a switch a list with a couple of values with this format Port Mac 1 00:0A:0B:0C:0D:0E .... (hundred of entries) Now with a simple arp on a router I am able to obtain another list 00:0A:0B:0C:0D:0E... (20 Replies)
Discussion started by: CM64
20 Replies
Login or Register to Ask a Question