简体   繁体   English

修剪Awk中的前导和尾随空格

[英]Trim Leading and trailing Spaces in Awk

I have a file which contains 1 line like below 我有一个包含1行的文件,如下所示

 VINOTH                                                                                    |KARTHICK                                                                                       |RAVI

I'm using the below command to remove the leading and trailing spaces , but it's not not working. 我正在使用以下命令删除前导空格和尾随空格,但是它不起作用。

awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }' Input_File

Please help. 请帮忙。

Required Output. 必需的输出。

VINOTH|KARTHICK|RAVI

You may use 您可以使用

sed 's/[ \t]*|[ \t]*/|/g;s/^[ \t]*\|[ \t]*$//g' Input_File

There are two regexps here: 这里有两个正则表达式:

  • s/[ \\t]*|[ \\t]*/|/g replaces all | s/[ \\t]*|[ \\t]*/|/g替换所有| enclosed with optional whitespaces with a single | 包含在可选的空格中,并带有一个| (the | in the regex matches a literal | char as per BRE POSIX standard) (在|在正则表达式字面匹配|焦炭用作每BRE POSIX标准)
  • s/^[ \\t]*\\|[ \\t]*$//g removes all whitespaces at the start and end of lines. s/^[ \\t]*\\|[ \\t]*$//g删除行首和结尾的所有空格。 Note that \\| 注意\\| here is an OR operator (escaped because the BRE POSIX syntax is used). 这是一个OR运算符(因为使用了BRE POSIX语法而退出了)。

See the online demo . 请参阅在线演示

您能否请尝试执行以下操作(由于您的示例输入和预期输出不清楚,因此未对其进行测试)。

awk '{gsub(/^[[:space:]]+|[[:space:]]+$/,"")} 1'  Input_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM