从 awk 中的字符串修剪前导和尾随空格 [英] Trim leading and trailing spaces from a string in awk

查看:34
本文介绍了从 awk 中的字符串修剪前导和尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to remove leading and trailing space in 2nd column of the below input.txt:

名称、订单
修剪,工作
cat,cat1

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

I have used the below awk to remove leading and trailing space in 2nd column but it is not working. What am I missing?

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

输出如下:

名称、订单
修剪,工作
cat,cat1

前导和尾随空格不会被删除.

Leading and trailing spaces are not removed.

推荐答案

如果你想修剪所有空格,只在有逗号的行中,并使用 awk,那么以下将适用你:

If you want to trim all spaces, only in lines that have a comma, and use awk, then the following will work for you:

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

如果只想去掉第二列的空格,把表达式改成

If you only want to remove spaces in the second column, change the expression to

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

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

Note that gsub substitutes the character in // with the second expression, in the variable that is the third parameter - and does so in-place - in other words, when it's done, the $0 (or $2) has been modified.

完整说明:

-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

EDIT 我想指出@BMW 的解决方案更好,因为它实际上只使用两个连续的 gsub 命令修剪前导和尾随空格.在给予信任的同时,我将解释它的工作原理.

EDIT I want to point out that @BMW's solution is better, as it actually trims only leading and trailing spaces with two successive gsub commands. Whilst giving credit I will give an explanation of how it works.

gsub(/^[ 	]+/,"",$2);    - starting at the beginning (^) replace all (+ = zero or more, greedy)
                             consecutive tabs and spaces with an empty string
gsub(/[ 	]+$/,"",$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 中的字符串修剪前导和尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆