-1

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]}'
rKSH
  • 39
  • 9
  • What did you try? Because I think that should just work. – Etan Reisner Oct 20 '15 at 14:00
  • Update the post. Don't use a comment. And it looks like you just forget to quote your awk script argument. – Etan Reisner Oct 20 '15 at 14:08
  • What do you mean, that the contents of rst.awk are not guaranteed to be unchanging? (It's pretty easy to make files and their directories unwritable, thus "locking" file contents.) – Jeff Y Oct 20 '15 at 14:08

1 Answers1

4

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".

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    You're welcome and please consider editing your question to provide a useful, relevant Subject line so people with a similar problem searching the archives in future stand a chance of finding this Q/A. – Ed Morton Oct 20 '15 at 14:33
  • 1
    For stand-alone things, my strong preference is shebang, so that I have greater freedom to use whatever quotes I want within the script. Why the strong recommendation to use a quoted script rather than shebang? Perhaps if people are used to that method, they'll have less problems when they try to integrate awk into a larger shell script? – ghoti Oct 21 '15 at 19:12
  • You frequently need to separate variable assignments from files to run the awk command on, e.g. lets say you want write this in awk: `awk -v s=3 'BEGIN{print "s="s} NR>=s' file`.If we write the shell script as `awk -v s="$1" 'BEGIN{print "s="s} NR>=s' "$2"` then you can call the shell script as `script.sh 3 file`. You can't do that if you use a shebang since you can't separate the shell args into variable values and file names. You can call `script.sh s=3 file` but then your BEGIN fails and now you're in workaround hell for a trivial script with the extremely common need of setting a variable. – Ed Morton Oct 22 '15 at 10:43