Concatenate last field values for all occurences

Hello all,

Maybe you can help me with an awk script to get what I need.

I have the input file with format below:

[2013-04-02 00:00:00.014] REQUEST|79023787741690|738227864597|985
[2013-04-02 00:00:00.215] REQUEST|79024002151717|738229423534|985
[2013-04-02 00:00:00.215] REQUEST|79024002151717|738229423534|*985
[2013-04-02 00:00:00.215] NDS-REQUEST|79024002151717|738229423534
[2013-04-02 00:00:00.215] REQUEST|79023364221647|738227504615|985
[2013-04-02 00:00:00.297] EXT-RESPONSE|79023998911716|738225664496|*702|Active|0|233
[2013-04-02 00:00:00.299] RESPONSE|79023998911716|738225664496|*702|233
[2013-04-02 00:00:00.481] NDS-RESPONSE|79024002151717|738229423534|1|AMB
[2013-04-02 00:00:00.482] RESPONSE|79024002151717|738229423534|*985|233
  • Fields are separated with "|" and values in field 2 may have more than one occurence.
  • I want to print only one time each value that is in field 2, where field 1 contains exactly "REQUEST" or "RESPONSE/EXT-RESPONSE",
    and concatenate all the values in last fields in each ocurrence.

For example,

  • 79023787741690 only appears once, then only has one "last field" that is 985
  • 79024002151717 appears 5 times, but only 4 times, the line where it's present contains exactly "REQUEST" or "RESPONSE/EXT-RESPONSE".
    and the last field (in order of appearence) for each occurence are 985, *985, AMB and 233

So the output for this input would be:

79023787741690|985
79024002151717|985|*985|AMB|233
79023364221647|985
79023998911716|233|233

I hope make ssense. Thanks in advance.

Try:

awk -F'[ \t]*|\\|' '$3~/^REQUEST$|RESPONSE/ {A[$4]=A[$4] OFS $NF} END{for(i in A) print i A}' OFS=\| file
1 Like

Hello Scrutinizer,

Thank you for your help. It works just fine.

May you explain how is made the regexp for field separator. I understand that
this [ \t] means space or tab, but the next part, why not only one "|"?

Thanks again

Hi Ophiuchus, the pipe symbol is a special character (alternation operator) that needs to be escaped if intended literally. Behind the -F is a string that contains a regex, which requires the escape character ( \ ) to be escaped itself, hence the double \\

Hello Scrutinizer,

Many thatnks for your help. You've helped me a lot.

Regards.