0

I have a stdout:

jh888636 4533 cklsls 55885
sp838922 7373 ksshhs 77321
dp828273 8722 kiwjkk 72661

and a static file to lookup:

"ah768889"|"Aron Hease"|"Louis Jid"|"99949"
"jh888636"|"John Holmes"|"Peter King"|"60036"
"sp838922"|"Steven Parn"|"Jay Walker"|"60004"
"dp828273"|"David Pim"|"Kate Jast"|"89488"

Now, my goal is to match column 1 from stdout to column 1 in the static file and extract/print column2/column4 etc

Exampled desired output:

John Holmes (60036) 4533 cklsls 55885
Steven Parn (60004) 7373 ksshhs 77321
David Pim   (89488)  8722 kiwjkk 72661
0

Another awk approach:

... |awk 'NR==FNR{one=$1;$1="";seen[one]=$0; next} 
{FS="[|\"]"} seen[$2]{printf ("%s (%s) %s\n", $5, $(NF-1), seen[$2])}' - static

Will give output:

John Holmes (60036)  4533 cklsls 55885
Steven Parn (60004)  7373 ksshhs 77321
David Pim (89488)  8722 kiwjkk 72661

At above in - static; the dash - means awk to read from stdin and the static is your staticfile.

| improve this answer | |
-1

I have done this by 4 steps

  1. sed "s/|/ /g" staticfile_lookup | sed 's/"//g' >> static_file_lookup_final.txt

    1. awk 'NR==FNR{a[$1];next}($1 in a){print $2,$3,$NF}' stdout static_file_lookup_final.txt >>static_file_lookup_fina1_1.txt
  2. awk '{$1="";print $0}' stdout >>stdout_final.txt

==========

cat static_file_lookup_fina1_1.txt 
John Holmes 60036
Steven Parn 60004
David Pim 89488

============

 cat stdout_final.txt 
 4533 cklsls 55885
 7373 ksshhs 77321
 8722 kiwjkk 72661

=================

  1. paste static_file_lookup_fina1_1.txt stdout_final.txt

Output

John Holmes 60036    4533 cklsls 55885
Steven Parn 60004    7373 ksshhs 77321
David Pim 89488  8722 kiwjkk 72661
| improve this answer | |
  • -1 You are replacing Pipes with Space sed "s/|/ /g" and printing $2,$3,$NF. What if names in second column (separated with pipe) has no space or has middle name? + no (...) is formatted. you could do all with single awk – αғsнιη Apr 22 '18 at 10:33

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.