how to make syntax into a korn script??

ok i have this script called script.nawk:

BEGIN {print "List\n"}
BEGIN {print "AL"}
BEGIN {print "AM"}
/EAST/ {print $3, $2}
END {print "End of Report"}

How would I change this so that I don't have to type nawk -f script.nawk filename?

Instead just run it as a korn script?

thanks!

$ chmod +x script.nawk
$ script.nawk myInputFile

script.awk:

#!/bin/nawk -f

BEGIN {print "List\n"}
BEGIN {print "AL"}
BEGIN {print "AM"}
/EAST/ {print $3, $2}
END {print "End of Report"}

thank you for your reply.
What if i want to make it a .ksh file???

so far I have this:

#!/bin/ksh

echo "List"
echo "AL"
echo "AM"

echo "End of Report"

But I dont know how to put the /EAST/ {print $3, $2} statement to make it work.

Thanks!

why bother? use the applicable tools for the job:

$ chmod +x script.ksh
$ script.ksh myInputFile

script.ksh:

#!/bin/ksh

nawk ' 
   BEGIN {print "List\n"}
   BEGIN {print "AL"}
   BEGIN {print "AM"}
  /EAST/ {print $3, $2}
  END {print "End of Report"}' "${1}"

is the $1 the input file put at the command line after script name?

Also when do you know to use:

print
echo
or begin???

By the way it worked but it did not grab any data from my file???

thanks!

yes

depending on the context/tool that's being used

Either

  1. your data file is not what you think it's
  2. your awk script is wrong

Post a sample file and the desired result.

total user error. I apologize it does work!

Now I have 2 columns, how can I group by column 1 and then sort by column 2?

Quote:
Originally Posted by llsmr777 View Post
Also when do you know to use:

print
echo
or begin???

depending on the context/tool that's being used - when/if you have time I'd love an explanation or a good website maybe I can read about it?

look into 'man sort'

sort -k1,2 myFile

do i actually have to specify the file name in the script??? Can't I just add it like I did below? Gonna go read what -k option is....thanks!

#!/bin/ksh

nawk '
BEGIN {print "List\n"}
BEGIN {print "AL"}
BEGIN {print "AM"}
/EAST/ {print $3, $2}
sort -k1,2
END {print "End of Report"}' "${1}"

no, you're mixing shell with awk.

#!/bin/ksh

nawk ' 
   BEGIN {print "List\n"}
   BEGIN {print "AL"}
   BEGIN {print "AM"}
  /EAST/ {print $3, $2}
  END {print "End of Report"}' "${1}" | sort -k1,2

Pls use vB Codes when posting/quoting text/code.

ok that makes sense! take the output from my script and THEN Sort!! got it! Thank you!!!

ok so it worked but how do I exclude it not to sort the text I put in the begining of the script? Just my output from the file?
Basically not include this part in my sort:
BEGIN {print "List\n"}
BEGIN {print "AL"}
BEGIN {print "AM"}

huh? Exclude what exactly?
If you don't want to sort/group - just remove the 'pipe' to sort....

I apologize if I was not clear.

Ok so in my script I print out some text:

List
AM
AL

Then it grabs some data from a file. I want to sort that data but I dont want the printed text at the begining of the file "List AM AL" to be included in that sort. Only the data it grabed from the file.

Thank you!

one way....

nawk ' 
   BEGIN {print "List\n" "AL\n" "AM\n" | "cat 1>&2" }
  /EAST/ {print $3, $2}
  END {print "End of Report"}' "${1}" | sort -k1,2

what does this do?

"cat 1>&2" }

redirects output to 'stderr' (standard error)

well that sort of worked.
"End of Report" was still included in the sort.

Also how can I sort the second column by the last two charecters and then by the first one?

Thanks again for all your help!

nawk ' 
   BEGIN {
     stderr="cat 1>&2"
     print "List\n" "AL\n" "AM\n" | stderr
   }
  /EAST/ {print $3, $2}
  END {print "End of Report" | stderr }' "${1}" | sort -k1,2

I'll leave the rest for you to experiment with 'sort' - good luck!

Thank you again for the help! I will definately poke around.