I\'m attempting to use perl to edit a file, essentially to cut and paste content within the file. However, it appears I\'ve incorrectly constructed the regex. I\'d appreciate he
To match across multiple lines you need to have the whole file in a scalar
perl -0777 -pe's/(export PATH.*?\n)(.*?)(?=\n?# THIS IS LAST)/$2$1/s' input.txt
where -0777 enables "slurp" mode, and (?=...)
is a positive lookahead.
The \n?
is in the lookahead to include the optional empty line that shouldn't be consumed, since that line shouldn't be moved. Then the previous pattern need be non-greedy, too. This protects a single preceding empty line, any others are matched and swapped.