我正在使用,但也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'
我看过 usingtr -d "\n"
但它将所有内容合并到一行中。我在想awk
或者不同的语言可能更适合这个。
编辑:
这是输入文件的示例assessment
:
路径/01/到/评估:
[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'
[../]
[]
答案1
这将在每个 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'