Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a file that looks something like this:

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

I need to run the text file through sed to remove all of the lines that start with spaces (number varies) and a hyphen, and another space.

What is the regex or pattern I need to use with sed to make the output look like this below?

Heading - 
  * Need to complete bar

Another - 
  * Need to complete foo
share|improve this question
add comment

4 Answers 4

up vote 3 down vote accepted

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

share|improve this answer
    
You could use + instead of * to make sure there's at least one space. –  Dennis Williamson Nov 6 '09 at 18:39
add comment

"s/\s*-\s.*//g" should do it, I think.

That's \s to match a space, * to match zero or more of the preceding character (the space), a literal hyphen character, then another space, then .+ to match everything after it.

share|improve this answer
    
Close. Very close! This leaves blank lines. It does not remove the carriage return/line feed. –  eleven81 Nov 6 '09 at 16:03
    
damn! Try appending a \s to the search string, then, I'm afraid I don't do enough work on full lines :P –  Phoshi Nov 6 '09 at 17:45
add comment

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.

share|improve this answer
    
With tabs: egrep -v $'^[ \t]+-[ \t]' /tmp/foof –  Dennis Williamson Nov 6 '09 at 18:36
add comment

For filtering entire lines from the output, you usually want grep, not sed. In particular, to exclude specific lines, you'll want to use grep -v 'exclusion-regex'.

share|improve this answer
1  
i dont see why one wants 'grep' instead of 'sed' .. except for more complex syntax. so, why is using grep for filtering stuff out better than the 'd' command of 'sed'? –  akira Nov 6 '09 at 17:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.