修复awk中字符串的前导和尾随空格

时间:2013-12-15 22:59:08

标签: shell unix awk

我正在尝试删除以下input.txt第二列中的前导和尾随空格:

Name, Order  
Trim, working
cat,cat1

我已使用下面的awk删除第二列中的前导和尾随空格,但它无效。我错过了什么?

awk -F, '{$2=$2};1' input.txt

这将输出为:

Name, Order  
Trim, working
cat,cat1

不删除前导和尾随空格。

8 个答案:

答案 0 :(得分:60)

如果您想修剪所有空格,只能在包含逗号的行中修剪,并使用awk,则以下内容适用于您:

awk -F, '/,/{gsub(/ /, "", $0); print} ' input.txt

如果您只想删除第二列中的空格,请将表达式更改为

awk -F, '/,/{gsub(/ /, "", $2); print$1","$2} ' input.txt

请注意gsub//中的字符替换为第二个表达式,在第三个参数的变量中 - 并且in-place - 换句话说,当它完成时, $0(或$2)已被修改。

完整的解释:

-F,            use comma as field separator 
               (so the thing before the first comma is $1, etc)
/,/            operate only on lines with a comma 
               (this means empty lines are skipped)
gsub(a,b,c)    match the regular expression a, replace it with b, 
               and do all this with the contents of c
print$1","$2   print the contents of field 1, a comma, then field 2
input.txt      use input.txt as the source of lines to process

编辑我想指出@Bob的解决方案更好,因为它实际上只用两个连续gsub命令修剪前导空格和尾随空格。在给予赞扬的同时,我会解释它是如何运作的。

gsub(/^[ \t]+/,"",$2);    - starting at the beginning (^) replace all (+ = zero or more, greedy)
                             consecutive tabs and spaces with an empty string
gsub(/[ \t]+$/,"",$2)}    - do the same, but now for all space up to the end of string ($)
1                         - ="true". Shorthand for "use default action", which is print $0
                          - that is, print the entire (modified) line

答案 1 :(得分:28)

删除第二列中的前导和尾随空格

awk 'BEGIN{FS=OFS=","}{gsub(/^[ \t]+/,"",$2);gsub(/[ \t]+$/,"",$2)}1' input.txt

另一种方式是一个gsub:

awk 'BEGIN{FS=OFS=","} {gsub(/^[ \t]+|[ \t]+$/, "", $2)}1' infile

答案 2 :(得分:17)

我会使用sed

sed 's/, /,/' input.txt

这将删除,之后的前导空格。 输出:

Name,Order
Trim,working
cat,cat1

更一般可能是以下内容,它会在,之后删除可能的多个空格和/或标签:

sed 's/,[ \t]\?/,/g' input.txt

由于全局修饰符/g

,它也可以使用两列以上
@Floris在讨论中询问了一个解决方案,该解决方案可以删除每个列中的尾随和结尾空格(即使是第一个和最后一个),同时不删除列中间的空格:

sed 's/[ \t]\?,[ \t]\?/,/g; s/^[ \t]\+//g; s/[ \t]\+$//g'

IMO sed是这项工作的最佳工具。但是,这里有awk的解决方案,因为您已经要求:

awk -F', ' '{printf "%s,%s\n", $1, $2}' input.txt

考虑删除所有空格的另一个简单解决方案是tr -d

cat input.txt | tr -d ' '

答案 3 :(得分:12)

我刚刚遇到过这个。正确答案是:

awk 'BEGIN{FS=OFS=","} {gsub(/^[[:space:]]+|[[:space:]]+$/,"",$2)} 1'

答案 4 :(得分:2)

以下似乎有效:

awk -F',[[:blank:]]*' '{$2=$2}1' OFS="," input.txt

答案 5 :(得分:2)

只使用正则表达式作为分隔符:

',*' - 领先的空间

' *,' - 用于尾随空格

用于前导和尾随:

awk -F' *, *' '{print $1","$2}' input.txt

答案 6 :(得分:0)

最简单的解决方案可能是使用tr

$ cat -A input
^I    Name, ^IOrder  $
  Trim, working  $
cat,cat1^I  

$ tr -d '[:blank:]' < input | cat -A
Name,Order$
Trim,working$
cat,cat1

答案 7 :(得分:0)

如果在第二列中只假设一组空格是安全的(这是原始示例):

awk '{print $1$2}' /tmp/input.txt

添加其他字段,例如awk '{print $1$2$3}' /tmp/input.txt将捕获两组空格(第二列中最多三个单词),如果数量较少,则不会中断。

如果您有一个不确定(大)空格分隔的单词,我会使用之前的建议之一,否则此解决方案是您使用awk找到的最简单的方法。