Little sed Tricks

I am beginning to use sed more and more these days. As I learn, I want to document and share that knowledge for all, and to recognize the places from where I learned.

Remove Selective Newline

I had text similar to the following, with ~ as the delimiter. I wanted to replace certain newline characters so that two or more lines combined into one.

1~Some Text~123,456,789~Some More Text
2~Some Text~9223,321,
,789~Some More Text
3~Some Text~3556,36512,57567~Some More Text

In example above, line 2 and line 3 should be just one line. To remove a selective newline, and to bring two lines into one, I used the following (hat tip to sed and newline):

sed '/,$/{N;s/\n//;}'

We are searching for ,$ which means a pattern where the last character on the line is a comma. We then replace it with {N;s/\n//;}. N means “Read/append the next line of input into the pattern space“. After that we again do a search/replace by replacing newline with nothing. This means, in effect, that whenever you find a pattern, read the next line, and replace the newline with nothing.

This resulted in the following text, which is what I needed.

1~Some Text~123,456,789~Some More Text
2~Some Text~9223,321,,789~Some More Text
3~Some Text~3556,36512,57567~Some More Text

Delete Selective Line

If you want to delete or ignore one or more specific lines, sed can help you with that as well. Extending the example above, let’s say we have following text.

1~Some Text~123,456,789~Some More Text
2~Some Text~9223,321,,789~Some More Text
3~Some Text~3556,36512,57567~Some More Text
4~Some Text~(00:00 – 23:59)~Some More Text
5~Some Text~~Some More Text

Let’s say you want to ignore or delete the line starting with 4. As you can see, colon is the character in this line but not in any other. This helps us identify which lines to ignore. The idea, then, is to pick a unique characteristic which clearly identifies the line(s) to ignore. The command, then, is as below.

sed '/:/d'

Hat tip: delete line in file with sed; Ignore Lines Begining With #.

Comments are closed.