【问题标题】:Match Column and Line in Shell Script在 Shell 脚本中匹配列和行
【发布时间】:2021-12-07 23:31:44
【问题描述】:

输入文件-test1

Failed ,abc,  /clients/FORD_1030PM_EST_Windows2008, Windows File System
Failed ,abc, /clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Completed, abc /clients/FORD_1030PM_EST_Windows2008, Windows File System
Failed ,def ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Failed ,def ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Failed ,def ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Failed ,ghi  ,/clients/FORD_1030PM_EST_Windows2008, Windows File System
Failed ,jkl ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Completed ,def ,/clients/FORD_1030PM_EST_Windows2008, Windows File System
Completed ,hkm ,/clients/FORD_1030PM_EST_Windows2008 Windows File System

预期输出

Failed ghi,  /clients/FORD_1030PM_EST_Windows2008, Windows File System
Failed jkl, /clients/FORD_1030PM_EST_Windows2008, Windows File System

代码

sed -n '/Completed/ s,\(.*\) .* Completed$,\1,a' "$pwd"/test1 | grep -v -f - "$pwd"/test1

我想获取只有失败值的列,或者它们在任何行中都没有完成。

【问题讨论】:

  • StackOverflow 不是一个“我们会为你做功课”的网站。展示你的努力,而不是要求一个现成的解决方案。显示您需要帮助的具体错误
  • sed -n '/Completed/s,(.) 。已完成$,\1,a' "$pwd"/test1 | grep -v -f - "$pwd"/test1 这是我已经完成的,它适用于早期的其他文件,但不适用于这个..
  • 编辑您的问题。不要在 cmets 中添加额外的说明......
  • 寻求代码帮助的问题必须包括在问题本身中重现它所需的最短代码,最好在Stack Snippet 中。请参阅如何创建 Minimal, Reproducible Example。在您的问题中显示预期结果并引用您遇到的任何(确切)错误也非常有帮助。您应该展示自己为解决这个问题所做的任何研究。

标签: bash shell multiple-columns


【解决方案1】:

如果awk 是您的选择,请尝试一下:

awk '
/^Failed/ {gsub(/,/, "", $2); fail[$2]=$0}         # if failed, store the line
/^Completed/ {gsub(/,/, "", $2); delete fail[$2]}  # if completed, abandon the line from the list
END {for (i in fail) print fail[i]}     # finally print the remaining list
' file

输出:

Failed jkl ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Failed ghi ,/clients/FORD_1030PM_EST_Windows2008, Windows File System

【讨论】:

  • 如果文件中的文本用逗号而不是空格分隔,则它不起作用。否则它工作正常。我尝试使用 awk -F, ' ' 但它仍然无法正常工作
  • 考虑到字段分隔符包含逗号的情况,我已经更新了脚本。顺便说一句,不建议在有人根据原始问题发布答案后更改您的问题。 BR。
  • 我的荣幸。干杯。
猜你喜欢
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 2017-06-06
  • 2014-07-14
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多