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

时间:2011-03-22 20:30:21

标签: awk

假设我有以下文件。

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中执行此操作?

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

可以将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.