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.
# 8  
Old 10-26-2010
If my code just merge the files, then it means that you have to reconsider the way you entered the sed statement.
Indeed, as shown in my example, it should remove the unwanted ONS*log

If your default separator for paste command is a space instead of a tab, just change it into the sed statement
so instead of
Code:
paste 1.txt 2.txt | sed 's|<Ctrl>+<V><tab>.*:|:|'

you would have :
Code:
paste 1.txt 2.txt | sed 's|<space>.*:|:|'

of course the <key> stand for the button you'd press
so on your screen :
Code:
paste 1.txt 2.txt | sed 's| .*:|:|'

otherwise, you can try this ?

Code:
paste 1.txt 2.txt | sed 's|.ONS.[0-9]*.[0-9]*.log||'

or more simply
Code:
paste 1.txt 2.txt | sed 's|.ONS.*log||'

in this statement the dot "." before the ONS is what makes it match the second ONS.*log occurrence instead of the first one

Last edited by ctsgnb; 10-26-2010 at 11:58 AM..
# 9  
Old 10-26-2010
@ vgersh99
My appologies to doubt on ur skills.

Actually issue was with extention. i tried by .txt but the actual was .TXT

thank you every one for you time.
# 10  
Old 10-26-2010
@ctsgnb: does your sed not support \t ?
# 11  
Old 10-26-2010
Yep you true i have just tryied it : it does ..Lol

Since i get to use vi, i robotically used the <ctrl+v> <key> combination which worked as well but your true, for a better understanding for the reader, next time i will use the \t

Smilie
# 12  
Old 10-27-2010
Code:
nawk -F: '{getline x<f;$0=x FS $2}1' f=two.TXT one.TXT

Wrong O/p:
Code:
ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]:V[4.1] 20Oct2010
ONS.1287677000.123.log:V[4.1.2] AC[ONS] CC[73] EN[]:V[4.1] 21Oct2010
ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]:V[4.1] 22Oct2010

But for above line o/p should be "20Oct2010" because the pattern "ONS.1287677000.820" is same in first and third line.

@
Code:
paste 1.txt 2.txt | sed 's|^  .*:|:|'      this is also fail in case of pattern matching.

i repeat my question
Code:
 
one.txt
ONS.820.log:V[4.1] 20Oct2010
ONS.123.log:V[4.1] 21Oct2010
ONS.234.log:V[4.1] 30Oct2010
two.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[]

o/p should be according to pattern("ONS.1287677000.820" and ONS.1287677000.123 in above file.)
need to merge date("20Oct2010") either in middle, or at start or at the end of each line("ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]"), but according to pattern mean file two.txt look for pattern in file one.txt and then merge according to that.
Code:
one form of output
ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]:V[4.1] 20Oct2010
ONS.1287677000.123.log:V[4.1.2] AC[ONS] CC[73] EN[]:V[4.1] 21Oct2010
ONS.1287677000.820.log:V[4.1.2] AC[ONS] CC[73] EN[]:V[4.1] 20Oct2010
but let say if two.txt have two rows and one.txt has 3 rows,then also it will show o/p acc to 2 lines present in two.txt.

[B]Please advice as all of above not giving expected o/p.[/B]

@ctsgnb
Code:
 
paste two.TXT one.TXT | sed 's|.ONS.*log||'
ONS.1287677000.820.log:V[4.1.2] AC:V[4.1] 20Oct2010
ONS.1287677000.123.log:V[4.1.2] AC:V[4.1] 21Oct2010

This is also wrong o/p , as in two.txt , there are 3 lines which is unsync with output as it is showing 2 lines.

Last edited by saluja.deepak; 10-27-2010 at 08:06 AM.. Reason: code tags please!!!
# 13  
Old 10-27-2010
You're right I hadn't looked at your spec well enough. This should hopefully work better (similar to vgersh99 suggestion):
Code:
nawk 'NR==FNR{A[$1]=$0;next}$1=A[$1]' file1 FS=: OFS=: file2

# 14  
Old 10-27-2010
@scrutinizer
Code:
 nawk 'NR==FNR{A[$1]=$0;next}$1=A[$1]' two.TXT FS=: OFS=: one.TXT

nothing comes in o/p.
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