awk命令在UB20.04中使用sudo bash执行时打印文件内容

发布于 2025-02-04 20:30:16 字数 472 浏览 3 评论 0原文

我有一个.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-02-11 20:30:16

awk命令不起作用(只是打印文件的内容),输出:

这是由于$ 2在双引号内的存在,该引号通过shell扩展并评估为空字符串和一个空字符串和然后,打印$ 2正成为print,这将显而易见。

sudo awk -F 'Depends:' 'NF > 1 {print $2}' demo.txt

或者更好地使用sed

sudo sed 's/^Depends: //' demo.txt

请注意,CAT完全不需要,因为AWK和SED都可以直接在文件上运行。

awk command is not working(it is just print the content of the file), output:

It is due to the presence of $2 inside the double quotes which is expanded by shell and evaluating to an empty string and then print $2 is becoming just print, which will obvious print full line.

sudo awk -F 'Depends:' 'NF > 1 {print $2}' demo.txt

Or better use sed:

sudo sed 's/^Depends: //' demo.txt

Note that cat is totally unnecessary as both awk and sed can directly operate on a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文