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


 
Thread Tools Search this Thread
Top Forums 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 :(
# 1  
Old 12-05-2018
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.

Code:
A                    2018-11-21 08:42:17 TEST_TEST 2018-11-21 00:50:45 Accessed:   2,146,893,824 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/temp/abcp_temp_10.dbf
A                    2018-11-21 08:41:52 TEST_TEST 2018-11-21 00:50:45 Accessed:   2,146,697,216 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/temp/abcp_temp_9.dbf
A                    2018-11-21 03:29:04 TEST_TEST 2017-06-17 22:01:33 Accessed:          22,528 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/LTS/abcp_ABCD_TEST_COMM_P201406_tts_exp.dta
A                    2018-11-21 03:28:57 TEST_TEST 2017-06-17 22:01:28 Accessed:     563,101,696 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/LTS/abcp_abcd_test_comm_201406_data_01.dbf

I need to search for all lines that contain the string /db/LTS/abcp_ but only those that ends in .dbf. So from the excerpts above, it should find the line below:

Code:
A                    2018-11-21 03:28:57 TEST_TEST 2017-06-17 22:01:28 Accessed:     563,101,696 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/LTS/abcp_abcd_test_comm_201406_data_01.dbf

And I want to replace them so it so that it will be

Code:
A                    2018-11-21 03:28:57 TEST_TEST 2017-06-17 22:01:28 Accessed:     563,101,696 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/LTS/abct_abcd_test_comm_201406_data_01.dbf

So, basically I need to change /db/LTS/abcp_ to /db/LTS/abct_. It is the one in BOLD that I am wanting to search and replace.
The grep regex below seems to do what I wanted. But I don't know how to do like a sed search and replace thing Smilie

Code:
$: grep "/db/LTS/abcp_.*dbf$" xx.txt
A                    2018-11-21 03:28:57 TEST_TEST 2017-06-17 22:01:28 Accessed:     563,101,696 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/LTS/abcp_abcd_test_comm_201406_data_01.dbf

Any help will be much appreciated.

P.S.:
- Is there any way to colorize or highlight certain text? I was hoping to be able to do that in this post but can't find the option to do so. I thought that option was available on the older forum ??? Smilie
# 2  
Old 12-05-2018
Try
Code:
sed -r '\#(/db/LTS/abc)p(_.*dbf)$#s//\1t\2/' file


And, yes, the text colour selector (available in the "quick reply editor") seems to be missing in the "advanced editor". But with the bold and underline you did quite well.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-05-2018
Hi RudiC

Doesn't work on Solaris but works perfectly on Linux. Thanks a lot. Not even our supposedly 'smartest' SA can figure out what to do. You're a genius.

Sorry forgot to say am trying to do this on Solaris. I have to do the sed on Linux and copy the file back to Solaris. Any idea how to go on the Solaris 8/10?

Can't quite work out or understand 100% what your code does though? Do you mind providing answers to my questions?

Code:
sed -r '\#(/db/LTS/abc)p(_.*dbf)$#s//\1t\2/' file

01 - Why are you escaping the # in the beginning?
02 - The p before the (_ is because I want to match a single p? Is that correct?
03 - The #s//\1t\2/ is the one that I don't understand for the most part. Is it doing another search?

Thanks again for looking into this.
# 4  
Old 12-05-2018
The following seems to do what you want just using standard sed features:
Code:
sed 's#\(/db/LTS/abc\)p\(_.*\.dbf\)#\1t\2#' file > file.$$ &&
    cp file.$$ file &&
    rm file.$$

The \1 and \2 in the replacement string in the substitute command are back references that are replaced by the strings matched by the BREs between the escaped sets of parentheses, respectively.

Using unescaped parentheses (as in RudiC's suggestion) is a GNU extension that is not allowed in the standards. The sed -r option is not included in the standards, but is allowed as an extension.

When I was using Solaris systems, I would have done this using /usr/xpg4/bin/sed, but I don't think there is anything in the above commands that won't work with /bin/sed or /usr/bin/sed.
These 3 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-05-2018
Quote:
Originally Posted by newbie_01
Hi RudiC

Doesn't work on Solaris but works perfectly on Linux.
Where and how does it fail? Please give as detailed and complete an error message as possible.

Quote:
01 - Why are you escaping the # in the beginning?
man sed:
Quote:
Addresses.
.
.
\cregexpc
Match lines matching the regular expression regexp. The c may be any character.
I use the # here because the usual / is part of the pattern.


Quote:
02 - The p before the (_ is because I want to match a single p? Is that correct?
Yes.


Quote:
03 - The #s//\1t\2/ is the one that I don't understand for the most part. Is it doing another search?
The # is the final address delimiter. An empty regex (s//) repeats the last one encountered, here: the address regex.
# 6  
Old 12-05-2018
Hi RudiC

Thanks for the explanation. I'll have a read.

Error is as below:

Here's the test file:

Code:
$: cat xx.txt
A                    2018-11-21 08:42:17 TEST_TEST 2018-11-21 00:50:45 Accessed:   2,146,893,824 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/temp/abcp_temp_10.dbf
A                    2018-11-21 08:41:52 TEST_TEST 2018-11-21 00:50:45 Accessed:   2,146,697,216 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/temp/abcp_temp_9.dbf
A                    2018-11-21 03:29:04 TEST_TEST 2017-06-17 22:01:33 Accessed:          22,528 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/LTS/abcp_ABCD_TEST_COMM_P201406_tts_exp.dta
A                    2018-11-21 03:28:57 TEST_TEST 2017-06-17 22:01:28 Accessed:     563,101,696 /NFS_mnt/mnlNFS111/vol_abcp_db/abcp/.snapshot/mnl1ns123_vol_abcp_db_ss.0/db/LTS/abcp_abcd_test_comm_201406_data_01.dbf

Running with /bin/sed or /usr/xpg4/bin/sed:

Code:
$: sed -r '\#(/db/LTS/abc)p(_.*dbf)$#s//\1t\2/' xx.txt
sed: illegal option -- r

$: /usr/xpg4/bin/sed -r '\#(/db/LTS/abc)p(_.*dbf)$#s//\1t\2/' xx.txt
/usr/xpg4/bin/sed: illegal option -- r
Usage:  sed [-n] script [file...]
        sed [-n] [-e script]...[-f script_file]...[file...]

OS version:

Code:
$: uname -a
SunOS [hostname] 5.11 11.3 sun4v sparc sun4v

# 7  
Old 12-05-2018
Hi newbie_01,
Did you try what I suggested in post #4 in this thread (perhaps replacing file everywhere it appears with xx.txt)?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Red Hat

Centos server, had to replace the motherboard now the embedded NICs don't work

Basically what the title says. Had to replace the motherboard on an HP DL380 G6 today, of course now the embedded NICs don't work because the ifcfg-eth files have the MAC addresses for the embedded NICs from the old machine. How can I find the new/correct MAC addresses so I can edit the... (2 Replies)
Discussion started by: xdawg
2 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

Help with change significant figure to normal figure command

Hi, Below is my input file: Long list of significant figure 1.757E-4 7.51E-3 5.634E-5 . . . Desired output file: 0.0001757 0.00751 0.00005634 . . . (10 Replies)
Discussion started by: perl_beginner
10 Replies

5. Shell Programming and Scripting

Regex - search and replace

I have file which contains data in the following format all in a single line: 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 );... (4 Replies)
Discussion started by: ysvsr1
4 Replies

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

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

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

9. Programming

I don't know how to replace input char with appropriate integer

Hi guys, I asked for help on programming forums and no one didn't helped me so I ask for help here. I am playing with some tasks from my book and I can't figure where did I get wrong. From the first program I get a blank screen, program won't generate 10*10 matrix. And second problem is I... (6 Replies)
Discussion started by: solaris_user
6 Replies

10. 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
Login or Register to Ask a Question