Issue
I am able to delete lines with certain patterns and shorter sed '/^.\{,20\}$/d' -i FILE
or longer sed '/^.\{25\}..*/d' -i FILE
than certain length separately, but how do I unite pattern and length in sed?
Lines containing A
should be between 20 and 25 characters
Lines containing B
should be between 10 and 15 characters
Lines containing C
should be between 3 and 8 characters
All other lines should be deleted from the file
1234567890 A 1234567890
12345 A 12345
1 A 1
1234567890 B 1234567890
12345 B 12345
1 B 1
1234567890 C 1234567890
12345 C 12345
1 C 1
So that the output should look like this
1234567890 A 1234567890
12345 B 12345
1 C 1
Solution
This is how you can do it with sed:
$ sed -ne '/A/ s/^\(.\{20,25\}\)$/\1/p; /B/ s/^\(.\{10,15\}\)$/\1/p; /C/ s/^\(.\{3,8\}\)$/\1/p;' file
1234567890 A 1234567890
12345 B 12345
1 C 1
How does it work:
-ne - suppress printing pattern
/A/ - look for pattern A
^\(.\{20,25\}\)$ - line with 20-25 characters
/\1/p - print pattern space
Answered By - Dave Grabowski Answer Checked By - Terry (WPSolving Volunteer)