开发者

How do I tell awk to use = as a separator (with white spaces removed too)

开发者 https://www.devze.com 2023-02-18 06:54 出处:网络
Suppose I have the following file. John=good Tom= ok Tim =excellent I know the following let\'s me use = as a separator.

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开发者_Python百科}' file | awk '{print $1,$2}'

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


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


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. 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号