The row of dots represents any number of lines that don't contain the strings "key1:" or "key2:" The strings key1: and key2: will always appear alternately as in the above example.
The sed command:
sed -n -e 's_key1: \(...\)_\1_p' -e 's_key2: \(...\)\1_p'
produces this output:
ABC
DEF
GGG
HHH
But what I really want is this output:
ABC DEF
GGG HHH
At one point I thought there was a upper case P flag that would print without
the newline character. That would do it, but at least with my GNU sed version 4.2.1 I don't see such a flag.
Are there any other simple ways to modify my sed command to produce the desired output?
Welcome to forums, please use code tags for commands/codes/Inputs which you are using in your posts as per forum rules. You could try following an awk solution.
Thanks R. Singh (and sorry for forgetting the code tags).
Yes, I did simplify the problem somewhat for brevity, so I will probably have to learn at least some of that arcane looking awk syntax. (I realize that sed is just as arcane, its just that I am already familiar with it).
It seems like this may be simple enough a sed solution might be possible without resorting to overly cryptic methods. If anyone knows of such a solution, I would still be interested in seeing it.
--
( or sed -n 's/key[12]: \(...\)/\1/p' file | paste -d" " - - if you will )
---
That will not work since even in GNU sed, the trailing newline will be chopped off before the line gets processed, so the substitute command will never "see" it..
Before the reply from neutronscott came in, I managed to produce the desired output by piping it to a separate sed command to remove the unwanted newlines (which is somewhat similar to the solution suggested by Scrutinizer using paste):
sed -n -r -e "s_.* key1: ([^,]*).*_\1_p" -e "s_.*key2: ([^,]*).*_\1_p" input.txt | sed "N;s/\n/ /"
Using the method suggested by neutronscott, I'm now producing the same output using:
sed -n -r "/key1:/{s/.* key1: ([^,]*).*/\1 /;h};/key2:/{H;x;s/\n.* key2: ([^,]*).*/\1/p}" input.txt
It's actually slightly longer, but I prefer a single call to sed when possible.