Awk:删除行中最后 space 之后的文本
Awk: Remove text after last space in row
我有一个制表符分隔的文本文件,格式如下。
Col1 | Col2 | Col3
123.0 | 534.2 | Blah0 2031/23/12
23.00 | 786.2 | Blah1 2033/01/01
12.40 | 343.0 | Blah2 2031/27/11
我需要删除最后一列 space 之后的所有字符。所以我的输出是
Col1 | Col2 | Col3
123.0 | 534.2 | Blah0
23.00 | 786.2 | Blah1
12.40 | 343.0 | Blah2
我应该如何使用 Awk
或类似的东西来解决这个问题?
使用 awk:
awk -F '\t' 'BEGIN { OFS = FS } NR != 1 { sub(/ [^ ]*$/, "", $NF) } 1' filename
即:
BEGIN { OFS = FS } # the output should be separated the same way as
# the input
NR != 1 { # in all lines except the header line:
sub(/ [^ ]*$/, "", $NF) # replace the last space and everything after it
} # in the last field ($NF) with the empty string
# (i.e., remove it)
1 # in all lines: print.
如果最后一个字段中可以有多个 space,而您想删除第一个 space 之后的所有内容,请改用 sub(/ .*/, "", $NF)
。在这种情况下应该发生什么的问题中并不完全清楚。
我有一个制表符分隔的文本文件,格式如下。
Col1 | Col2 | Col3
123.0 | 534.2 | Blah0 2031/23/12
23.00 | 786.2 | Blah1 2033/01/01
12.40 | 343.0 | Blah2 2031/27/11
我需要删除最后一列 space 之后的所有字符。所以我的输出是
Col1 | Col2 | Col3
123.0 | 534.2 | Blah0
23.00 | 786.2 | Blah1
12.40 | 343.0 | Blah2
我应该如何使用 Awk
或类似的东西来解决这个问题?
使用 awk:
awk -F '\t' 'BEGIN { OFS = FS } NR != 1 { sub(/ [^ ]*$/, "", $NF) } 1' filename
即:
BEGIN { OFS = FS } # the output should be separated the same way as
# the input
NR != 1 { # in all lines except the header line:
sub(/ [^ ]*$/, "", $NF) # replace the last space and everything after it
} # in the last field ($NF) with the empty string
# (i.e., remove it)
1 # in all lines: print.
如果最后一个字段中可以有多个 space,而您想删除第一个 space 之后的所有内容,请改用 sub(/ .*/, "", $NF)
。在这种情况下应该发生什么的问题中并不完全清楚。