I have been doing this by hand and I just can’t do it anymore– I have thousands of lines and I think this is a job for sed or awk.
Essentially, we have a file like this:
A sentence X
A matching sentence Y
A sentence Z
A matching sentence N
This pattern continues for the entire file. I want to flip every sentence and matching sentence so the entire file will end up like:
A matching sentence Y
A sentence X
A matching sentence N
A sentence Z
Any tips?
edit: extending the initial problem
Dimitre Radoulov provided a great answer for the initial problem. This is an extension of the main problem– some more details:
Let’s say we have an organized file (due to the sed line Dimitre gave, the file is organized). However, now I want to organize the file alphabetically but only using the language (English) of the second line.
watashi
me
annyonghaseyo
hello
dobroye utro!
Good morning!
I would like to organize alphabetically via the English sentences (every 2nd sentence). Given the above input, this should be the output:
dobroye utro!
Good morning!
annyonghaseyo
hello
watashi
me
N– append the next line of input into the pattern space\(.*\)\n\(.*\)– save the matching parts of the pattern spacethe one before and the one after the newline.
\2\\– exchange the two lines (\1 is the first saved part,\1
\2 the second). Use escaped literal newline for portability
With some sed implementations you could use the escape sequence
\n:
\2\n\1instead.