$ cat read.sh
#!bin/bash
// how can I read the columnwise data to awk-script?
awk '{sum+=$1} END {print sum}' read
$ cat data
1
2
3
4
5
$ . ./read.sh <data
awk: cmd. line:1: fatal: cannot open file `read' for reading (No such file or directory)
views:
37answers:
2
+1
A:
Remove the filename
from the end of the awk
command:
Change
awk '{sum+=$1} END {print sum}' read
to
awk '{sum+=$1} END {print sum}'
The 1st one tell awk
to get the input from a file named read
where as the 2nd one tells awk
to get the input from standard input
.
The way you are running the script: ./read.sh <data
You are supplying the input through standard input.
Alternatively if you always want the script to read input from the file named data
, you can do:
awk '{sum+=$1} END {print sum}' data
and run the script as: ./read.sh
codaddict
2010-04-22 03:36:12
You can also use - as the filename for stdin instead of leaving it blank. I find that easier to read.
drawnonward
2010-04-22 03:55:37
A:
Also, your she-bang line is garbled; it should be #!/bin/bash
. But this should work instead:
#!/usr/bin/awk -f
{ sum += $1 }
END { print sum }
Mark Edgar
2010-04-22 10:30:57