Home > OS >  Delete lines between 2 patterns in all occurrences in a text file
Delete lines between 2 patterns in all occurrences in a text file

Time:01-08

This is the sample file I have and I wanted to delete these lines between 2 patterns for all the occurrences in the file. (Loop of details printed in here)

022-Central-Florida

SQL*Plus: Release 21.0.0.0.0 - Production on Fri Jan 7 07:12:44 2022
Version 21.4.0.0.0

Copyright (c) 1982, 2021, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> 
VERSION
--------------------
45.2.7

SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
009-Pittsburgh

SQL*Plus: Release 21.0.0.0.0 - Production on Fri Jan 7 07:12:44 2022
Version 21.4.0.0.0

Copyright (c) 1982, 2021, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> 
VERSION
--------------------
45.2.7

I wanted to delete lines starting with SQL*Plus... up to SQL> but SQL> should be exclusive and SQL*PLUS line should be deleted. And also the line starts with SQL> Disconnected .. and the line below that line also needed to be deleted.

The expected outcome is

022-Central-Florida
SQL> 
VERSION
--------------------
45.2.7

009-Pittsburgh
SQL> 
VERSION
--------------------
45.2.7

Can someone help me to achieve this one using sed or awk.

CodePudding user response:

Using ed, which almost always works better for editing files than sed (In this case because it can refer to the line before one that matches a regular expression when addressing a block of lines to delete):

$ printf "%s\n" 'g/^SQL\*Plus/.,/^SQL>/-1d' 'g/^SQL> Disconnected/., 1d' w | ed -s input.txt
$ cat input.txt
022-Central-Florida

SQL>
VERSION
--------------------
45.2.7

009-Pittsburgh

SQL>
VERSION
--------------------
45.2.7

Probably more legible as a heredoc (Or interactively if this isn't for a script):

ed -s input.txt <<'EOF'
g/^SQL\*Plus/.,/^SQL>/-1d
g/^SQL> Disconnected/., 1d
w
q
EOF

CodePudding user response:

How about an awk answer:

awk -v f=1 '                                    # initialize f to 1
    /^SQL\*Plus/ {f = 0; next}                  # reset f to 0
    /^SQL> Disconnected/ {c = 2; next}          # set c to count down the lines
    /^SQL>/ {f = 1}                             # set f to 1
    !(c && --c) && f                            # print if c is inactive and f is set
' input.txt

Output:

022-Central-Florida

SQL>
VERSION
--------------------
45.2.7

009-Pittsburgh

SQL>
VERSION
--------------------
45.2.7

CodePudding user response:

This might work for you (GNU sed):

sed -En '/^SQL> Disconnected/{n;n};/^SQL\*Plus/{:a;n;/^SQL>\s*$/!ba};p' file

Match a line beginning SQL> Disconnected delete it and the following line.

Match a line beginning SQL*Plus, delete it and any following lines until a line beginning SQL> only.

Print all other lines.

  •  Tags:  
  • Related