awk Error

Hi

I am trying to create a file with count of lines and string from shell variable, i am getting the error incorrect syntax.

Below is the code :

wc -l $filename | awk '{ print $1"|"$2"|"${SOURCETYPE} }'>>$AUDITFILENAME

Could someone help me on this?

wc -l "$filename" | awk -v S="$SOURCETYPE" '{ print $1"|"$2"|"S }' >> "$AUDITFILENAME"

A long shot in the dark:

wc -l "$filename" | 
  awk '{ print $1, $2, ENVIRON["SOURCETYPE"] }' OFS=\| >> "$AUDITFILENAME"

If the code above doesn't work as expected, please post the exact command and complete output/error that you're getting.

The special builtin array ENVIRON may not be available in your awk implementation.
It contains only exported variables, it that's not your case, use bipinajith's solution

This is in the spirit of what you tried. It should work:

wc -l $filename | awk '{ print $1"|"$2"|"'${SOURCETYPE}' }'>>$AUDITFILENAME

Thanks dexdex200,

It got executed but it hasn't create the file as expected, it didn't take any value from the shell variable. I ran teh script with sex-x, so below is the one executed on.
Actual Code: wc -l $filename | awk '{ print $1"|"$2"|"'$SOURCETYPE' }'>>$AUDITFILENAME

+ awk { print $1"|"$2"|"SOURCE }
+ wc -l INPUTFILE_20130131070015560.LOG
+ 1>> SOURCE_AUDIT_FILE.TXT

Output:

41|INPUTFILE_20130129073044447.LOG|
Actual Output: 41|INPUTFILE_20130129073044447.LOG|SOURCE

Thanks

This should do it then:

wc -l $filename | awk '{ print $1"|"$2"|'${SOURCETYPE}'" }'

Bipinajith's code is neater though

BTW you can get rid of wc -l and do that part in awk itself:

awk -v S="$SOURCETYPE" 'END{ print NR,FILENAME,S; }' OFS=\| "$filename" >> "$AUDITFILENAME"

Thanks a lot , it worked now.