3

I've been using this forum, so I just now joined! I have seen similar questions but nothing has worked so far (mostly using sed). I want to find each instance of an asterisk (*) in a file and delete it and everything after it until a blank line is reached. For example:

*252
253
254
255
(blank line here)

OR

*261
265
(blank line here)

Each block of data will vary in length. Then I want to have output go to a new file. Any ideas?

  • 2
    What exactly hasn't worked so far ? This is really simple... – don_crissti Aug 16 '16 at 22:20
  • sed '/"*"/,/" "/{/" "/ s/.*/" "/; t; d}'DIDtemp2 >DIDtemp1 – 985ranch Aug 16 '16 at 22:23
  • that's the most recent that didn't work. I based that on another response in here. I've deleted the other two already. – 985ranch Aug 16 '16 at 22:25
  • 2
    Since you've been using "this forum" you know this kind of stuff should be part of your question and not a comment... – don_crissti Aug 16 '16 at 22:26
  • Sorry... still learning! – 985ranch Aug 16 '16 at 22:27
5

To delete from a line containing * to the next empty line, use:

sed '/[*]/,/^$/d' filename

Notes:

  1. Since * is a regex-active character and we only want to match a literal *, we need to escape the *. This could be done as \* or, as shown above, [*].

  2. To match an empty line, we need a line with no characters between its beginning, denoted ^, and its end, denoted $. Thus ^$ matches an empty line.

  3. /[*]/,/^$/ is a range: it matches a group of lines that start with a line containing * and end with an empty line.

  4. d tells sed to delete any line in the range.

The above produces output to stdout. To change a file in place, use:

sed -i.bak '/[*]/,/^$/d' filename

Example

Consider this sample file:

$ cat filename
*252
253
254
255

OR
*261
265

end

This produces:

$ sed '/[*]/,/^$/d' filename
OR
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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