我想从TAB-DELIMITED 文本文件的第二列中删除特定字符串“num=”。
this is a sentence num= 123.45
this is a phrase num= 768.90
我知道如何使用 sed 删除“num=”,但我似乎无法删除“=”之后的空格。我想要的是:
this is a sentence 123.45
this is a phrase 768.90
此外,如果第二列数字大于 500,我想在第三列中标记该行,如下所示:
this is a sentence 123.45 true
this is a phrase 768.90 false
我尝试了什么:
我使用 awk 将第二列放入它自己的文件中,然后是:
sed -e s/num=//g -i # Removes just "num="
sed -e s/num= //g -i # I get an error
sed -e s/num=\s//g -i # No effect
最佳答案
使用 awk:
$ awk '
BEGIN { FS=OFS="\t" } # set delimiters to tab
{
sub(/num= /,"",$2) # remove num=
print $0,($2+0>500?"true":"false") # output edited record and true/false
}' file
this is a sentence 123.45 false
this is a phrase 768.90 true
关于linux - 删除文本文件第二列中的字符串和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50795880/