Calling Awk in a shell script

后端 未结 4 1749
遥遥无期
遥遥无期 2021-01-18 19:32

I have this command which executes correctly if run directly on the terminal.

awk \'/word/ {print NR}\' file.txt | head -n 1

The purpose i

4条回答
  •  無奈伤痛
    2021-01-18 19:36

    You should use AWK's variable passing feature:

    awk -v patt="$1" '$0 ~ patt {print NR; exit}' "$2"
    

    The exit makes the head -1 unnecessary.

提交回复
热议问题