sed a multiple line pattern that has newlines followed by text and r


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed a multiple line pattern that has newlines followed by text and r
# 1  
Old 10-10-2015
sed a multiple line pattern that has newlines followed by text and r

Here is the text that I was to run sed on. In this text I want to insert a semi colon ';' before 'select a13.STORE_TYPE STORE_TYPE,' and after 'from ZZMR00 pa11'

Input text:

Code:
insert into ZZMQ01
select  pa11.STATE_NBR  STATE_NBR,
        pa11.STORE_TYPE  STORE_TYPE,
        pa11.WEEK_ID  WEEK_ID
from    ZZMR00  pa11



select  a13.STORE_TYPE  STORE_TYPE,
        max(a16.TYPE_DESC)  TYPE_DESC,
        a14.WEEK_ID  WEEK_ID,
        max(a17.WEEK_DESC)  WEEK_DESC,
        sum(a11.CLE_SLS_QTY)  WJXBFS1
from    STORE_DIVISION  a11
        join    LOOKUP_STORE    a12
          on    (a11.STORE_NBR = a12.STORE_NBR)
        join    RELATE_STORE_TYPE       a13
          on    (a11.STORE_NBR = a13.STORE_NBR)
        join    LOOKUP_DAY      a14
          on    (a11.CUR_TRN_DT = a14.CUR_TRN_DT)
        join    ZZMQ01  pa15
          on    (a12.STATE_NBR = pa15.STATE_NBR and
        a13.STORE_TYPE = pa15.STORE_TYPE and
        a14.WEEK_ID = pa15.WEEK_ID)
        join    LOOKUP_TYPE     a16
          on    (a13.STORE_TYPE = a16.STORE_TYPE)
        join    LOOKUP_WEEK     a17
          on    (a14.WEEK_ID = a17.WEEK_ID)
where   (a13.STORE_TYPE in (1, 2)
 and a14.MONTH_ID = 199411)
group by        a13.STORE_TYPE,
        a14.WEEK_ID

I want the sed to be generic like insert a semicolon if a 'select' is preceded by newlines.

The output will be

Code:
insert into ZZMQ01
select  pa11.STATE_NBR  STATE_NBR,
        pa11.STORE_TYPE  STORE_TYPE,
        pa11.WEEK_ID  WEEK_ID
from    ZZMR00  pa11

;  # Notice the semi colon that I want to insert here. This doesn't exist in the input text.

select  a13.STORE_TYPE  STORE_TYPE,
        max(a16.TYPE_DESC)  TYPE_DESC,
        a14.WEEK_ID  WEEK_ID,
        max(a17.WEEK_DESC)  WEEK_DESC,
        sum(a11.CLE_SLS_QTY)  WJXBFS1
from    STORE_DIVISION  a11
        join    LOOKUP_STORE    a12
          on    (a11.STORE_NBR = a12.STORE_NBR)
        join    RELATE_STORE_TYPE       a13
          on    (a11.STORE_NBR = a13.STORE_NBR)
        join    LOOKUP_DAY      a14
          on    (a11.CUR_TRN_DT = a14.CUR_TRN_DT)
        join    ZZMQ01  pa15
          on    (a12.STATE_NBR = pa15.STATE_NBR and
        a13.STORE_TYPE = pa15.STORE_TYPE and
        a14.WEEK_ID = pa15.WEEK_ID)
        join    LOOKUP_TYPE     a16
          on    (a13.STORE_TYPE = a16.STORE_TYPE)
        join    LOOKUP_WEEK     a17
          on    (a14.WEEK_ID = a17.WEEK_ID)
where   (a13.STORE_TYPE in (1, 2)
 and a14.MONTH_ID = 199411)
group by        a13.STORE_TYPE,
        a14.WEEK_ID


Last edited by Don Cragun; 10-10-2015 at 03:39 AM.. Reason: Add CODE tags.
# 2  
Old 10-10-2015
Insert a line with a semicolon before each select statement
Code:
sed '/^select/i\
;' input

# 3  
Old 10-10-2015
@ MadeInGermany: That wouldn't work to satisfaction:
Code:
insert into ZZMQ01
;
select  pa11.STATE_NBR  STATE_NBR,

Try
Code:
awk '
/select/ &&
 NL && !SC      {print ";"}
                {NL=(NF==0)
                }
! NL            {SC=gsub (/;[   ]*$/, "&")
                }
1
' file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-10-2015
Another option:
Code:
awk '$1=="select"{print ";"}1' RS= ORS='\n\n'  file

# 5  
Old 10-11-2015
@ Scrutinizer: that would insert a semicolon even if the former statement were terminated with one.
# 6  
Old 10-11-2015
Hi RudiC,

I responded to this specification by the OP
Quote:
I want the sed to be generic like insert a semicolon if a 'select' is preceded by newlines
So yes, but I interpreted it to be desired behavior..
# 7  
Old 10-11-2015
You are absolutely right. I was reading too much into that spec. So, there may be two consecutive semicolons - no idea what the SQL interpreter will do with it. My SQL days are long gone...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

2. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

3. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

sed multiple multi line blocks of text containing pattern

Hi, I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id. The files are large at several Gb. My... (3 Replies)
Discussion started by: andyatit
3 Replies

6. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

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

8. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

9. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

10. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies
Login or Register to Ask a Question