Swap a line with another
If the idea is to swap the MATCH
line with the immediately preceding one, then something like this would do:
$ awk '!/MATCH/ { if (NR > 1) print prev; prev=$0}
/MATCH/ {print $0;}
END {print prev}' < file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The script holds the previous line in prev
, printing and updating it on the non-matching lines. On lines matching the pattern, it prints the current line, leaving the previous in the variable to be printed next.
Special cases for the first line (NR==1
) when there's no previous line to print, and for the END
when we print the held line.
Using sed
with a N;P;D cycle
:
sed -e '$!N;s/\(Line 1\)\(\n\)\(.*MATCH.*\)/\3\2\1/;t' -e 'P;D' infile
This will swap only if the line with MATCH
is preceded by Line 1
: the t
without label branches to the end of script if successful and so it avoids another swap if any Line 1
is followed by consecutive lines with MATCH
. Adjust the regex for any leading/trailing blanks.
Using ed
:
$ printf 'g/MATCH/m-2\n,p\n' | ed -s file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The m
command moves the current line after to the subsequent target address. Here, we find all lines matching MATCH
(it's the g
in front of the regular expression that makes this a "global" operation), and for each line move it one line up (to "after the line two lines up"). The effect is that the MATCH
lines swap places with the immediately preceding lines.
The final ,p
in the editing script just displays the modified editing buffer. This could be changed to something like wq
to write the changed editing buffer back to the original file.
Note that using ed
for editing files might look neat, but is not recommended for large files as the whole file is read into memory.