AWK: how to read column file in 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)
+3
source share
2 answers

Remove filenamefrom the end of the command awk:

Edit

awk '{sum+=$1} END {print sum}' read

to

awk '{sum+=$1} END {print sum}' 

The first one will tell you awkto get the input file from a file with a name read, where the second is specified awkto get the input from standard input.

How you use the script: ./read.sh <data
You feed the input through standard input.

Alternatively, if you always want the script to read input from a file with a name data, you can do:

awk '{sum+=$1} END {print sum}' data

and run the script like: ./read.sh

+2
source

, - ; #!/bin/bash. :

#!/usr/bin/awk -f

{ sum += $1 }
END { print sum }
+1

Source: https://habr.com/ru/post/1742215/


All Articles