0

I need to print the inverse match (inverse match pattern will be in first line and in end of word in my paragraph) in a paragraph until a line break.

Tried with sed using [^text] not working.

My pattern string will be at the end of word xxx_yyyy: ZZZ.

sed -n '/[^ZZZ]$/,/^$/p' not working.

Can some one correct me what wrong here.

Input:

sometext tobe here xxx_yyyy: ZZZ sjhdajsh skdjah hjk sometext tobe here xxx_yyyy: AAA sjhdajsh skdjah hjk sometext tobe here xxx_yyyy: ZZZ sjhdajsh skdjah hjk 

Desired output:

needs to print the paragraph not contain xxx_yyyy: ZZZ

sometext tobe here xxx_yyyy: AAA sjhdajsh skdjah hjk 
2
  • 3
    [^text] matches any character that is not t or e or x or t - you probably want sed -n '/ZZZ$/,/^$/!p' or sed '/ZZZ$/,/^$/d' (delete lines that ARE in the addressed range) Commented Dec 15, 2020 at 2:26
  • 1
    @Renga do you realise what steeldriver told you? Your [^ZZZ] pattern is not "match anything except ZZZ, at the start of line", it's "match any one character that isn't Z (and isn't Z and isn't Z), anywhere in the line" Commented Dec 15, 2020 at 14:17

1 Answer 1

1

This would be easier to do with awk than with sed:

$ awk -v RS='' -F '\n' '!($1 ~ /ZZZ$/)' file sometext tobe here xxx_yyyy: AAA sjhdajsh skdjah hjk 

This first sets the record separator, RS, to the empty string. With this special value of RS, awk will treat each paragraph, delimited by one or more empty line, as a record. With -F '\n' we tell awk to treat each line of the paragraph as a field.

The test !($1 ~ /ZZZ$/) will be true for each record (paragraph) whose first field (1st line) does not end with the string ZZZ. Each such paragraph will be printed.

If you want an empty line after each outputted section, add -v ORS='\n\n' to the awk command line (before the awk code).


With sed, you could possibly do

$ sed '/ZZZ$/,/^$/d' file sometext tobe here xxx_yyyy: AAA sjhdajsh skdjah hjk 

I.e., "delete all lines between any line ending with ZZZ to the next empty line". You could also use the sed expression /ZZZ$/,/^$/!p with sed -n. Note however that an ZZZ on any other line other than the first line of a paragraph would also trigger this deletion.

This issue could obviously be fixed (by reading paragraphs, line by line, into the hold space, and then deciding which ones to print and which ones to discard), but it would turn into more of a bother than just using the awk variation above.


Just to comment on the expression [^ZZZ] that you use in the question. This is exactly equivalent to [^Z] and will match any single character that is not a Z. Likewise, [^text] is the same as [^etx] and would match any single character that is not an e, t, or x.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.