Extracting part of files with sed

For reference for my future self, a few handy sed commands. Let’s consider this file:

$ cat test-sed
First line
Second line

Another line
Last line
We can extract the lines from the start of the file to the marker by deleting the rest:
$ 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-sed 

Another line
Last line
This one is slightly more complicated. By default sed spits all the lines it receives as input, ’-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!


Unix

149 Words

2012-01-10 14:34 +0000