开发者

AWK: how to read columnwise file to AWK-script in Bash?

开发者 https://www.devze.com 2022-12-27 14:00 出处:网络
$ cat re开发者_JAVA百科ad.sh #!bin/bash // how can I read the columnwise data to awk-script? awk \'{sum+=$1} END {print sum}\' read
$ cat re开发者_JAVA百科ad.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)


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


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 }
0

精彩评论

暂无评论...
验证码 换一张
取 消