在shell脚本调用awk中 [英] Calling Awk in a shell script

查看:143
本文介绍了在shell脚本调用awk中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的命令,如果直接在终端上运行的正确执行。

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

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

目的是找到行的行号上的字'字'第一次出现在 file.txt的

但是,当我把它放在一个脚本文件,它好好尝试似乎工作。

But when I put it in a script file, it doens't seem to work.

#! /bin/sh

if [ $# -ne 2 ]
then
        echo "Usage: $0 <word> <filename>"
        exit 1
fi

awk '/$1/ {print NR}' $2 | head -n 1

那么,我做错?

谢谢,

推荐答案

替换单引号用双引号,使 $ 1 是由shell评价:

Replace the single quotes with double quotes so that the $1 is evaluated by the shell:

awk "/$1/ {print NR}" $2 | head -n 1

这篇关于在shell脚本调用awk中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆