I'm using zsh but open to a bash solution as well. Suppose I have the following command:

ls **/*/assessment(.) | xargs egrep "(^  \[\./.*\]$|^    input = .*$)"

Which gives me an output that resembles this:

<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'

I would like to collapse lines so each ([./title] input) pair are on the same line. Thus the expected output would look like this:

<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'

I've looked at using tr -d "\n" but it combines everything into one line. I'm thinking awk or a different language might be better suited for this.


Here is an example of an input file assessment:


    type = RunApp
    input = 'input_file_01.i'


    type = RunApp
    input = 'input_file_02.i'
    cli_args = 'blah blah'
    type = CSVDiff
    input = 'input_file_02b.i'
  • 1
    @EdMorton Good point, I shouldn't parse the output of ls. I've updated my queston to include a few examples of assessment files.
    – dylanjm
    Mar 27 '20 at 2:34
  • You probably need awk here. Something like awk '/^ \[\.\/.*\]$/ || /^ input = .*$/{a[FILENAME] = a[FILENAME] " " $0} END { for (f in a) print f": " a[f]}'
    – muru
    Mar 27 '20 at 2:58

This will work using any awk in any shell on every UNIX box:

$ 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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.