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 :(
# 8  
Old 12-06-2018
Thanks, that works fine on Solaris

--- Post updated 12-06-18 at 06:45 AM ---

Hi Don

I did try on Solaris what you suggested in post #4 and it works as I want it to be.

If you don't mind, got some more question to ask playing around with BRE/regex.

Here's another one that I am wanting to do:

Below is the test file file:

Code:
$: cat file1
/db/abcp/DIR/abcp_data_01.dbf

Now I want to replace abcp to abct and I use the following which works fine like I wanted it to:

Code:
$: sed 's#\(/db/abc\)p\(/DIR/abc\)p\(_.*\.dbf\)#\1t\2t\3#' file > file1
$: cat file1
/db/abct/DIR/abct_data_01.dbf

I also try the following in case I only want to replace only where /db is the beginning of the line:

Code:
$: cat file2
/db/abcp/DIR/abcp_data_01.dbf
   /db/abcp/DIR/abcp_data_02.dbf
$: sed 's#^\(/db/abc\)p\(/DIR/abc\)p\(_.*\.dbf\)#\1t\2t\3#' file2 > file1
$: cat file1
/db/abct/DIR/abct_data_01.dbf
   /db/abcp/DIR/abcp_data_02.dbf

OK, it's all good so far. But I thought the previous two options are too 'complicated' so I then try the one below but it is not giving me the result that I wanted. Can you tell me what's wrong with it?

Code:
$: sed 's#\(/db/abcp/DIR/abcp_\)\(_.*\.dbf\)#\(/db/abct/DIR/abct_\)\1#' file > file1
$: cat file1
/db/abcp/DIR/abcp_data_01.dbf

I am expecting file1 to be having /db/abct/DIR/abct_data_01.dbf as well shouldn't it be Smilie

You may be thinking why don't I just simply do 's#abcp#abct#g' instead of having to do sed 's#\(/db/abc\)p\(/DIR/abc\)p\(_.*\.dbf\)#\1t\2t\3#' file > file1. This is because I may have a line that is /db/abcp/DIR/abcp_abcp_data_01.dbf and that string is supposed to be replaced to /db/abct/DIR/abct_abcp_data_01.dbf. Hence I don't want to do a global replace.
# 9  
Old 12-06-2018
Quote:
Originally Posted by newbie_01
...
Can you tell me what's wrong with it?

[CODE]$: sed 's#\(/db/abcp/DIR/abcp_\)\(_.*\.dbf\)#\(/db/abct/DIR/abct_\)\1#' file > file1
...

There are several errors in your attempt.
- the regex looks for strings with two adjacent _ in them; there is none in the input.
- parentheses lose their special meaning in the replacement and will be printed as such.
- the leading part of the string seems to be (almost) duplicated by the replacement constant (with the t char) and the back reference \1.
- the trailing part is lost as the back reference \2 is not used.


Try to correct those and come back with the result.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 12-07-2018
Hi RudiC

I think I get what you mean. Need to get the hang of trying to understand these regex.
I tested as below.
It gives the output that am after.
Will try with the real file that I am wanting to change.

Code:
$: head -20 file*
==> file1 <==
/db/abcp/DIR/abcp_data_01.dbf

==> file2 <==
/db/abcp/DIR/abcp_data_01.dbf
   /db/abcp/DIR/abcp_data_02.dbf

==> file3 <==
/db/abcp/DIR/abcp_data_01.dbf
/db/abcp/DIR/abcp_data_01.dbf txt
   /db/abcp/DIR/abcp_data_02.dbf
$: sed 's#^\(/db/abcp/DIR/abcp\)\(_.*\.dbf\)$#/db/abct/DIR/abct\2#' file1 > file
$: cat file
/db/abct/DIR/abct_data_01.dbf
$: sed 's#^\(/db/abcp/DIR/abcp\)\(_.*\.dbf\)$#/db/abct/DIR/abct\2#' file2 > file
$: cat file
/db/abct/DIR/abct_data_01.dbf
   /db/abcp/DIR/abcp_data_02.dbf
$: sed 's#^\(/db/abcp/DIR/abcp\)\(_.*\.dbf\)$#/db/abct/DIR/abct\2#' file3 > file
$: cat file
/db/abct/DIR/abct_data_01.dbf
/db/abcp/DIR/abcp_data_01.dbf txt
   /db/abcp/DIR/abcp_data_02.dbf

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