Look up 2 files and print the concatenated output

file 1

Sun Mar 17 00:01:33 2013 submit , Name="1234"
Sun Mar 17 00:01:33 2013 submit , Name="1344"
Sun Mar 17 00:01:33 2013 submit , Name="1124"
..
..
..
..
Sun Mar 17 00:01:33 2013 submit , Name="8901"

file 2

Sun Mar 17 00:02:47 2013 1234 execute SUCCEEDED
Sun Mar 17 00:02:48 2013 1344 execute FAILED
Sun Mar 17 00:02:48 2013 1124 execute SUCCEEDED
Sun Mar 17 00:02:49 2013 1222 execute SUCCEEDED
Sun Mar 17 00:02:49 2013 1111 execute SUCCEEDED
Sun Mar 17 00:02:50 2013 1211 execute SUCCEEDED
Sun Mar 17 00:02:50 2013 1111 execute SUCCEEDED
Sun Mar 17 00:02:51 2013 8901 execute FAILED

both the file should be looked up and the out put should be

Sun Mar 17 00:01:33 2013 submit , Name="1234" execute SUCCEEDED
Sun Mar 17 00:01:33 2013 submit , Name="1344" execute FAILED
..
..
..

Any solution please...

This question asked many times and answered too. Based on awk using NR==FNR condition.

Search in the forum , give a try your self and if you are stuck some where pls report.

awk -F, 'BEGIN{OFS=","}FNR==NR{a[$1$2]=$3;next}($1$2 in a && $3=$3","a[$1$2])'

Not sure, but your solution did not work for me. The below one is working ok for me.

$ cat f1
Sun Mar 17 00:01:33 2013 submit , Name="1234"
Sun Mar 17 00:01:33 2013 submit , Name="1344"
Sun Mar 17 00:01:33 2013 submit , Name="1124"

$ cat f2
Sun Mar 17 00:02:47 2013 1234 execute SUCCEEDED
Sun Mar 17 00:02:48 2013 1344 execute FAILED
Sun Mar 17 00:02:48 2013 1124 execute SUCCEEDED
Sun Mar 17 00:02:49 2013 1222 execute SUCCEEDED
Sun Mar 17 00:02:49 2013 1111 execute SUCCEEDED
Sun Mar 17 00:02:50 2013 1211 execute SUCCEEDED
Sun Mar 17 00:02:50 2013 1111 execute SUCCEEDED
Sun Mar 17 00:02:51 2013 8901 execute FAILED


$ awk 'BEGIN { FS="[=\t\" ]+" } NR==FNR{a[$6]=$7" "$8;next} {if($9 in a) {print $0" "a[$9]}}' f2 f1
Sun Mar 17 00:01:33 2013 submit , Name="1234"  execute SUCCEEDED
Sun Mar 17 00:01:33 2013 submit , Name="1344"  execute FAILED
Sun Mar 17 00:01:33 2013 submit , Name="1124"  execute SUCCEEDED

Tks for the query but one more thing , how can i give input as date in the awk command

suppose i want to awk nawk '/Sun/ && p{print p;p=""}{p=p $0}END{if(p) print p}'

I want to use TZ=IST+24 date +%a instead of giving Sun , how it is possible to give in Awk

Assign the value to a variable and pass it to awk using "-v"  and then use it inside awk.

nawk -v U="(TZ=IST+24 date +%a)" /U/ && p{print p;p=""}{p=p $0}END{if(p) print p}'

Is It working?

U="(TZ=IST+24 date +%a)" 

you missed a $ sign here.

It should be

U=$(TZ=IST+24 date +%a)"

I tried to use the variable it is giving error like not a valid token , can some one help.

Actually the file1.txt has multiple lines , i tried to join lines based on the date and day post which i am trying to the awk to merge the 2 files.

The command is failing

Hi Aravind,

Your requirement is keep changing. Pls post , sample input and the output you are expecting from it ( mention the condition too ) . Use code tags to place your data.

$ cat f1
Sun Mar 17 00:01:33 2013
submit ,Name="1234"
Hello 123
how are you
Sun Mar 17 00:01:33 2013
submit , Name="1344"
Hello 345
how are you
Sun Mar 17 00:01:33 2013
submit , Name="1124"
Hello 456
how are you

$ nawk '/^Sun/ && p{print p;p=""}{p=p $0}END{if(p) print p}' f1 > f3.txt
nawk '/^`TZ=IST+24 Date + %a`/ && p{print p;p=""}{p=p $0}END{if(p) print p}' f1 > f3.txt

I executed the above script to join all the lines based on the Day . I am not able to give input as `TZ=IST+24 Date + %a` in this and if i give all the lines get concatenated and the whole file becomes 1 line.

But if i manually give expression as /^Sun/ it is working perfectly , but the same is not working if i try with `TZ=IST+24 Date + %a` as input.

Any solution to crack it.

Try like this:

$ awk -vDT=$(date +%a) '
         NR==FNR        {TMP[$6]=$7 FS $8; next}
         $0~DT          {getline X
                         $0=$0 ", "X
                         gsub (/[^0-9]/, "", X)
                         print $0, TMP[X]}
        ' file1 file
Sun Mar 17 00:01:33 2013, submit ,Name="1234" execute SUCCEEDED
Sun Mar 17 00:01:33 2013, submit ,Name="1344" execute FAILED
Sun Mar 17 00:01:33 2013, submit ,Name="1124" execute SUCCEEDED
nawk '/^Sun/ && p{print p;p=""}{p=p $0}END{if(p) print p}' f1 > f3

The above helps me to concatenate all the lines that are in between from the Sun to Next Sun.

Please tell me how i can replace /^Sun/ in the above script , that will be helpful.

$ U="$(TZ=IST+24 date +%a)"

$ awk '/^'$U'/  && p{print p;p=""}{p=p $0}END{if(p) print p}' f1 > f3

Got Error like this in Unix

nawk: syntax error at source line 1
context is
>>> /^$(TZ=IST+24 <<<
nawk: bailing out at source line 1

Why are you using the expression there? Did you read my previous post?

Assign it to a variable and use the variable instead.

cat f1

Mon Mar 18 00:06:22 2013 bulk WorkOrderIntf::submit wo={ woName="3885582_2" woInfo="test1" soAction=active soName="a23ba23ba23ba41" soAction=active soName="a23ba23ba23ba41" soAction=active soName="a23ba23ba23ba41"
Mon Mar 18 00:06:25 2013 bulk WorkOrderIntf::submit wo={ woName="3885587_2" woInfo="test1" soAction=active soName="a23ba23ba23ba07" soAction=active soName="a23ba23ba23ba07" soAction=active soName="a23ba23ba23ba07"
Mon Mar 18 00:06:34 2013 bulk WorkOrderIntf::submit wo={ woName="3885605_2" woInfo="test1" soAction=active soName="a23ba23ba23ba87" soAction=active soName="a23ba23ba23ba87" soAction=active soName="a23ba23ba23ba87"
Mon Mar 18 00:06:37 2013 bulk WorkOrderIntf::submit wo={ woName="3885604_2" woInfo="test1" soAction=active soName="a23ba23ba23ba32" soAction=active soName="a23ba23ba23ba32" soAction=active soName="a23ba23ba23ba32"
Mon Mar 18 00:16:07 2013 bulk WorkOrderIntf::submit wo={ woName="3885607_2" woInfo="test1" soAction=active soName="a23ba23ba23ba63" soAction=active soName="a23ba23ba23ba63" soAction=active soName="a23ba23ba23ba63"
Mon Mar 18 00:16:10 2013 bulk WorkOrderIntf::submit wo={ woName="3885609_2" woInfo="test1" soAction=active soName="a23ba23ba23ba99" soAction=active soName="a23ba23ba23ba99" soAction=active soName="a23ba23ba23ba99"
Mon Mar 18 00:16:11 2013 bulk WorkOrderIntf::submit wo={ woName="3885610_2" woInfo="test1" soAction=active soName="a23ba23ba23ba79" soAction=active soName="a23ba23ba23ba79" soAction=active soName="a23ba23ba23ba79"

cat f2

Mon Mar 18 00:06:24 2013 3885582_2 execute SUCCEEDED
Mon Mar 18 00:06:25 2013 3885587_2 execute SUCCEEDED
Mon Mar 18 00:06:34 2013 3885605_2 execute SUCCEEDED
Mon Mar 18 00:06:38 2013 3885604_2 execute SUCCEEDED
Mon Mar 18 00:16:10 2013 3885607_2 execute SUCCEEDED
Mon Mar 18 00:16:11 2013 3885609_2 execute SUCCEEDED
Mon Mar 18 00:16:11 2013 3885610_2 execute SUCCEEDED
Mon Mar 18 00:16:13 2013 3885608_2 execute SUCCEEDED
Mon Mar 18 00:16:14 2013 3885613_2 execute SUCCEEDED
Mon Mar 18 00:16:15 2013 3885618_2 execute SUCCEEDED
Mon Mar 18 00:16:15 2013 3885615_2 execute SUCCEEDED
Mon Mar 18 00:16:19 2013 3885623_2 execute SUCCEEDED
Mon Mar 18 00:16:20 2013 3885624_2 execute SUCCEEDED
Mon Mar 18 00:16:24 2013 3885626_2 execute SUCCEEDED
Mon Mar 18 00:25:00 2013 3885634_2 execute SUCCEEDED
Mon Mar 18 00:25:00 2013 3885628_2 execute SUCCEEDED
Mon Mar 18 00:25:01 2013 3885635_2 execute SUCCEEDED
Mon Mar 18 00:40:15 2013 3885641_2 execute SUCCEEDED

Still look up is failing from f2 and f1.. from f2 i need the value need to be picked up example "3885582_2" and look in the file f1 and append the status to file3.

the field from f2 has to go for complete look up in the f1 and then only it should be go to the next line and pick up the same field..

Hi Aravind,

Every time you are hijacking your own post. Each time your are posting different questions and expecting different output. In your last post, you posted some random files, neither you mentioned output expected not what you have tried so far.

nawk 'BEGIN { FS="[=\t\" ]+" } NR==FNR{a[$6]=$9" "$8;next} {if($9 in a) {print $0" "a[$9]}}'  f2 f1 

The output is coming as NULL...

Please help as i am struck..

Pls post the output expected.