linux - 删除文本文件第二列中的字符串和空格

标签 linux bash awk sed

我想从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/

相关文章:

linux - 在 bash 和 php 之间共享数据

linux - unix - awk - 在用户定义的函数中打印参数名称

.net - 如何在 dotnet 核心中运行 F# interactive (fsi.exe)?现在还支持吗?

linux - 检查脚本中的差异退出状态

bash - 如何重新授权 OAuth 应用程序 'Git Credential Manager'

awk - 在表格数据上使用带有 shell 模块的 awk 条件语句在 Ansible 中没有按预期工作

bash - Awk:停止 'print' 添加换行符以及如何循环/自动化

Linux 根据目录中的另一个文件重命名文件?

linux - 如何理解Linux内核模块的 "Building Separate Files"?

php - 记录从 start-stop-daemon 调用的 php 脚本的错误输出