简体   繁体   English

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

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

Desired Output:所需的 Output:

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

But Output I'm getting:但是 Output 我得到了:

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

Command Im using:我使用的命令:

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

You never need grep when you're using awk and using : as the field separator then printing $2 and would truncate lines like print(I'm good: I got paid) to print(I'm good so don't do that.当您使用 awk 并使用:作为字段分隔符然后打印$2并且会截断诸如print(I'm good: I got paid)之类的行以print(I'm good ,所以不要那样做)时,您永远不需要 grep。

You didn't provide sample input so it's a guess at what you want to do based on reading the code you provided that doesn't do what you want to do so YMMV but this may be what you want:您没有提供示例输入,因此根据阅读您提供的代码来猜测您想要做什么,而这些代码并没有做您想做的事情,所以 YMMV 但这可能是您想要的:

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

If you want to replace every tab ( \t ) with space (如果您想用空格替换每个制表符 ( \t ) ( ) then considering that you applied linux tag, you might use tr command like so ) 然后考虑到您应用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