簡體   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