Regex - search and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex - search and replace
# 1  
Old 11-27-2013
Regex - search and replace

I have file which contains data in the following format all in a single line:
Code:
BDW_PUBLN_ID DECIMAL(18:0) NOT NULL PRIMARY INDEX ARGO_ACCT_DEP_PI ( OFC_ID ,CSHBX_ID ,TRXN_SEQ_NUM ,PROCG_DT ) PARTITION BY RANGE_N(PROCG_DT  BETWEEN DATE '2012-03-01' AND DATE '2014-12-31' EACH INTERVAL '1' MONTH );

I need to replace the COMMA within regex PRIMARY INDEX ABC (Q ,W,E ,R, T) with colon ':' and remove and extra spaces and tab's within the parentheses.

so output should be like :

Code:
BDW_PUBLN_ID DECIMAL(18:0) NOT NULL PRIMARY INDEX ARGO_ACCT_DEP_PI (OFC_ID:CSHBX_ID:TRXN_SEQ_NUM:PROCG_DT ) PARTITION BY RANGE_N(PROCG_DT  BETWEEN DATE '2012-03-01' AND DATE '2014-12-31' EACH INTERVAL '1' MONTH );

# 2  
Old 11-27-2013
Not sure if this is the most brilliant piece of code ever, but try:
Code:
awk      'match ($0, / PRIMARY INDEX[^(]*\([^)]*\)/)    {B=RSTART;L=RLENGTH
                                                         split (substr($0, B, L), T, "(")
                                                         gsub(/[        ]*/,"", T[2])
                                                         gsub(/,/,":", T[2])
                                                         print substr ($0,1,B-1) T[1] "(" T[2], substr ($0, B+L+1)}
        ' file
BDW_PUBLN_ID DECIMAL(18:0) NOT NULL PRIMARY INDEX ARGO_ACCT_DEP_PI (OFC_ID:CSHBX_ID:TRXN_SEQ_NUM:PROCG_DT) PARTITION ...

# 3  
Old 11-27-2013
Try this:
Code:
 perl -pe 's/(.*)\(\s(\w+)\s,(\w+)\s,(\w+)\s,(\w+\s)\)/$1($2:$3:$4:$5)/' file

It gives the desired output.

Code:
BDW_PUBLN_ID DECIMAL(18:0) NOT NULL PRIMARY INDEX ARGO_ACCT_DEP_PI (OFC_ID:CSHBX_ID:TRXN_SEQ_NUM:PROCG_DT ) PARTITION BY RANGE_N(PROCG_DT  BETWEEN DATE '2012-03-01' AND DATE '2014-12-31' EACH INTERVAL '1' MONTH )

I am not sure, you want to retain the white space in the end inside the parenthesis
Code:
(OFC_ID:CSHBX_ID:TRXN_SEQ_NUM:PROCG_DT )

If you dont want to retain the whitespace, you can try:
Code:
 perl -pe 's/(.*)\(\s(\w+)\s,(\w+)\s,(\w+)\s,(\w+)\s\)/$1($2:$3:$4:$5)/' file

# 4  
Old 11-27-2013
I dont need to retain the last white space.

The challenge is that the number of columns/words is variable. In the example i stated there are four, but it can vary typically from 1 - 10 or even more in some cases.
# 5  
Old 11-28-2013
The following awk script seems to do what you want:
Code:
awk '
{       # Set s to the offset of the 1st character after the opening parenthesis.
        match($0, /^.*PRIMARY INDEX[^(]*[(]/)
        s = RLENGTH + 1
        # Set e to the offset of the closing parenthesis.
        match($0, /^.*PRIMARY INDEX[^)]*[)]/)
        e = RLENGTH
        # Set b to the string that is between the parentheses.
        b = substr($0, s, e - s)
        gsub(/ /, "", b)        # Get rid of spaces in b.
        gsub(/,/, ":", b)       # Change commas in b to colons.
        # Print the updated line.
        printf("%s%s%s\n", substr($0, 1, s - 1), b, substr($0, e))
}' file

With your sample input, it produces:
Code:
BDW_PUBLN_ID DECIMAL(18:0) NOT NULL PRIMARY INDEX ARGO_ACCT_DEP_PI (OFC_ID:CSHBX_ID:TRXN_SEQ_NUM:PROCG_DT) PARTITION BY RANGE_N(PROCG_DT  BETWEEN DATE '2012-03-01' AND DATE '2014-12-31' EACH INTERVAL '1' MONTH );

If you run this command on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help to use regex to do search and replace. Don't know how to and can't figure out how :(

Hi, Below is an excerpt from a 20000+ lines and I want to do a search and replace of a specific string but I don't know how and I can't figure out how to. Can't find an example from Google or anywhere to do what I am wanting to do. A 2018-11-21 08:42:17 TEST_TEST 2018-11-21... (9 Replies)
Discussion started by: newbie_01
9 Replies

2. Shell Programming and Scripting

Search Replace Specific Column using RegEx

Have Pipe Delimited File: > BRYAN BAKER|4/4/2015|518 VIRGINIA AVE|TEST > JOE BAXTER|3/30/2015|2233 MockingBird RD|ROW2On 3rd column where the address is located, I want to add a space after every numeric value - basically doing a "s//&\ / ": > BRYAN BAKER|4/4/2015|5 1 8 VIRGINIA AVE|TEST > JOE... (5 Replies)
Discussion started by: svn
5 Replies

3. Shell Programming and Scripting

Multi line regex for search and replace

I have text file like below: a.txt Server=abc Run=1 Time=120.123 Tables=10 Sessions=16 Time=380.123 Version=1.1 Jobs=5 Server=abc Run=2 Time=160.123 Tables=15 Sessions=16 Time=400.258 Version=2.0 (1 Reply)
Discussion started by: sol_nov
1 Replies

4. Shell Programming and Scripting

Perl:Regex for Search and Replace that has a flexible match

Hi, I'm trying to match the front and back of a sequence. It works when there is an exact match (obviously), but I need the regex to be more flexible. When we get strings of nucleotides sometimes their prefixes and suffixes aren't exact matches. Sometimes there will be an extra letter and... (2 Replies)
Discussion started by: jdilts
2 Replies

5. Shell Programming and Scripting

Regex:search/replace but not for escaped character

Hi Input: - -- --- ---- aa-bb-cc aa--bb--cc aa---bb---cc aa----bb----cc Output: . - -. -- aa.bb.cc (7 Replies)
Discussion started by: chitech
7 Replies

6. Emergency UNIX and Linux Support

search replace regex question

Hi, I need to run a search and replace on a large database, what I need to change is all instances of #### (eg. 1764 or 1964) to (####) (eg. (1764) or (1964)) But there might be other numbers in there such as (1764) and I do not need those changed to ((1764)) How can I... (7 Replies)
Discussion started by: lawstudent
7 Replies

7. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

8. Shell Programming and Scripting

How do I search with regex in one spot?

Hello im new here and i shot stright with question. Mainly i wanna ask , how do i search with regexp in one spot and show the whole thing, what im trying to ask is , for eg. i do ls -l, and i see all the info for the dirs and dats. now say i wanna get all the dats that in their name they start... (2 Replies)
Discussion started by: Goroner
2 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question