awk命令在UB20.04中使用sudo bash执行时打印文件内容
我有一个.txt文件,其中包含以下内容:
Depends: abc
Depends: def
Depends: ghi
使用ub20.04
我在执行以下命令时
cat demo.txt | awk -F'Depends:' 'NF > 1 {print $2}'
:我在运行时获得输出:
abc
def
ghi
但是:
sudo -s bash -c "cat demo.txt | awk -F'Depends:' 'NF > 1 {print $2}'"
awk命令不起作用(它只是打印文件的内容),输出:输出:
Depends: abc
Depends: def
Depends: ghi
I have a .txt file containing following:
Depends: abc
Depends: def
Depends: ghi
I am using ub20.04
While executing following command:
cat demo.txt | awk -F'Depends:' 'NF > 1 {print $2}'
I get output as follows:
abc
def
ghi
But while running:
sudo -s bash -c "cat demo.txt | awk -F'Depends:' 'NF > 1 {print $2}'"
awk command is not working(it is just print the content of the file), output:
Depends: abc
Depends: def
Depends: ghi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于
$ 2
在双引号内的存在,该引号通过shell扩展并评估为空字符串和一个空字符串和然后,打印$ 2
正成为print
,这将显而易见。或者更好地使用
sed
:请注意,
CAT
完全不需要,因为AWK和SED都可以直接在文件上运行。It is due to the presence of
$2
inside the double quotes which is expanded by shell and evaluating to an empty string and thenprint $2
is becoming justprint
, which will obvious print full line.Or better use
sed
:Note that
cat
is totally unnecessary as both awk and sed can directly operate on a file.