@RahulVerma's awk
solution is simple and awesome! However, I've been looking for a sed
solution. I wondered if sed
is capable to solve this sort of problem. Finally, I've found my own solution! The core idea is treat the hold space as a variable, put what we want to search in the hold space, and search it in the current input line. I want to give credit to @chaos, because the core idea is from his answer how-to-search-for-the-word-stored-in-the-hold-space-with-sed#239049.
Using sed one liner
sed -E '1{:OK; h; $b; d}; x; G; s/^([^:]+)(.*)\n\1:(.*)/\1\2;\3/; tOK; s/\n.*//' file
or
sed -E 'x; G; s/^([^:]+)(.*)\n\1:(.*)/\1\2;\3/; tOK; 1d; s/\n.*//; b; :OK; h; $!d' file
Output:
apple:A fruit;Type of: pie
banana:tropical fruit
cherry:small burgundy fruit;1 for me to eat;bright red
Explanation: (the 2nd sed
)
x
exchange the hold space and the pattern space
- The hold space would become the current input line.
- The pattern space would become the content generated by
s///
in the last cycle (see below), or the last input line if s///
fails to replace anything. It is empty for the very beginning, i.e., when the current input line is the line 1.
G
append the hold space (the current input line) to the pattern space (the content generated in the last cycle). Thus, the pattern space would be two lines (The hold space is not changed). For example,
- If the current input line is line 1, the pattern space would be
# the empty hold space
apple:A fruit # the current input line
- If it is line 2, the pattern space would be
apple:A fruit # the last input line, because `s///` fails to replace anything in the last cycle
apple:Type of: pie # the current input line
- If it is line 3, the pattern space would be
apple:A fruit;Type of: pie # the resulting content generated by `s///` in the last cycle
banana:tropical fruit # the current input line
s///
use the first field ^([^:]+)
(:
is the field separator) in the first line of the pattern space to search the second line. The part of \n\1:
indicates how it works. If it is found in the second line, concatenate the first line \1\2'
, a ;
, and the 2+ fields of the second line \3
. Thus, the result is \1\2;\3
.
tOK
if the s///
is successful (s
has replaced something) jump to the label OK
, otherwise continue
- In case of success
:OK
specify the location of the label OK
h
put the pattern space which is \1\2;\3
, generated by s///
, to the hold space. (The first line of the pattern space in the next cycle is made up of this very content)
$!d
delete it, don't print it, if hasn't reached the last line
- In case of failure, (
s///
hasn't changed anything, the pattern space still has two lines)
1d
delete the pattern space, don't print it, if the current input line is the line 1. At this moment, there is nothing ready to output. Without this, an empty line would be printed.
s/\n.*//
delete the second line of the pattern space, which is the current input line.
b
jump without a label, means end the sed
script, and the remaining content in the pattern space would be printed out before starting the next cycle.
- This is the very place where we finish combining the lines starting with the "current" same field and print out the combined line.
- The hold space is still holding the the current input line for this cycle, and is going to be "the last input line" for the next cycle (see above). What it holds will trigger a brand new combining of the lines starting with a new same field.
In the second sed
command line, the 1d;
is not very necessary, however, the output has a subtle difference without it -- an innocuous empty line would be printed on the top, like below,
apple:A fruit;Type of: pie
banana:tropical fruit
cherry:small burgundy fruit;1 for me to eat;bright red
Thoughts at the end
The time I spent to figuring out the sed
solution and the final look of the sed
solution, both indicates that awk
is better and easier for problems as complicated as this. sed
is not good at searching/replacing a file according to the file itself. Like, searching/replacing constant text, it is easy, but what to search/replace is from the file itself, which means it might vary from line to line. For this kind, sed
is not as good as awk
-- the superior that has the full programming facilities, like variable, function, if-else, for/while/do. (See further in what is the difference between sed and awk)