在 Shell 腳本中匹配列和行

古魯·辛格

輸入文件-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

我想獲取只有失敗值的列,或者它們在任何行中都沒有完成。

喬諾

如果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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Shell腳本從TXT讀取行並分配給env變量?

在 Jenkins 管道代碼中通過 shell 腳本運行時找不到 SQL 命令

如何僅從 Shell 腳本中的變量返回整數並丟棄字母和前導零?

當在子shell腳本中運行時,scp沒有列出

shell 腳本中的標題列驗證

為什麼使用 Applescript 庫會阻止我運行 shell 腳本?

Power Shell 簡單腳本

在shell腳本中將4位數年份轉換為2位數

循環變量不會傳遞給 Jenkins 中的 shell 腳本

如何在shell腳本中組合exec命令

Shell 腳本應該等到 kubernetes pod 運行

Shell腳本一次將一個大文件中的行分成兩個單獨的文件?

Shell/Bash - 腳本和函數衝突的位置參數

如何通過讀取 shell 腳本中的 csv 文件將 2 列的總和添加到新列中

正則表達式匹配 shell 腳本中數組中的數字

想在 shell 腳本的文本文件中添加標題

如何使用shell腳本在csv文件的列開頭插入值

如何在執行之前查找和替換 build.gradle 中的行,使用 shell 腳本

如何在spark中將雙大括號從shell腳本傳遞到python腳本?

Jenkins 和 Shell 腳本 - 無法更改日期時間值

當腳本位於 shell 的 stdin 上時,apt-get 之後的 shell 腳本中的命令未正確執行

如何使用shell腳本解壓縮目錄中的所有文件?

Ansible 調用執行帶有特定標籤的 shell 腳本

在 bash/shell 腳本中測試“null”不起作用

如何在 shell 腳本中運行 sacct 命令

我需要在腳本執行結束時更新 shell 腳本中的源配置文件

我在將 bash 命令轉換為 shell 腳本中的 shell 腳本語法錯誤時遇到錯誤

如何在一行中抽像兩個或多個子字符串在shell腳本中

創建一個執行多個 grep 操作(AND 操作)的 shell 腳本