The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 05-27-2009
maxim42 maxim42 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 15
can any one explain this example

hi all i have an example i want one help me to understand cause i tried to test it but almost fail and i don't know how can i solve this problem " the main idea to read from two files and replace something from one to another " but i don't understand why it fail all time


Code:
$ cat main.txt
512|1241503759|ax|90
234|1241503760|ay|10
122|1241503823|az|90
123|1241503947|at|80

Code:
$ cat id.txt
122|US
123|IN
125|NZ
234|HK
512|ZM
600|KR

Code:
$ awk '
    BEGIN {FS=OFS="|"}
    FNR==NR{a[$1]=$2;next}
    $1 in a{print a[$1],$2,$3,$4}
' id.txt main.txt
Code:
Output:
ZM|1241503759|ax|90
HK|1241503760|ay|10
US|1241503823|az|90
IN|1241503947|at|80
i tried this
Code:
awk 'BEGIN { FS=OFS="|" } FNR==NR{ a[$1]=$2;next } $1 in a { print a[$1] }' id.txt  main.txt
awk: syntax error near line 1
awk: bailing out near line 1
when i remove " $1 in a "

Code:
awk 'BEGIN { FS=OFS="|" } FNR==NR{ a[$1]=$2;next } { print a[$1] }' id.txt  main.txt
it prints spaces

i want one help me to explain this thing plz