SED - adding blank line after each Range Match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED - adding blank line after each Range Match
# 1  
Old 06-09-2009
SED - adding blank line after each Range Match

the following range matching works great but i wish to add a blank line after each range result set... which i've tried and researched to no avail

MY INPUT DATA:

Quote:
typeset lfile_selection_count=${1}
typeset lcyclename_suffix=_cycle${lfile_selection_count}

cat > ${Vsqlfile} <<BATCH_EOF
-- THESE OPTION SETTINGS MAKE SURE THAT THE SQL IS PROCESSED
-- CORRECTLY WITHIN THE CONTEXT OF THIS SCRIPT. SPECIFICALLY,
-- THE SCRIPT WILL STOP AND ISSUE AN ERRROR MESSAGE ON THE FIRST
-- SQL STATEMENT THAT RETURNS A NON-ZERO CONDITION CODE (-S ON).
-- THE SCRIPT WILL NOT COMMIT ANY CHANGES UNTIL THE COMMIT
-- STATEMENT IS EXECUTED AT THE END OF THE SCRIPT (I.E. -C AUTOCOMMIT
-- IS OFF). tHE PROMPT IS TURNED OFF SO THAT IT IS NOT SHOWN
-- IN THE OUTPUT.
CONNECT TO ${Gdb2instance_ar} user ${Gdb2user} using XXXXXXXX;
UPDATE COMMAND OPTIONS USING p OFF;
UPDATE COMMAND OPTIONS USING s ON;
LIST COMMAND OPTIONS;
alter table ${Gmdsschema}.PLN_RV_SM activate not logged initially;
INSERT into ${Gmdsschema}.PLN_RV_SM
SELECT
a11.CLNDR_MTH_ID,
a11.CMPNY_GRP_CD,
a12.CRPRT_ST_CD,
a11.FNCTN_CD,
a11.PLCY_RV_CTGRY_CD,
'RQ',
a11.PLN_CRNT_YR_CN
FROM
${Gmdsschema}.PLN_RV_SM a11
join ${Gmdsschema}.CRPRT_ST a12
on (a11.CRPRT_ST_CD = a12.CRPRT_ST_CD)
WHERE a11.cmpny_grp_cd in ('GE/GG')
AND a11.TRNCTN_TYP_CD = 'TI'
;
alter table ${Gmdsschema}.PLN_RV_SM activate not logged initially;
INSERT into ${Gmdsschema}.PLN_RV_SM
select a11.CLNDR_MTH_ID,
a11.CMPNY_GRP_CD,
a12.CRPRT_ST_CD,
a11.FNCTN_CD,
a11.PLCY_RV_CTGRY_CD,
'RQ',
a11.PLN_CRNT_YR_CN
FROM
${Gmdsschema}.PLN_RV_SM a11
join ${Gmdsschema}.CRPRT_ST a12
on (a11.CRPRT_ST_CD = a12.CRPRT_ST_CD)
WHERE a11.cmpny_grp_cd in ('GI')
AND a11.TRNCTN_TYP_CD = 'TI'
;


CURRENT CODE I'M USING:

Code:
 
sed -n '/[Ss][Ee][Ll][Ee][Cc][Tt][ ]*$/,/;/p' $INPUT_FILE

RESULTS I'M GETTING:

Quote:
SELECT
a11.CLNDR_MTH_ID,
a11.CMPNY_GRP_CD,
a12.CRPRT_ST_CD,
a11.FNCTN_CD,
a11.PLCY_RV_CTGRY_CD,
'RQ',
a11.PLN_CRNT_YR_CN
FROM
${Gmdsschema}.PLN_RV_SM a11
join ${Gmdsschema}.CRPRT_ST a12
on (a11.CRPRT_ST_CD = a12.CRPRT_ST_CD)
WHERE a11.cmpny_grp_cd in ('GE/GG')
AND a11.TRNCTN_TYP_CD = 'TI'
;
select
a11.CLNDR_MTH_ID,
a11.CMPNY_GRP_CD,
a12.CRPRT_ST_CD,
a11.FNCTN_CD,
a11.PLCY_RV_CTGRY_CD,
'RQ',
a11.PLN_CRNT_YR_CN
FROM
${Gmdsschema}.PLN_RV_SM a11
join ${Gmdsschema}.CRPRT_ST a12
on (a11.CRPRT_ST_CD = a12.CRPRT_ST_CD)
WHERE a11.cmpny_grp_cd in ('GI')
AND a11.TRNCTN_TYP_CD = 'TI'
;

RESULT I looking to get: (see blank line after semicolumn)

Quote:
SELECT
a11.CLNDR_MTH_ID,
a11.CMPNY_GRP_CD,
a12.CRPRT_ST_CD,
a11.FNCTN_CD,
a11.PLCY_RV_CTGRY_CD,
'RQ',
a11.PLN_CRNT_YR_CN
FROM
${Gmdsschema}.PLN_RV_SM a11
join ${Gmdsschema}.CRPRT_ST a12
on (a11.CRPRT_ST_CD = a12.CRPRT_ST_CD)
WHERE a11.cmpny_grp_cd in ('GE/GG')
AND a11.TRNCTN_TYP_CD = 'TI'
;

select
a11.CLNDR_MTH_ID,
a11.CMPNY_GRP_CD,
a12.CRPRT_ST_CD,
a11.FNCTN_CD,
a11.PLCY_RV_CTGRY_CD,
'RQ',
a11.PLN_CRNT_YR_CN
FROM
${Gmdsschema}.PLN_RV_SM a11
join ${Gmdsschema}.CRPRT_ST a12
on (a11.CRPRT_ST_CD = a12.CRPRT_ST_CD)
WHERE a11.cmpny_grp_cd in ('GI')
AND a11.TRNCTN_TYP_CD = 'TI'
;

I thank you in advance for your time.

-----Post Update-----

I found solution at:

https://www.unix.com/shell-programmin...d-pattern.html

Sorry for the trouble... didn't see that post until it came up as "helpfull thread" to my new post :-(

Last edited by danmauer; 06-10-2009 at 01:05 PM..
# 2  
Old 06-10-2009
You could try something like this:

Code:
sed -n '
  /[Ss][Ee][Ll][Ee][Cc][Tt]/,/;/{/;/G;p;}
  ' infile

Some versions of sed support the I (for case Insensitive) flag:

Code:
sed -n '
  /select/I,/;/{/;/G;p}
  ' infile

# 3  
Old 06-10-2009
Quote:
sed -n '/select/I,/;/{/;/G;p}
' infile
Awsome!...... in my quest to understand the hows and whys of SED, ( so as not to just be a coding monkey Smilie) could you explaing the part in red

sed -n '/select/I,/;/{/;/G;p}

i beleive the p is to output the results and /;/G to add a blank line..
but I don't understand the purpose/usage of the construct { .... } and the ; between G & p

Can you help me understand so i can use this in the future?


PS: could you recommend a usefull sed book?

Thanks in advance for you help
# 4  
Old 06-10-2009
The braces limit the scope of the following code to the previously matched records (the records that satisfy the range pattern).
So, the G command (append the hold space to the pattern space, in this case the hold space is empty so you get only the newline character)
will apply on for the records that match the pattern /;/ (the last line of your paragraph).

The only printed book I know is sed & awk by Dale Dougherty and Arnold Robbins.

You can begin with Bruce Barnett's sed tutorial.
# 5  
Old 06-10-2009
[quote=radoulov;302324253]The braces limit the scope of the following code to the previously matched records (the records that satisfy the range pattern).
So, the G command (append the hold space to the pattern space, in this case the hold space is empty so you get only the newline character)
will apply on for the records that match the pattern /;/ (the last line of your paragraph).

quote]


i understand that the /;/ as part of /;/G means that newline will go after the semicolum... BUT...

i'm refering to the following semicolum , between the G and p

Code:
 
{/;/G;p}

Also..... if {coding} limits the scope to the previous command.... is that the same as using the -e to apply code to just previously execute expression?

Code:
 
sed -e '/select/I,/,/;/!d' -e '/;/G'



Thanks again for your patience with my questions.

-----Post Update-----

Quote:
Originally Posted by radoulov
You could try something like this:

Code:
sed -n '
  /[Ss][Ee][Ll][Ee][Cc][Tt]/,/;/{/;/G;p;}
  ' infile

Some versions of sed support the I (for case Insensitive) flag:

Code:
sed -n '
  /select/I,/;/{/;/G;p}
  ' infile


Thanks, the first worked, but the /I,/ didn't work...

but i had to add back after the select [ ]*$ because: (which works for 1 & 2 below but not 3)

1. i don't want to catch occurences of where select is part of word, (e.g. selection)

2. i want to catch when select is the only word on the line.

3. i want to catch when select is not the only word on the line, and is followed by a space

***** see that i've modified the test data to show you *****

Code:
 
sed -n '/[Ss][Ee][Ll][Ee][Cc][Tt][ ]*$/,/;/{/;/G;p;}'


Last edited by danmauer; 06-10-2009 at 01:07 PM..
# 6  
Old 06-10-2009
Quote:
Originally Posted by danmauer
[...]1. i don't want to catch occurences of where select is part of word, (e.g. selection)

2. i want to catch when select is the only word on the line.

3. i want to catch when select is not the only word on the line, and is followed by a space

Some sed implementations support word boundaries:

Code:
/\<[Ss][Ee][Ll][Ee][Cc][Tt]\>/

Or just use a more powerful tool (e.g. Perl).

Last edited by radoulov; 06-10-2009 at 05:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

2. Shell Programming and Scripting

Adding a blank line in between two O/Ps in tabular format which gets received over email

Hi Guys, I am stuck in between and seeking help here. Requirement: A script that will run every morning which will connect to Mysql database and run the query to inform us about the holidays (it will also check if there were any holidays during last 2 business days). So the three queries are... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

3. Shell Programming and Scripting

sed one Liner inverse range match-help required

cat test.txt a c d e g (2 Replies)
Discussion started by: TomG
2 Replies

4. 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

5. Shell Programming and Scripting

sed adding a blank line

I use the following as part of a script to correct for a faulty hostname file. # get the domain name read -r thehostname < /etc/hostname dom="$(echo $thehostname | cut -d'.' -f2)" numchar=${#dom} if then echo "It appears as though the hostname is not correctly set." echo "Hostname has... (5 Replies)
Discussion started by: bugeye
5 Replies

6. Shell Programming and Scripting

Adding a blank line after every 5th line

Hello... I have a file which contain certain number of records. I want to generate another file from this file which will contain 1st line as a blank line & after every 5 lines one blank line will be inserted. How to achieve this through shell scripting? Thanks... (5 Replies)
Discussion started by: 46019
5 Replies

7. Shell Programming and Scripting

Awk script to match pattern till blank line

Hi, I need to match lines after a pattern, upto the first blank line. Searched in web and some forums but coulnt find the answer. where <restart_step> = 10 -- Execute query 20 -- Write the contents to the Oracle table 30 -- Writing Contents to OUTPUT... (7 Replies)
Discussion started by: justchill
7 Replies

8. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

9. Shell Programming and Scripting

Need help in sed command (adding a blank line btw each block generated by pattern)

Hello friends, I have a C source code containing sql statements. I use the following sed command to print all the sql blocks in the source code.... sed -n "/exec sql/,/;/p" Sample.cpp The above sed command will print the sql blocks based on the pattern "exec sql" & ";"... (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. UNIX for Dummies Questions & Answers

adding blank line in egrep

I followed the egrep example given in the thread "parse text or complex grep ?". It is exactly what I need...except... how do I insert a blank line after the second line? My exact command is: egrep 'patt1|patt2' filename the result is: patt1 patt2 patt1 patt2 and so on. I would... (2 Replies)
Discussion started by: antalexi
2 Replies
Login or Register to Ask a Question