繁体   English   中英

如何在 bash 中使用 grep 命令时从 output 中删除选项卡?

[英]How to remove the tabs from the output while using grep command in bash?

所需的 Output:

[3] print(Hi, How are you?)
[5] print(I'm good, how are you?)

但是 Output 我得到了:

[3]     print(Hi, How are you?)
[5]     print(I'm good, how are you?)

我使用的命令:

grep -n -P "\t" $1 | awk --field-separator=":" '{print "["$1"]"$2}'

当您使用 awk 并使用:作为字段分隔符然后打印$2并且会截断诸如print(I'm good: I got paid)之类的行以print(I'm good ,所以不要那样做)时,您永远不需要 grep。

您没有提供示例输入,因此根据阅读您提供的代码来猜测您想要做什么,而这些代码并没有做您想做的事情,所以 YMMV 但这可能是您想要的:

awk 'sub(/\t/,""){print "["NR"]", $0}' "$1"

尝试:

grep -n -P "\t" $1 | awk --field-separator=":" '{$2=$2; print "["$1"]"$2}'

参考: https://unix.stackexchange.com/questions/102008/how-do-i-trim-leading-and-trailing-whitespace-from-each-line-of-some-output

如果您想用空格替换每个制表符 ( \t ) ( ) 然后考虑到您应用linux标记,您可以像这样使用tr命令

grep -n -P "\t" $1 | awk --field-separator=":" '{print "["$1"]"$2}' | tr '\t' ' '

暂无
暂无

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

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