Linux - delete lines containing ":\r\n"

Viewed 35

I'm looking for a command to delete lines where ":\r\n" is found. For example from:

test1:
test2:true
tool3:
tool4:false

to:

test2:true
tool4:false

I'm not used to sed very much I tried:

sed '/:\r\n/d' doesn't work

sed '/test1/d' works for this line

2 Answers

Instead of trying to match the newline character, you could match the EOL character, represented by $:

sed '/:$/d' file.txt

You can print all lines that have a : followed with any char:

sed -n '/:./p' file.txt

Here, -n suppresses default line output, :. matches a colon followed with any one char, and p prints the line matched.

It is even more elegant with awk:

awk '/:./' file.txt

See the online demo:

#!/bin/bash
s='test1:
test2:true
tool3:
tool4:false'
sed -n '/:./p' <<< "$s"
awk '/:./' <<< "$s"

Both output:

test2:true
tool4:false
Related