Bash
將 grep 結果折疊成一行,同時保留唯一值並刪除重複值?
我正在使用
zsh
但也對bash
解決方案持開放態度。假設我有以下命令:ls **/*/assessment(.) | xargs egrep "(^ \[\./.*\]$|^ input = .*$)"
這給了我一個類似於這樣的輸出:
<path-01-to>/assessment: [./input_file_01] <path-01-to>/assessment: input = 'input_file_01.i' <path-02-to>/assessment: [./input_file_02] <path-02-to>/assessment: input = 'input_file_02.i' <path-02-to>/assessment: [./input_file_02b] <path-02-to>/assessment: input = 'input_file_02b.i' <path-03-to>/assessment: [./input_file_03] <path-03-to>/assessment: input = 'input_file_03.i'
我想折疊線,使每對 (
[./title]
input
) 對都在同一行。因此,預期的輸出將如下所示:<path-01-to>/assessment: [./input_file_01] input = 'input_file_01.i' <path-02-to>/assessment: [./input_file_02] input = 'input_file_02.i' <path-02-to>/assessment: [./input_file_02b] input = 'input_file_02b.i' <path-03-to>/assessment: [./input_file_03] input = 'input_file_03.i'
我看過使用
tr -d "\n"
,但它將所有內容組合成一行。我在想awk
,或者不同的語言可能更適合這個。編輯:
這是輸入文件的範例
assessment
:路徑/01/to/assessment:
[Tests] [./input_file_01] type = RunApp input = 'input_file_01.i' [../] []
路徑/02/到/評估:
[Tests] [./input_file_02] type = RunApp input = 'input_file_02.i' cli_args = 'blah blah' [../] [./input_file_02b] type = CSVDiff input = 'input_file_02b.i' [../] []
這將在每個 UNIX 機器上的任何 shell 中使用任何 awk 工作:
$ awk '/^ *\[\.\//{title=$1} /^ *input =/{print FILENAME ":", title, "=", $NF}' */*/assessment 01/to/assessment: [./input_file_01] = 'input_file_01.i' 02/to/assessment: [./input_file_02] = 'input_file_02.i' 02/to/assessment: [./input_file_02b] = 'input_file_02b.i'