简体   繁体   中英

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

$ 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)

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 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM