I have file-1 which has list of transaction id .

File -1


I have a file-2 which has the logs .It can have pipes in other place .So pipe cannot be used as delimiter


logline-1|somefield fdfdsff dfwwtwtwt **201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|**
logline-2|somefield fdfdsff dfwwtwtwt **201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|**
logline-3|somefield fdfdsff dfwwtwtwt **201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|**
logline-4|somefield fdfdsff dfwwtwtwt **201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|**
logline-5|somefield fdfdsff dfwwtwtwt **201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|**

How can i use awk to read all contents in file-1 ... grep on file 2 and create a file 3 which has (transaction id - occurrences in file 2)

Expected File-3

201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|     2
201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|     1
201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|     2
  • 1
    I add values from the other question. thoses strings are more interesting. – Archemar Apr 3 '15 at 13:50

You can try with awk:

awk 'FNR == NR { v[$1] = 0; next; } { v[$4]++; } END { for (elem in v) { print elem, v[elem]; } }' File-1 File-2 >outfile

So result will be:

201503301616|9b8791b3-f860-409f-aad0-24debf834c1b| 1
201503301616|9b8791b3-f860-409f-aad0-24debf834c1b| 2
201503301616|9b8791b3-f860-409f-aad0-24debf834c1b| 2

But again I do not know why you need 2 files. You can do it with File-2:

awk '{ v[$4]++; } END { for (elem in v) { print elem, v[elem]; } }' File-2 >outfile

Result will be the same.

  • sorry for the typo in my file-2 .There is not really a space in between tranid-3 .But isnt there a way where i don't have to mention the search strings position in file-2 . – Dhyan Apr 3 '15 at 12:46
  • Then you should try the first one. And why you nedd 2 files? – taliezin Apr 3 '15 at 12:47
  • Probably i should be more specific .. My transaction id looks like : "201503301616|9b8791b3-f860-409f-aad0-24debf834c1b|" and my file 2 can have pipes in other field too .(So i cannot use pipe as delimiter to perfectly extract transaction id )So i would need to do somthing like |grep wc-l – Dhyan Apr 3 '15 at 12:51
  • If it does not help, you can edit the question and I think someone could help. I can delete this answer as irrelevant. – taliezin Apr 3 '15 at 13:04
  • 1
    the answers seems to fit with new value (double star was used to bold other question). – Archemar Apr 3 '15 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.