How to identify delimiter to find and replace a string with sed?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to identify delimiter to find and replace a string with sed?
# 1  
Old 10-29-2019
How to identify delimiter to find and replace a string with sed?

I need to find and replace a date format in a SQL script with sed. The original lines are like this:

Code:
       ep.begin_date, ep.end_date, ep.facility_code,
       AND     ep.begin_date <= '01-JUL-2019'
                     ep.begin_date, ep.end_date, ep.facility_code,
       AND     ep.begin_date <= '01-JUL-2019'
                     ep.begin_date, ep.end_date, ep.facility_code,
       AND    ep.begin_date <= '01-JUL-2019'

Then I want to replace '01-JUL-2019' with variable which real value is '01-OCT-2019'. After replacement, the line in file should be like this:
AND ep.begin_date <= '01-OCT-2019'. I am not good at how to use sed to identify delimiter or separator and help sed to find right part of this string and replace it with right value. Please help with your code. My testing code is ugly so that I do not post them. I would like to learn from all of you. Thanks for help.
# 2  
Old 10-29-2019
How specific the date regex has to be? Is it OK to match 31-FEB? Any condidtions for the year, e.g. only 20xx, or 19xx as well? Does your sed handle EREs? Try
Code:
NEWDATE=01-OCT-2019
sed -r "s/[0-3][0-9]-($(locale abmon | tr 'a-z;' 'A-Z|'))-[12][09][0-9]{2}/$NEWDATE/" file
       ep.begin_date, ep.end_date, ep.facility_code,
       AND     ep.begin_date <= '01-OCT-2019'
                     ep.begin_date, ep.end_date, ep.facility_code,
       AND     ep.begin_date <= '01-OCT-2019'
                     ep.begin_date, ep.end_date, ep.facility_code,
       AND    ep.begin_date <= '01-OCT-2019'

# 3  
Old 10-30-2019
RudiC:

Thanks for your questions and advice. My Unix is Solaris 10. It seems sed do not support EREs. Please see following output. As for date regex is concerned, this format was created by some other people and has been in production for years. I could not change it at this moment. So the best expression will still be like '01-MON-20XX' or '31-MON-20XX'. These dates are government fiscal quarter begin date and end date. It is in SQL statement. I want to use shell script to automate the change to new quarter begin date and end date for data process and report generation. You are absolutely right. $NEWDATA is variable which is queried from database and put with sed to replace old date. Here is output:

Code:
/home/oracle> sed -r "s/[0-3][0-9]-($(locale abmon | tr 'a-z;' 'A-Z|'))-[12][09][0-9]{2}/"'01-OCT-2019'"/" test4.txt
sed: illegal option -- r

/home/oracle> sed -E "s/[0-3][0-9]-($(locale abmon | tr 'a-z;' 'A-Z|'))-[12][09][0-9]{2}/"'01-OCT-2019'"/" test4.txt
sed: illegal option -- E

/home/oracle> sed -e "s/[0-3][0-9]-($(locale abmon | tr 'a-z;' 'A-Z|'))-[12][09][0-9]{2}/"'01-OCT-2019'"/" test4.txt
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'

Now question is: based on Solaris 10, how can sed handle the extended regular expressions? Please give me some idea. Thanks.
# 4  
Old 10-30-2019
how about:
Code:
/home/oracle> sed -e "s/[0-3][0-9]-($(locale abmon | tr 'a-z;' 'A-Z|'))-[12][09][0-9][0-9]/"'01-OCT-2019'"/" test4.txt

# 5  
Old 10-30-2019
vgersh99:

Thanks for input, here is the output

Code:
/home/oracle/> sed -e "s/[0-3][0-9]-($(locale abmon | tr 'a-z;' 'A-Z|'))-[12][09][0-9][0-9]/"'01-OCT-2019'"/" test4.txt
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'

# 6  
Old 10-30-2019
OK, then, try to write the months verbatim explicitly and escape parentheses, pipes, and braces:
Code:
sed  "s/[0-3][0-9]-\(JUN\|JUL\|AUG\)-[12][09][0-9]\{2\}/$NEWDATE/" file


Last edited by RudiC; 10-30-2019 at 04:29 PM..
# 7  
Old 10-30-2019
RudiC:

To write months verbatim and escape, it didn't work either. I need sed identify months because 4 quarters have 4 different begin date and 4 end date. Hard code will not work. I tried something. Please see all output below. Here, $NEWDATE=01-OCT-2019.

Code:
/home/oracle> sed  "s/[0-3][0-9]-\(JUN\|JUL\|AUG\)-[12][09][0-9]\{2\}/$NEWDATE/" test4.txt
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'

/home/oracle/> sed  "s/[0-3][0-9]\-[a-zA-Z]{3}\-[12][09][0-9]\{2\}/$NEWDATE/" test4.txt
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'
       ep.begin_date, ep.end_date, ep.facility_code,
AND    ep.begin_date <= '01-JUL-2019'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed find replace after HOST string

Hi Everyone, I have a xml file, where i need to find this HOST=dbhost.domain.com and then replace only dbhost.domain.com with db.one.in so finally it should like this HOST=db.one.in i tried this but its not working. sed -i "s/^HOST=*com$/HOST=db.one.in/g" repository.xml ^... (4 Replies)
Discussion started by: shajay12
4 Replies

2. Shell Programming and Scripting

Sed: find and replace backwards, until string

Some help please: Need to find string ||(everything in front of it)B0300|| and replace it with ||0|| globally In: 16112121||||0||0||0||0||0||52||52||0||0||0||0||1507200053342B0300||1507200053342B0300||0||0||0||0700 Out: 16112121||||0||0||0||0||0||52||52||0||0||0||0||0||0||0||0||0||0700 ... (4 Replies)
Discussion started by: drbiloukos
4 Replies

3. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

4. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

Using sed in ksh to find and replace string

Hi All, I have a file in which contains location of various data files. I want to change locations using sed. Find and replace strings are in a separate file. Content of this file (/tmp/tt) - /dd/pp/test/test/1/ /pp/aa/test/dg1/ /dd/pp/test/test/2/ /pp/aa/test/dg2/ /dd/pp/test/test/3/... (2 Replies)
Discussion started by: pandeyra
2 Replies

7. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

8. Shell Programming and Scripting

Help identify string using sed

I have the following output and would like to only identify strings with "vw" at the end. Here is the file contents: SELECT n.contract_num, n.descr, s.prj_level2_cf_val, r.descr, r.project_id, p.offering_id, o.n_cust_contract, u.name1, ' ', ' ', SUM (0), TO_CHAR (t.start_dt, 'YYYY-MM-DD'),... (6 Replies)
Discussion started by: bobroberts369
6 Replies

9. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

10. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies
Login or Register to Ask a Question