我试图从以下input.txt
的第二列中删除前导和尾随空格:
Name,Order
Trim,working
cat,cat1
我已经使用了下面的awk
来删除第二列中的前导和尾随空格,但它没有起作用。我错过了什么吗?
awk -F, '{$2=$2};1' input.txt
这将产生以下输出:
名称,订单
修剪,工作
猫,猫1
前导和尾随空格未被移除。
awk
,则以下方法适用于您:awk -F, '/,/{gsub(/ /, "", $0); print} ' input.txt
awk -F, '/,/{gsub(/ /, "", $2); print$1","$2} ' input.txt
gsub
将第二个表达式中的字符替换为第一个表达式中的字符,并在作为第三个参数的变量中进行原地修改
。换句话说,在完成操作后,$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
编辑 我想指出@BMW的解决方案更好,因为它实际上只使用两个连续的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
删除第二列中的前导和尾随空格
awk 'BEGIN{FS=OFS=","}{gsub(/^[ \t]+/,"",$2);gsub(/[ \t]+$/,"",$2)}1' input.txt
另一种方法是使用一个gsub:
awk 'BEGIN{FS=OFS=","} {gsub(/^[ \t]+|[ \t]+$/, "", $2)}1' infile
gsub
命令,通过使用 ^
和 $
锚点仅删除前导和尾随空格的方式。更复杂,但非常准确。 - Florisawk '$1=$1' infile > outfile
- BMWawk '$1=$1'
将在 $1
包含 0
时失败,所以不要这样做 - 如果你想从 CSV 中的所有字段中去除空格,应该使用 awk -F'[[:space:]]*,[[:space:]]*' -v OFS=',' '{$1=$1; gsub(/^[[:space:]]+|[[:space:]]+$/,"")} 1'
。 - Ed Morton我刚刚看到了这个。正确答案是:
awk 'BEGIN{FS=OFS=","} {gsub(/^[[:space:]]+|[[:space:]]+$/,"",$2)} 1'
警告:@Geoff指出,仅有本答案中的一个建议有效(但适用于两个列)。
我会使用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' input.txt
在我看来,sed
是这个任务的最佳工具。但是,这里提供了一个使用awk
的解决方案,因为你要求用它:
awk -F', ' '{printf "%s,%s\n", $1, $2}' input.txt
另一个简单的解决方案是使用 tr -d
命令来删除所有空格:
cat input.txt | tr -d ' '
sed
比 awk
更适合这个任务,因为它是一个文本编辑任务,但是 awk
脚本应该可以工作。我已经测试过了。有什么问题吗? - hek2mglsed
命令似乎也无法移除尾随空格。只需在末尾加上 > output.txt
,然后在文本编辑器中打开结果,你就会看到... - Floris只需使用正则表达式作为分隔符:
', *' - 用于前导空格
' *,' - 用于尾随空格
同时用于前导和尾随:
awk -F' *,? *' '{print $1","$2}' input.txt
最简单的解决方案可能是使用tr
$ cat -A input
^I Name, ^IOrder $
Trim, working $
cat,cat1^I
$ tr -d '[:blank:]' < input | cat -A
Name,Order$
Trim,working$
cat,cat1
awk -F',[[:blank:]]*' '{$2=$2}1' OFS="," input.txt
如果可以安全地假设第二列只有一个空格集(这是原始示例):
awk '{print $1$2}' /tmp/input.txt
添加另一个字段,例如awk '{print $1$2$3}' /tmp/input.txt
将捕获两个空格集(最多三个单词在第二列),并且如果少于两个也不会出错。
如果您有一个不确定(大量)的空格分隔单词,则我建议使用先前的建议之一,否则使用awk找到的这个解决方案是最简单的。
gsub
解决方案。它还使用空格字符类,这是更好的做法。 - codeforester