Issue
I am trying to move an existing line that starts with let's say: abc::
below an existing line that starts with id:
in various md files in a folder.
Let's assume the files looked like this
(file 1)
text
id: 123
more text
abc:: cba
(file 2)
text
id: 321
more text
a lot more text
abc:: def
The result of running the code should in both cases be that abc::...
moves right below id:...
My assumption was, that the following code, would move the line that starts with abc::
right below the line that contains id:
However, if I run the code, it runs over the files (as per edited timestamp), but changes nothing.
MWE:
find . -name '*.md' -type f -exec sed -i '' '/^id:/{
N
s/\n\(abc::.*\)/\1\
/
}' {} +
Solution
This sed
command (executed via find
's -exec
) should do the trick:
find . -name '*.md' -type f -exec sed -i '' '
/^id:/,/^abc::/{
/^id:/b
/^abc::/{p; x; s/.//p; d;}
${x; s/.//p; x; p; d;}
H; d
}' {} +
Answered By - M. Nejat Aydin Answer Checked By - David Goodson (WPSolving Volunteer)