Extracting part of files with sed
For reference for my future self, a few handy sed commands. Let’s consider this file:
$ cat test-sedWe can extract the lines from the start of the file to the marker by deleting the rest:
First line
Second line
–
Another line
Last line
$ sed '/–/,$d' test-sed
First line
Second line
a,b
is the range the command, here d(elete)
, applies to. a
and b
can be, among others, line numbers, regular expressions or $
for end of the file. We can also extract the lines from the marker to the end of the file with:$ sed -n '/–/,$p' test-sedThis one is slightly more complicated. By default sed spits all the lines it receives as input,
–
Another line
Last line
’-n’
is there to tell sed not to do that. The rest of the expression is to p(rint)
the lines between –
and the end of the file.That’s all folks!