How to remove lines with specific pattern and next line

D. Elliott

I am new to Unix and currently I have a large file of various data. In this file there are lines that are now redundant and will need to be removed.

In the file the pattern:

<contact contact_id="<number>" txn="D">
</contact>

Edit: There are also similar lines to the ones to be removed, an example is:

<contact contact_id="<number>" txn="N">
</contact>

I have attempted to use grep -A 1 to pick up the pattern and remove the next line however I am operating on an old version of Solaris and -A is an illegal expression.

As well as this I have attempted to use sed -e '12442,+1d' and this just give the ouput of

sed: command garbled: 12442,+1d

.

Please can you help me with a new solution.

Ronald

use awk?

something like

/<contact contact_id=.* txn="D">/ { got_contact = 1; next }
got_contact == 1 { got_contact = 0; next }
{ print }

even the ancient awk should be able to handle that. (There might be a more compact solution, but this isn't code golf)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use Pattern, Matcher in Java regex API to remove a specific line

Remove lines with specific pattern with Bash

Remove lines with specific pattern

sed remove lines that starts with a specific pattern

Sed/Awk: how to find and remove two lines if a pattern in the first line is being repeated; bash

How to create a regex pattern to remove line breaks after specific tag

print line containing pattern, concatenate next lines until next line with pattern found

sed/awk/perl remove the first two lines of a 3 line pattern

How to remove specific pattern in string?

sed remove end of line for specific lines

Read lines from a file and skip to next line if pattern matches in perl

Remove a line of text and the next 0 to 5 lines with powershell 2

How to remove line matching specific pattern from a file

How to sort lines based on specific pattern in that line using python

Remove lines with specific line number specified in a file

Extract nth line matching pattern and the next N lines

Remove spaces except lines beginning with a specific pattern

SED: remove pattern from two specific lines

How to remove specific empty lines from multi line strings in python?

Extracting lines containing pattern + next line

grep pattern next two lines and discard the pattern with one line

Remove a line if all fields match a specific pattern

bash : Remove the specific pattern of characters from a line

How to combine a first line with several next lines?

Search for pattern and print next lines in pattern matching line

How to remove lines with a pattern if the next line matches the same pattern?

python Pandas how to remove lines from dataframe based on next lines (in a specific timeframe)

How to remove pattern line if next line is empty using sed

Remove all lines after a specific line in Python