Replace pattern matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace pattern matching
# 1  
Old 08-09-2017
Replace pattern matching

Can anyone help me with sed or awk to do a bulk replace of the below requirements.

Code:
"REC_ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (
                    START WITH +7486
                    INCREMENT BY +1
                    MINVALUE +7467
                    MAXVALUE +2147483647
                    NO CYCLE
                    CACHE 20
                    NO ORDER ) )

The above needs to be replaced with

Code:
"REC_ID" INTEGER NOT NULL

##############################################

Code:
REC_ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (
                    START WITH +241
                    INCREMENT BY +1
                    MINVALUE +222
                    MAXVALUE +2147483647
                    NO CYCLE
                    CACHE 20
                    NO ORDER ) ,
                  "SYS_START" TIMESTAMP(12) NOT NULL IMPLICITLY HIDDEN GENERATED ALWAYS AS ROW BEGIN ,
                  "SYS_END" TIMESTAMP(12) NOT NULL IMPLICITLY HIDDEN GENERATED ALWAYS AS ROW END ,
                  "TRANS_START" TIMESTAMP(12) IMPLICITLY HIDDEN GENERATED ALWAYS AS TRANSACTION START ID ,
                 PERIOD SYSTEM_TIME ("SYS_START","SYS_END") )


The above needs to replaced to

Code:
"REC_ID" INTEGER NOT NULL,
        "SYS_START" TIMESTAMP(12) NOT NULL ,
        "SYS_END" TIMESTAMP(12) NOT NULL ,
        "TRANS_START" TIMESTAMP(12) )

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-09-2017 at 06:37 PM.. Reason: Added CODE tags.
# 2  
Old 08-09-2017
Any attempts / ideas / thoughts from your side?
Why is PERIOD SYSTEM_TIME ("SYS_START","SYS_END") not found in the desired output?
# 3  
Old 08-09-2017
RudiC - My apologies for not using the code. Thanks for making it code format. The statement that you mentioned is not required for the output. It is not considered as a column in the target database. So it is not required. Appreciate for any support.
# 4  
Old 08-10-2017
I'm afraid its not too clear how to identify the partial lines / statements to be removed. While it were pretty easy to remove anything after e.g. "NOT NULL" or starting with "IMPLICITLY" or "GENERATED" I'm pretty sure someone would come back with yet another statement for which that approach would not work. On top, there seems to be an opening parethesis missing.

Please specify exactly how to identify the to be removed parts.
# 5  
Old 08-10-2017
I'm fine with after NULL till the NO ORDER.

You see a paranthesis because its part of CREATE TABLE command,
Code:
CREATE TABLE (
           Column1 int,
            Column2 int,
             Rec_id int )

This is the reason you see a paranthesis at the last.
# 6  
Old 08-10-2017
Try this as a starting point
Code:
awk '
        {while (!gsub (/[),] *$/, "&")) {getline X
                                         $0 = $0 " " X
                                        }
         sub (/ *$/, _)
         EOL = substr ($0, length)
         sub (/(GENERATED|IMPLICITLY).*$/, _)
         $0 = $0 EOL
        }
1
' file
"REC_ID" INTEGER NOT NULL )
"REC_ID" INTEGER NOT NULL ,
                  "SYS_START" TIMESTAMP(12) NOT NULL ,
                  "SYS_END" TIMESTAMP(12) NOT NULL ,
                  "TRANS_START" TIMESTAMP(12) ,
                 PERIOD SYSTEM_TIME ("SYS_START","SYS_END") ))

# 7  
Old 08-10-2017
Another one that ony looks for corresponding brackets.
Code:
awk '
match($0,/"REC_ID" INTEGER NOT NULL/) {
  print substr($0,RSTART,RLENGTH)
  cut=1; blevel=0 
}
cut==1 {
  x=$0; blevel=blevel+gsub(/[(]/,"",x)-gsub(/[)]/,"",x)
}
blevel<=0 {
  if (cut==1) { sub(/^[^)]*[)]/,""); cut=0 }
  print
}
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace String matching wildcard pattern

Hi, I know how to replace a string with another in a file. But, i wish to replace the below string pattern EncryptedPassword="{gafgfa}]\asffafsf312a" i.e EncryptedPassword="<any random string>" To EncryptedPassword="" i.e remove the random password to a empty string. Can you... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

sed - Exact pattern matching and replace

Hi Team, I am facing a problem as under, Suppose I have a file (test.txt) with the below content (all braces and slashes are included in the contents of the file) Now I want to append few words below matched line, I have written the below sed: sed '/option/a insert text here' test... (2 Replies)
Discussion started by: ankur328
2 Replies

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

4. Shell Programming and Scripting

Pattern matching and replace in shell script

Hi I want to find a line in a file which contains a word and replace the patterns. Sample file content temp.xml ==================== <applications> <application> Name="FirstService" location="http://my.website.selected/myfirstService/V1.0/myfirst.war" ... (1 Reply)
Discussion started by: sakthi.99it
1 Replies

5. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

6. Shell Programming and Scripting

Need help to replace a perl pattern matching

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (4 Replies)
Discussion started by: sags007_99
4 Replies

7. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

8. UNIX for Dummies Questions & Answers

Pattern Matching - serach and replace script

My requirement is to replace a a particular pattren in a script from A to B. I am not sure if this can be done through sed command or through awk . The file sv.inc is window DialogBox AddConnection tag "~ActiveApp/Add Connection - Provider Type?URL" I would wnat the file to be... (10 Replies)
Discussion started by: bsandeep_80
10 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question