0

This is working on some tools as regex101.com but I cannot make it work with sed.

Blocks:

dn: abcd1,ou=test
aaaaa
bbbb
1111

dn: abcd2,ou=test
33333
ddddd
aaaaa

dn: qwert,ou=test
55555
hhhh
dddd

I want to match and replace with nothing every block that starts with dn: abcd. A block always ends with \n\n.

Regexp: (?s)\b(?:(?!\n\n).)*?\bdn: abcd\b(?:(?!\n\n).)*

Is it possible to achieve with sed?

3
  • try using perl... sed doesn't support lookarounds or non-capturing groups and many other features that are available in perl... Commented Mar 29, 2018 at 14:32
  • also, if you could modify given input sample without \n (assuming you have literal newlines and not \ followed by n) - use the {} button in editor to format the sample instead of backticks.. and add complete expected output, it'd help to suggest solutions.. Commented Mar 29, 2018 at 14:34
  • @Sundeep already edited, thanks! I'm not familiar with perl, any idea? Commented Mar 29, 2018 at 14:37

3 Answers 3

1

is a better choice for this task.

$ perl -00ne 'print if not /^dn: abcd/' file

or

$ perl -ne 'print if not /^dn: abc/ .. /^$/' file

dn: qwert,ou=test
55555
hhhh
dddd
Sign up to request clarification or add additional context in comments.

Comments

1

Use awk's paragraph mode

$ awk -v RS= '!/^dn: abcd/' ip.txt
dn: qwert,ou=test
55555
hhhh
dddd
  • -v RS= When RS is set to empty string, one or more consecutive empty lines is used as input record separator
  • !/^dn: abcd/ to ignore paragraphs starting with dn: abcd

note that default output record separator is single newline, so you might need something like this:

$ awk -v RS= -v ORS='\n\n' '!/^dn: abcd1/' ip.txt
dn: abcd2,ou=test
33333
ddddd
aaaaa

dn: qwert,ou=test
55555
hhhh
dddd

Comments

0

You can try this sed

sed '/^dn: abcd/,/^$/d' infile

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.