如何有选择地 awk 字符串?

发布于 2024-08-05 08:32:59 字数 307 浏览 3 评论 0原文

如果文件中具有不确定宽度的文本行包含文本“Line-to-reorder”,并且我只想翻转并显示前三个标记的顺序,我可以这样做:

# cat file.txt | awk '/Line-to-reorder/ { print $3 $2 $1 }'

How can I let line of text that don't匹配标准是否未改变?

其次,如何在匹配的行上显示剩余的标记(该行的剩余部分)?

(awk 是首选工具,因为我的嵌入式系统的 busybox 实现就有它。)

If text lines of indeterminate width had in a file had text "Line-to-reorder", and I only wanted to flip and display the order of the first three tokens I can do:

# cat file.txt | awk '/Line-to-reorder/ { print $3 $2 $1 }'

How can I let lines of text that don't have the matching criteria pass through unaltered?

Secondly, How can I display the remainder of the tokens (the remainder of the line) on the matched line?

(awk is the tool of choice since my embedded system's busybox implementation has it.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

能怎样 2024-08-12 08:32:59

这个简单的脚本回答了您的两个问题:

awk '/Line-to-reorder/ {tmp = $1; $1 = $3; $3 = tmp} {print}' file.txt
  • 您可以分配字段来编辑行,
  • 不需要 cat
  • 打印每行(的所有字段)

This simple script answers both your questions:

awk '/Line-to-reorder/ {tmp = $1; $1 = $3; $3 = tmp} {print}' file.txt
  • you can assign to fields to edit the line
  • no need for cat
  • prints (all the fields of) every line
温柔少女心 2024-08-12 08:32:59

没有必要执行任何if...else,只需执行匹配/不匹配即可。

如果该行匹配,则将打印字段 3、2 和 1,然后按原始顺序打印其余字段。如果没有,它将按原样打印整行。

awk '/Line-to-reorder/ {printf "%s %s %s", $3, $2, $1; for (i=4; i<=NF; i++) {printf " %s", $i }; printf "\n"} !/Line-to-reorder/ {print}' file.txt

分解为 awk 脚本:

#!/usr/bin/awk -f
/Line-to-reorder/ {
        printf "%s %s %s", $3, $2, $1
        for (i=4; i<=NF; i++) {
                printf " %s", $i
        }
        printf "\n"
}
!/Line-to-reorder/ {print}

使用以下命令运行此脚本:

awkscript file.txt

awk 脚本将文件名作为参数(因为 awk 这样做),所以cat 对于这两种调用方法都不是必需的。

It's not necessary to do any if...else, just do a match/not-match.

This prints fields 3, 2, and 1 followed by the rest of the fields in their original order if the line matches. If it doesn't, it prints the whole line as-is.

awk '/Line-to-reorder/ {printf "%s %s %s", $3, $2, $1; for (i=4; i<=NF; i++) {printf " %s", $i }; printf "\n"} !/Line-to-reorder/ {print}' file.txt

Broken out into an awk script:

#!/usr/bin/awk -f
/Line-to-reorder/ {
        printf "%s %s %s", $3, $2, $1
        for (i=4; i<=NF; i++) {
                printf " %s", $i
        }
        printf "\n"
}
!/Line-to-reorder/ {print}

Run this with something like:

awkscript file.txt

This awk script takes a filename as an argument (because awk does) and so cat isn't necessary for either invocation method.

離人涙 2024-08-12 08:32:59

最好的选择可能是匹配所有行(无模式),然后在操作中执行 if ... else

类似的东西

{
if ($0 ~ /Line-to-reorder/)
    print $3 $2 $1
else
    print $0
}

best option is probably to match all lines (no pattern) and then do if ... else in the action.

something like

{
if ($0 ~ /Line-to-reorder/)
    print $3 $2 $1
else
    print $0
}
清风无影 2024-08-12 08:32:59

参考可能会有所帮助

This reference may help

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