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

one.txt

ONS.1287677000.820.log 20Oct2010
ONS.1287677000.123.log 21Oct2010
ONS.1287677000.456.log 22Oct2010

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[]

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

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.

Hi, hat have you tried so far?

nawk 'FNR==NR{f1[$1]=$2;next} $1 in f1 {$2=f1[$1] FS $2;print}' one.txt FS=: two.txt
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

# 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

@ 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.

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?

nawk -F: '{getline x<f;$0=x FS $2}1' f=file1 file2

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

paste 1.txt 2.txt | sed 's|<Ctrl>+<V><tab>.*:|:|'

you would have :

paste 1.txt 2.txt | sed 's|<space>.*:|:|'

of course the <key> stand for the button you'd press
so on your screen :

paste 1.txt 2.txt | sed 's| .*:|:|'

otherwise, you can try this ?

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

or more simply

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

@ 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.

@ctsgnb: does your sed not support \t ?

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

:wink:

nawk -F: '{getline x<f;$0=x FS $2}1' f=two.TXT one.TXT

Wrong O/p:

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.

@

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

i repeat my question

 
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.

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.

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

@ctsgnb

 
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.

You're right I hadn't looked at your spec well enough. This should hopefully work better (similar to vgersh99 suggestion):

nawk 'NR==FNR{A[$1]=$0;next}$1=A[$1]' file1 FS=: OFS=: file2

@scrutinizer

 nawk 'NR==FNR{A[$1]=$0;next}$1=A[$1]' two.TXT FS=: OFS=: one.TXT

nothing comes in o/p.

I think you should have one.txt and two.txt reversed, no?

nawk 'NR==FNR{A[$1]=$0;next}$1=A[$1]' one.TXT FS=: OFS=:two.TXT

no output comes

---------- Post updated at 07:14 AM ---------- Previous update was at 07:14 AM ----------

 
nawk 'NR==FNR{A[$1]=$0;next}$1=A[$1]' one.TXT FS=: OFS=:two.TXT
no output comes.
{ IFS=":" ; while read a b ; do echo "$(grep $a 1.txt):$b" ; done } <2.txt
# 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[]
# { IFS=":" ; while read a b ; do echo "$(grep $a 1.txt):$b" ; done } <2.txt
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[]
#

The should be a space between OFS=: and two.txt