我如何告诉 awk 使用 = 作为分隔符(也删除了空格)

发布于 2024-10-25 14:58:34 字数 461 浏览 2 评论 0原文

假设我有以下文件。

John=good
Tom   = ok
Tim =    excellent

我知道下面让我使用 = 作为分隔符。

awk -F= '{print $1,$2}' file

这给了我以下结果。

John good
Tom     ok
Tim      excellent

我希望忽略空格,以便只打印出名称和他们的表演。

解决此问题的一种方法是对结果运行另一个 awk

awk -F= '{print$1,$2}' file | awk '{print $1,$2}'

但我想知道是否可以在一个 awk 中完成此操作?

Suppose I have the following file.

John=good
Tom   = ok
Tim =    excellent

I know the following let's me use = as a separator.

awk -F= '{print $1,$2}' file

This gives me the following results.

John good
Tom     ok
Tim      excellent

I would like the white spaces to be ignored, so that only the names and their performances are printed out.

One way to get around this is run another awk on the results.

awk -F= '{print$1,$2}' file | awk '{print $1,$2}'

But I wanted to know if I could do this in one awk?

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

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

发布评论

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

评论(2

北渚 2024-11-01 14:58:34

将它们包含在分隔符定义中;这是一个正则表达式。

jinx:1654 Z$ awk -F' *= *' '{print $1, $2}' foo.ds
John good
Tom ok
Tim excellent

Include them in the separator definition; it's a regexp.

jinx:1654 Z$ awk -F' *= *' '{print $1, $2}' foo.ds
John good
Tom ok
Tim excellent
多情出卖 2024-11-01 14:58:34

FS 变量可以设置为正则表达式。

来自 AWK 手册

The following table summarizes how fields are split, based on the value of FS.

FS == " "
    Fields are separated by runs of whitespace. Leading and trailing whitespace are ignored. This is the default. 
FS == any single character
    Fields are separated by each occurrence of the character. Multiple successive occurrences delimit empty fields, as do leading and trailing occurrences. 
FS == regexp
    Fields are separated by occurrences of characters that match regexp. Leading and trailing matches of regexp delimit empty fields. 

The FS variable can be set to a regular expression.

From the AWK manual

The following table summarizes how fields are split, based on the value of FS.

FS == " "
    Fields are separated by runs of whitespace. Leading and trailing whitespace are ignored. This is the default. 
FS == any single character
    Fields are separated by each occurrence of the character. Multiple successive occurrences delimit empty fields, as do leading and trailing occurrences. 
FS == regexp
    Fields are separated by occurrences of characters that match regexp. Leading and trailing matches of regexp delimit empty fields. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文