简体   繁体   中英

Issue in Generating new linux file

I am trying to just perform a vlookup from file 1 to file 2 using linux commands whereas i am getting proper results in wrong format.

To make it more clear,

File 1 :

http://www.amazon.com/dp/B00006IBAX test1_test  test3   test2   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4

File 2 :

http://www.amazon.com/dp/B00006IBAX

Desired Output :

  http://www.amazon.com/dp/B00006IBAX   test1_test  test3   test2   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4

Code I used :

FNR==NR{s=$1; sub(".*"$2,"");a[s]=$0; next} a[$1]{OFS = "\t"; FS = "\t"; print $0 a[$1]}

Output I got :

http://www.amazon.com/dp/B00006IBAX_test    test3   test2   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4

There goes some mis-alignment and due to which i am not able to process the file. I want the data in file 1 exactly in file 2 if the lookup result succeeds. Please, help me on this

No need for awk , there is join command:

join -t$'\t' file1 file2

So, given your original inputs, you should now see:

http://www.amazon.com/dp/B00006IBAX test1_test  test3   test2   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4

How this works

Excerpt from man join :

For each pair of input lines with identical join fields, write a line to standard output. The default join field is the first, delimited by whitespace.

  • -t specifies delimiter, in your case from your awk code and text files, looks like you are aiming to deal with tab-delimited files
  • Since we don't want default whitespace as delimiter, we need to specify tab. But there is a trick to tabs, due to a quirk of join in that if we specified -t '\\t' , join seems to see \\t as literally two characters \\ and t , and gives an error.
  • So to specify tab, one way, is to do a literal tab, type -t ' then ctrl - v , then tab to insert a literal tab, then '
  • or, what I feel is simpler, as we have done here, use C-style escaped tab -t$'\\t'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM