How to delete blank line/s before and after a search pattern?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to delete blank line/s before and after a search pattern?
# 1  
Old 03-23-2016
How to delete blank line/s before and after a search pattern?

Hi,

Test file x.txt below. This file is generated by a program that I unfortunately do not have control on how it gets presented/generated.

Code:
create PACKAGE "XXX_INTERFACE_DEFECT_RPT_TEST" is

TYPE refCursor IS REF CURSOR;

Function queryRecords (
  p_status varchar2,
...
...
...
) return refCursor;

Function getQueryRecords(
...
...
...

ALTER PACKAGE "XXX"."XXX_INTERFACE_DEFECT_RPT_TEST"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'


 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:01'

/

ALTER PACKAGE "XXX"."XXX_INTERFACE_PRODUCTION_WO"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'

 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:17'
/


ALTER PACKAGE "XXX"."XXX_INTERFACE_PRODUCTION_WO"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'
 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:17'

/

-- new object type path: SCHEMA_EXPORT/PACKAGE/PACKAGE_BODY
CREATE PACKAGE BODY "XXX_MORE_HERE" wrapped
...
...
...

What I am wanting to do is below:
  • Replace any line that starts with create to be create or replace.
  • Search for the REUSE SETTINGS TIMESTAMP string and delete all the blank lines that precedes it and any blank lines after it.
At the moment, I am deleting all blank lines using
Code:
sed 's/CREATE/CREATE or REPLACE/ig' x.txt | sed '/^$/d'

The main reason I am wanting to retain some of the blank lines is for clarify and to make it easier reading the code.

While deleting all the blank lines work, it makes it difficult to read the generated code. Not removing the blank lines in the REUSE SETTINGS section gives error when running the SQL script.

If I can't use sed/awk to do what's required maybe I should be write some sort of cleansing script? Smilie

Any advice much appreciated. Thanks in advance.
# 2  
Old 03-24-2016
So, just to be clear, are all of the following true?:
  1. All strings being matched are case insensitive.
  2. If a line contains create starting is column 1, that string should be changed to CREATE or REPLACE (even if a line currently starts with the string create or replace.
  3. You want to remove every set of adjacent blank lines immediately preceding and immediately following any line that contains the string REUSE SETTINGS. All other blank lines should be copied from the input to the output unchanged.
Please confirm that this list is correct (or correct items in the list that are incorrect) and show us the exact output that you hope to produce from the input shown in post #1 in this thread.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-24-2016
Hi, newbie_01,

Do you have an objection to a non-liner?

Code:
#!/usr/bin/perl
# search_and_replace.pl
use strict;
use warnings;

my $blank;
my $reuse;
while (<>) {
    s/CREATE/CREATE or REPLACE/ig;

    if (/^\s*$/) {
        $blank .= $_ unless $reuse;
    }
    elsif (/REUSE/i) {
        print and $blank = undef;
        $reuse = 1;
    }
    else {
        if($blank){
            print $blank;
            $blank = undef;
        }
        $reuse = 0;
        print;
    }
}


Save as search_and_replace.pl
Run as perl search_and_replace.pl newbie_01.input > result.output

Code:
CREATE or REPLACE PACKAGE "XXX_INTERFACE_DEFECT_RPT_TEST" is

TYPE refCursor IS REF CURSOR;

Function queryRecords (
  p_status varchar2,
...
...
...
) return refCursor;

Function getQueryRecords(
...
...
...

ALTER PACKAGE "XXX"."XXX_INTERFACE_DEFECT_RPT_TEST"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'
 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:01'
/

ALTER PACKAGE "XXX"."XXX_INTERFACE_PRODUCTION_WO"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'
 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:17'
/


ALTER PACKAGE "XXX"."XXX_INTERFACE_PRODUCTION_WO"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'
 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:17'
/

-- new object type path: SCHEMA_EXPORT/PACKAGE/PACKAGE_BODY
CREATE or REPLACE PACKAGE BODY "XXX_MORE_HERE" wrapped
...
...
...

Noticed that there is a space in front of REUSE. Do you need that removed, as well?
This User Gave Thanks to Aia For This Post:
# 4  
Old 03-24-2016
Hi,

No objection to a non-one liner. No need to remove the space in front of the REUSE. I will try this out. Thanks.

---------- Post updated at 06:57 AM ---------- Previous update was at 06:36 AM ----------

Hi Don,

Thanks for replying. Please find answers below.
  1. All strings being matched are case insensitive.
    Answer: YES
  2. If a line contains create starting is column 1, that string should be changed to CREATE or REPLACE (even if a line currently starts with the string create or replace .
    Answer
    : hmmm, very, very good thinking. I never thought of that. The answer for that is NO, if it already contains create or replace, then skip it, no change required.
  3. You want to remove every set of adjacent blank lines immediately preceding and immediately following any line that contains the string REUSE SETTINGS . All other blank lines should be copied from the input to the output unchanged.
    Answer: All blank lines preceding the REUSE SETTINGS and blank lines after it up until the next /, will have to be removed.
The output that I am expecting to get is as below:

Code:
CREATE or REPLACE PACKAGE "XXX_INTERFACE_DEFECT_RPT_TEST" is

TYPE refCursor IS REF CURSOR;

Function queryRecords (
  p_status varchar2,
...
...
...
) return refCursor;

Function getQueryRecords(
...
...
...

ALTER PACKAGE "XXX"."XXX_INTERFACE_DEFECT_RPT_TEST"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'
 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:01'
/

ALTER PACKAGE "XXX"."XXX_INTERFACE_PRODUCTION_WO"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'
 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:17'
/

ALTER PACKAGE "XXX"."XXX_INTERFACE_PRODUCTION_WO"
  COMPILE SPECIFICATION
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE    PLSCOPE_SETTINGS=  'IDENTIFIERS:NONE'
 REUSE SETTINGS TIMESTAMP '2015-06-24 23:45:17'
/

-- new object type path: SCHEMA_EXPORT/PACKAGE/PACKAGE_BODY
CREATE or REPLACE PACKAGE BODY "XXX_MORE_HERE" wrapped
...
...
...

# 5  
Old 03-24-2016
Hi newbie_01,
I'm not as fluent in perl as Aia, but the following awk script seems to do what you want:
Code:
awk '
BEGIN {	bpat = "^[[:blank:]]*$"
	cpat = "^[cC][rR][eE][aA][tT][eE] "
	corpat = cpat "[oO][rR] [rR][eE][pP][lL][aA][cC][eE] "
	rpat = "[rR][eE][uU][sS][eE] [sS][eE][tT][tT][iI][nN][gG][sS] "
}
$0 ~ bpat && dblank {
	next
}
$0 ~ bpat {
	savedblanks = savedblanks $0 "\n"
	next
}
$0 ~ rpat {
	dblank = 1
	savedblanks = ""
	print
	next
}
{	dblank = 0
	if(savedblanks != "") {
		printf("%s", savedblanks)
		savedblanks = ""
	}
	if($0 !~ corpat)
		sub(cpat, "CREATE or REPLACE ")
}
1
END {	if(savedblanks != "")
		printf("%s", savedblanks)
}' x.txt

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk (nawk won't correctly process this script).
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

2. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

3. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

4. Shell Programming and Scripting

Need help to replace a pattern with a blank line

Need help to replace the line beginning with tcp_sendspace with a blank line. # cat if en0: flags=1e080863,480<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),CHAIN> inet 10.27.53.21 netmask 0xffffff00 broadcast 10.207.52.255 inet... (11 Replies)
Discussion started by: sags007_99
11 Replies

5. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

6. Shell Programming and Scripting

Delete last blank line.

I need to delete the last line only if its blank not otherwise. (3 Replies)
Discussion started by: dinjo_jo
3 Replies

7. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies

8. Shell Programming and Scripting

how to delete a first blank line from the file

I have a file which has the first blank line: sundev22$cat /t1/bin/startallocs /t1/bin/startallocsys 123 sundev22$ Is there a command to remove this first blank line? Thanks for help -A (4 Replies)
Discussion started by: aoussenko
4 Replies

9. Shell Programming and Scripting

Multile Pattern Search in a same line and delete

HI Gurus, I need to delete a line from a syslog file, if it matches three conditions. Say for ex., if the device name is device.name.com and if it contains the syslog message PAGP-5-PORTFROMSTP in between the time period 00:00:00 to 04:00:00, then the particular line has to be deleted from... (2 Replies)
Discussion started by: sasree76
2 Replies

10. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies
Login or Register to Ask a Question