currently I am having a script in which I am calling AWK like
/usr/bin/nawk -f rst.awk file1 file2
and rst.awk looks like
cat rst.awk
{ split($2,a,/\./); curr = a[1]*10000 + a[2]*100 + a[3] }
NR==FNR { prev[$1] = curr; next }
($1 in prev) && (curr > prev[$1])
in this case I need to make sure to check rst.awk is having above content every time to run script.
Can we write rst.awk content in script itself? tried by myself but no luck.
cat te.sh
/usr/bin/nawk -f
{ split($2,a,/\./); curr = a[1]*10000 + a[2]*100 + a[3] }
NR==FNR { prev[$1] = curr; next }
($1 in prev) && (curr > prev[$1])
file1 file2
./te.sh
/usr/bin/nawk: no program filename
./te.sh: line 2: syntax error near unexpected token `$2,a,/\./'
./te.sh: line 2: `{ split($2,a,/\./); curr = a[1]*10000 + a[2]*100 + a[3]}'
You need te.sh to contain either of these:
#!/usr/bin/nawk -f
{ split($2,a,/\./); curr = a[1]*10000 + a[2]*100 + a[3] }
NR==FNR { prev[$1] = curr; next }
($1 in prev) && (curr > prev[$1])
or
/usr/bin/nawk '
{ split($2,a,/\./); curr = a[1]*10000 + a[2]*100 + a[3] }
NR==FNR { prev[$1] = curr; next }
($1 in prev) && (curr > prev[$1])
' "$@"
I strongly recommend the latter. Either way you'd then execute it as te.sh file1 file2
.
btw "Need help on Awk" is just about the worst subject line you could choose, not much better than "dinosaurs smell funny" or similarly useless for someone in future looking for an answer to a similar problem. Choose subject lines that capture your problem, e.g. "Executing an awk script from a shell script produces syntax errors".