Вы можете использовать утилиту "dc" для математической работы:
dc -e "
[zsmlksn]sb
[lk1+skzls+ss]sa
[[Max = ]nlmn[(]nlnn[)]n10an[Avg = ]n5klslk/1/n10an]sp
[lpxq]sq
[?z0=qlaxzlm<bcl?x]s?
0ddddsksmsnssd=?
"
Что было показано ниже для работы вышеизложенного
tr '\t-' ' _' data.in | # dc wants negative numbers to begin with underscores
dc -e "
[
z sm # store current num of cols in register "m"
lk sn # store row num in register "n"
]sb
[
lk 1 + sk # increment the number of rows
z ls + ss # add num of cols to running sum of cols
]sa
[
[Max=]n
lmn # put max number of cols on ToS & print it
[(]n
lnn # put row num at which max number of cols are present on ToS & print it
[)]n
10an
[Avg=]n
5k ls lk /1/n # accuracy of 5 digits, compute average = sum of cols / total num of cols
10an
]sp
[
lpx # print the results by invoking the macro "p"
q # quit
]sq
# while loop for reading in lines
[
? z 0 =q # quit when no columns found in the current line read in
lax # macro "a" does: rows++, sum+=cols
z lm <b # update max cols value stored in register "lm" when cols > lm
c # clear out the line and read in the next line
l?x # read the next line
]s?
# initializations+set the ball rolling:
# register "sk" -> line kount
# register "sm" -> max cols
# register "sn" -> row number corresp. to max cols
# register "ss" -> sum of cols
0
d d d d
sk sm sn ss
d=?
"
Max = 9(6)
Avg = 3.33333
Программа awk
будет ожидать ввода и для каждой строки ввода будет печатать слово foo
. Это то, что делает программа awk
{ print "foo" }
.
Напротив, echo
в первой подстановке команд не ожидает ввода.
Хотели бы вы, чтобы программа awk
просто печатала что-то без каких-либо входных данных, выполняя вывод в блоке BEGIN
:
awk 'BEGIN { print "foo" }'
Блок BEGIN
выполняется перед чтением первой строки ввода, и, поскольку в сценарии нет других блоков и нет входного файла, он завершает работу.
Кроме того, никогда не пишите код типа echo $(... )
или echo `... `
, вместо этого используйте код внутри подстановки команд.