PDA

View Full Version : [SOLVED] awk outputs the input ... that's odd.



bruce.staples
May 13th, 2011, 12:57 AM
This may be an *obvious* problem, but I just can't see it!

I have an awk script to extract fields, and output a csv file, but now I'm not able to use it!

I have an input of the following format:


START:1
FIELD_1:content11
FIELD_2:content12
FIELD_3:content13
END
START:2
FIELD_1:content21
#FIELD_2:content22
FIELD_3:content23
END
START:3
FIELD_1:content31
FIELD_2:content32
FIELD_3:content33
END

The awk script is:


!/bin/awk
BEGIN {
FS =":"
print "Fieldname1,Fieldname2,fieldname3"
}
/^FIELD_1/ {
field1 = $2
}
/^FIELD_2/ {
field2 = $2
}
/^FIELD_3/ {
field3 = $2
}
/^END/ {
print field1 "," field2 "," field3
# messy clean up for each record
field1 = ""
field2 = ""
field3 = ""
}
#[eof]


So invoking it with

$ awk -f e.awk input.txt > output.txt

I expected



Fieldname1,Fieldname2,fieldname3
content11,content12,content13
content21,,content23
content31,content32,content33

but I got:


Fieldname1,Fieldname2,fieldname3
START:1
FIELD_1:content11
FIELD_2:content12
FIELD_3:content13
END
content11,content12,content13
START:2
FIELD_1:content21
#FIELD_2:content22
FIELD_3:content23
END
content21,,content23
START:3
FIELD_1:content31
FIELD_2:content32
FIELD_3:content33
END
content31,content32,content33


... which is apparently echoing the input to the output ...
This happens with

awk -f e.awk input.txt
as well.
Any suggestions (please)?

erind
May 13th, 2011, 03:10 AM
Remove this line from the beginning of the awk script:


!/bin/awkYou're already using awk outside, to call the script: awk -f e.awk.

cgroza
May 13th, 2011, 02:59 PM
Remove this line from the beginning of the awk script:


!/bin/awkYou're already using awk outside, to call the script: awk -f e.awk.
I think that line should have a "#" in front. So it would become:

#!/bin/awk

matt_symes
May 13th, 2011, 03:11 PM
Hi

There are many things wrong with the script the least of which is


matthew@matthew-laptop:~$ which awk
/usr/bin/awk
matthew@matthew-laptop:~$

Is this homework ?

Kind regards

bruce.staples
May 15th, 2011, 07:10 AM
*BINGO!*
Yes the missing hash in the first line was it!
Thanks cgroza.
And Matt, yes, I seem to have mislaid awk, but as I had invoked it from the command line, the bad location was ignored (once the hash was inserted).

Thanks all ...

erind
May 15th, 2011, 08:53 PM
Glad you got it working. Just to clarify it again: The way you're calling the script "awk -f e.awk input.txt" , the line "!/bin/awk" is wrong where it is, because awk will interpret it as a valid pattern to be evaluated, and not as awk interpreter, which you probably thought it would do. As I mentioned before, it's awk outside of the script that takes care of it: awk -f e.awk ...
With the hash before it - "#!/bin/awk", it'd be interpreted as only a simple comment, nothing else - as you noticed it was ignored.
If you still want to have the script the way you wrote it before ( with !/bin/awk ), then precede that line with # as suggested and add -f at that line's end, make the file e.awk executable and run it as a stand-alone script, like so:


$./e.awk input.txt > output.txt

$ type awk
awk is /usr/bin/awk

$ cat e.awk
#!/usr/bin/awk -f

BEGIN {
FS =":"
...
# more code here ...
}You can read more on how to run awk at: http://www.gnu.org/software/gawk/manual/gawk.html#Long

Hope it helps.

matt_symes
May 15th, 2011, 10:28 PM
Hi


You can read more on how to run awk at: http://www.gnu.org/software/gawk/manual/gawk.html#LongAnd if you are wondering why it's gawk. It's because it's linked to though your alternatives (assuming you have not changed where it points to).


matthew@matthew-laptop:~$ which awk
/usr/bin/awk
matthew@matthew-laptop:~$ ls -l /usr/bin/awk
lrwxrwxrwx 1 root root 21 2011-05-08 22:35 /usr/bin/awk -> /etc/alternatives/awk
matthew@matthew-laptop:~$ ls -l /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 2011-05-08 22:35 /etc/alternatives/awk -> /usr/bin/gawk
matthew@matthew-laptop:~$ readlink -f /usr/bin/awk
/usr/bin/gawk
matthew@matthew-laptop:~$ The symbolic link /usr/bin/awk points to /usr/bin/gawk via the alternatives symbolic link in /etc/alternatives/awk.

Gawk is the default implementation of awk in Ubuntu.

Kind regards