Home » Questions » Computers [ Ask a new question ]

using sed to remove lines in a file

using sed to remove lines in a file

I have a file that looks something like this:

Asked by: Guest | Views: 301
Total answers/comments: 2
Guest [Entry]

I used Phoshi's answer, assisted by Dennis Williamson, to help me come up with sed /^\s+-\s.*/d which works as expected.
Guest [Entry]

"You should use egrep or grep for this task, sed is a stream editor, grep is more in line with the line-at-a-time philosophy.

You need a regex that matches the start of line, whitespace, hyphen, space. Sounds like this would work:

egrep -v '^[ ]+-[ ]' filename

The -v option causes egrep to REMOVE the matching lines -- this is easier than building a regex that rejects the lines.

Example:

nobody$ egrep -v '^[ ]+-[ ]' /tmp/foof
Heading -
* Need to complete bar

Another -
* Need to complete foo
nobody$ cat /tmp/foof
Heading -
- Completed foo
- More information
- Still more
* Need to complete bar
- Did baz (comment blah blah) ***

Another -
* Need to complete foo
- Completed bar (blah comment blah) ***
- Done baz
nobody$ _

Dealing with Tab characters only means you need them in the bracket expressions,but that's hard to show online."