I had files with one key-value pair per line, e.g.:
Acquired Name: foo Acquired Date: 1066-10-14T10:00:00Z etc.
I wanted to extract all of the fields to one CSV line per file.
This is the sed script that I used. As it happens, this would also work if all of the input files were concatenated (because I used the ‘d’ command rather than ‘q’).
/Acquired Name:/ { s/.*Acquired Name: \(.*\)./"\1"/ ; h ; } /Acquired Date:/ { s/.*Acquired Date: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; } /Acquired Time:/ { s/.*Acquired Time: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; } /Job Code:/ { s/.*Job Code: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; } /Instrument:/ { s/.*Instrument: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; } /Bottle Number:/ { s/.*Bottle Number: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; } /Inlet Method:/ { s/.*Inlet Method: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; } /MS Method:/ { s/.*MS Method: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; } /Tune Method:/ { s/.*Tune Method: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; p ; d ; }
The manual for sed
<https://www.gnu.org/software/sed/manual/sed.html>
explains the commands used:
- d – Delete the pattern space; immediately start next cycle with next input line.
- h – Copy the pattern space to the hold space
- G – Append the hold space to the pattern space
- p – Print the pattern space
- s/pattern/replacement/ – Substitute pattern with replacement
- s/pattern/replacement/M – Do multi-line substitution; see this
- x – Exchange the pattern space and the hold space