Replace a word After a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a word After a string.
# 1  
Old 07-28-2010
Question Replace a word After a string.

Hello Gurus,

I have a file which has foll contents scattered:
Code:
,TotUnasgndNetAmt     
FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht
WHERE
CalDayRunDt = '2010-07-21'
UNION ALL
SELECT 
CalDayRunDt             
,BusWkCd
,'N'

I want to replace 2010-07-21 that starts after ' and ends before ' with a variable called $curr_date. There is also an N which starts after ' and ends before ' but I want to replace only the date field. It will be always in the format YYYY-MM-DD so it will be 10 characters long (if this helps).


Thank you in advance.

Last edited by Scott; 07-28-2010 at 07:21 PM.. Reason: Please use code tags
# 2  
Old 07-28-2010
Try:
Code:
awk -vx=$curr_date '{sub("....-..-..",x,$0)}1' file

These 2 Users Gave Thanks to bartus11 For This Post:
# 3  
Old 07-28-2010
Code:
$ cat file1
,TotUnasgndNetAmt
FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht
WHERE
CalDayRunDt = '2010-07-21'
UNION ALL
SELECT
CalDayRunDt
,BusWkCd
,'N'


$ curdate=something
$ sed "s/[0-9][^']*/$curdate/" file1
,TotUnasgndNetAmt
FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht
WHERE
CalDayRunDt = 'something'
UNION ALL
SELECT
CalDayRunDt
,BusWkCd
,'N'

These 2 Users Gave Thanks to Scott For This Post:
# 4  
Old 07-28-2010
Thank You Scottn and Bartus11 - both solutions work perfect...!!!
# 5  
Old 07-28-2010
Great Smilie

But I imagine it's CalDayRunDt that you explicitly wanted to change.

Code:
$ sed "/CalDayRunDt/ s/'.*/'$curdate'/" file1
,TotUnasgndNetAmt
FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht
WHERE
CalDayRunDt = 'something'
UNION ALL
SELECT
CalDayRunDt
,BusWkCd
,'N'

This User Gave Thanks to Scott For This Post:
# 6  
Old 08-02-2010
REOPEN - Replace a word After a string (Scottn and Bartus11)

Hello,

The solution was working fine until i had following in my input file:

Soln By Scottn:
Code:
awk -vx=2010-06-28 '{sub("....-..-..",x,$0)}1' vwdef_MoSumSnpsht1_tmp

the $0 got replaced with the .ksh script name I was running dont know why??

I did the following

Code:
awk -vx=2010-06-28 '{sub("....-..-..",x)}1' vwdef_MoSumSnpsht1_tmp

and it worked
Could you please explain the difference?

Soln By Bartus11:
----------------

Code:
sed "s/[0-9][^']*/$curdate/" file1

Input file contained a field as

Code:
Replace view Test1
,TotUnasgndNetAmt
FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht
WHERE
CalDayRunDt = '2010-07-21'
UNION ALL
SELECT
CalDayRunDt
,BusWkCd
,'N'

which got changed to
Code:
Replace view Test2010-07-28
,TotUnasgndNetAmt
FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht
WHERE
CalDayRunDt = '2010-07-28'
UNION ALL
SELECT
CalDayRunDt
,BusWkCd
,'N'

I was expecting "Replace view Test1" to stay as it is I only want to replace characters that are enclosed in single quotes and start and end with numbers or rather it will always be date in format yyyy-mm-dd.

e.g '2010-06-21' should be replaced by '2010-07-28'

Code:
sed "s/*\'[0-9][^']*/2010-06-28/"

works fine but it replaces other numbers in the file as well.

Thank you once again.

Last edited by Scott; 08-02-2010 at 07:43 PM.. Reason: Please use code tags
# 7  
Old 08-02-2010
Hi.

You got a bit mixed up when you said "Soln By" (the wrong way round).

Code:
sed "s/'[0-9][^']*/'$curdate/" file1

If you read back:

Quote:
But I imagine it's CalDayRunDt that you explicitly wanted to change.
Code:
sed "/CalDayRunDt/ s/'.*/'$curdate'/" file1


Last edited by Scott; 08-02-2010 at 08:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

4. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

5. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

6. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies

7. Shell Programming and Scripting

Replace first word after string INITIAL

Hi I have a file with hundreds of lines, some of the lines have word INITIAL followed by some numbers like ....INITIAL 1234535 .... ....INITIAL 5768644 .... I would like to replace the number after word INITLA with 4K how can I do it? Cant get my heard around this! The string is always... (3 Replies)
Discussion started by: halacil
3 Replies

8. UNIX for Dummies Questions & Answers

Seach for part of string and replace whole word

I am trying to find words in a text with a certain ending with sed and replace them with themselves but wrapped in tabs ex.: The fish swims in the water. -> searching for -ms ending The fish <tab>swims<tab>in the water. I've been trying all sorts of commands and get either an error... (5 Replies)
Discussion started by: stinnes
5 Replies

9. Shell Programming and Scripting

Replace a word from a string

How can i replace a particular word from string i.e. var="shiv_dutt_para_shar" wrd="para" rep_wrd="PARA" what i am trying to do that first i'll search if $var catains #wrd or not. if it contains then i've to replace $wrd with $rep_wrd. I have tried following #!/bin/sh t="shiv... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

10. Shell Programming and Scripting

Replace a word with string from another file

Hi, this belongs a little to my other post but only at the starting point. With find -name "*.htm*" i got a list like this: ./1999/01/file1.html ./1999/01/file2.html ./1999/02/file1.html ./2000/04/file1.html ./2000/04/file2.html ./2000/04/file3.html ./2000/file1.html... (2 Replies)
Discussion started by: Tonda
2 Replies
Login or Register to Ask a Question