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

Mar*_*jer 45 unix shell awk

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

Name, Order  
Trim, working
cat,cat1

我已使用下面的内容awk删除第二列中的前导和尾随空格但它不起作用.我错过了什么?

awk -F, '{$2=$2};1' input.txt
Run Code Online (Sandbox Code Playgroud)

这使输出为:

Name, Order  
Trim, working
cat,cat1

不删除前导和尾随空格.

Flo*_*ris 68

如果你想修剪所有空格,只在带有逗号的行中使用awk,那么以下内容对你有用:

awk -F, '/,/{gsub(/ /, "", $0); print} ' input.txt
Run Code Online (Sandbox Code Playgroud)

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

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

请注意,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
Run Code Online (Sandbox Code Playgroud)

编辑我想指出@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
Run Code Online (Sandbox Code Playgroud)

  • 查看@ EdMorton对单个`gsub`解决方案的回答.它还使用空间的字符类,这是一个更好的事情. (3认同)
  • 这不是正确的答案,因为它删除了所有空格,而不仅仅是前导和尾随。 (2认同)

BMW*_*BMW 29

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

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

一个gsub的另一种方式:

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

  • 随意写您对自己所做的事情的解释(或抄袭我的观点)来改善您的答案。我认为您的答案应该被接受-但是“完成”后总会更好。 (2认同)

hek*_*mgl 17

我会用sed:

sed 's/, /,/' input.txt
Run Code Online (Sandbox Code Playgroud)

这将删除后的领先空间,.输出:

Name,Order
Trim,working
cat,cat1
Run Code Online (Sandbox Code Playgroud)

更一般的可能如下,它将删除可能的多个空格和/或标签后,:

sed 's/,[ \t]\?/,/g' input.txt
Run Code Online (Sandbox Code Playgroud)

由于全局修饰符,它还可以使用两列以上 /g


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

sed 's/[ \t]\?,[ \t]\?/,/g; s/^[ \t]\+//g; s/[ \t]\+$//g'
Run Code Online (Sandbox Code Playgroud)

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

awk -F', ' '{printf "%s,%s\n", $1, $2}' input.txt
Run Code Online (Sandbox Code Playgroud)

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

cat input.txt | tr -d ' '
Run Code Online (Sandbox Code Playgroud)


Ed *_*ton 14

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

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


Ily*_*mov 5

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

', *' - 用于前导空格

' *,' - 用于尾随空格

对于前导和尾随:

awk -F' *,? *' '{print $1","$2}' input.txt
Run Code Online (Sandbox Code Playgroud)