LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to replace expressions with embedded blanks (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-replace-expressions-with-embedded-blanks-795216/)

btacuso 03-13-2010 06:28 PM

How to replace expressions with embedded blanks
 
I need to edit myfile in a script.I tried items below but it did not change anything. Thanks again.

myfile:
141 aaaa bb Jun 4'09su Jun15'09mo yyyy
206 vvv nn Jun 4'09su Jun16'09tu tt
208 bb yyyy Jun 4'09su Jun15'09mo vvvvvvvv

result:
141 aaaa bb Jun04'09su Jun15'09mo yyyy
206 vvv nn Jun04'09su Jun16'09tu tt
208 bb yyyy Jun04'09su Jun15'09mo vvvvvvvv

pdate="Jun 4"
pdate1=Jun04

sed 's/$pdate/$pdate1' myfile
or
sed 's/"$pdate"/$pdate1' myfile

crts 03-13-2010 06:59 PM

Code:

sed "s/$pdate/$pdate1/g" myfilep
I did not test it though.

EDIT:
What do you mean it did not change anything? Your result is different. If you want to make that changes permanent in your file then use the -i option or redirect output to another file.

BeacoN 03-14-2010 01:15 AM

yeah-if you want to edit the file "in place" add the -i, otherwise sed will just print the output and leave the file as-is.

btacuso 03-14-2010 10:51 AM

Quote:

Originally Posted by crts (Post 3897265)
Code:

sed "s/$pdate/$pdate1/g" myfilep
I did not test it though.

EDIT:
What do you mean it did not change anything? Your result is different. If you want to make that changes permanent in your file then use the -i option or redirect output to another file.

I tried your suggestion and still did not work. Btw, I also did my testing by directing output to a file although I feel I did not need to because I can see it print on the screen. Any other ideas?

crts 03-14-2010 11:22 AM

Quote:

Originally Posted by btacuso (Post 3897844)
I tried your suggestion and still did not work. Btw, I also did my testing by directing output to a file although I feel I did not need to because I can see it print on the screen. Any other ideas?

Your output in your first post suggests that 'Jun 4' was indeed changed to 'Jun04', as expected. So right now I do not really understand what you are trying to accomplish.

If printing on the screen is sufficient then you do not need the -i option.

catkin 03-14-2010 11:22 AM

Quote:

Originally Posted by btacuso (Post 3897844)
I tried your suggestion and still did not work.

Works for me:
Code:

c@CW8:~$ echo '141 aaaa bb Jun 4'09su Jun15'09mo yyyy
> 206 vvv nn Jun 4'09su Jun16'09tu tt
> 208 bb yyyy Jun 4'09su Jun15'09mo vvvvvvvv' > /tmp/trash
c@CW8:~$ pdate="Jun 4"
c@CW8:~$ pdate1=Jun04
c@CW8:~$ sed "s/$pdate/$pdate1/g" /tmp/trash
141 aaaa bb Jun0409su Jun1509mo yyyy
206 vvv nn Jun0409su Jun1609tu tt
208 bb yyyy Jun0409su Jun1509mo vvvvvvvv

Are you 100% sure your test was valid?

btacuso 03-16-2010 12:39 AM

Quote:

Originally Posted by catkin (Post 3897862)
Works for me:
Code:

c@CW8:~$ echo '141 aaaa bb Jun 4'09su Jun15'09mo yyyy
> 206 vvv nn Jun 4'09su Jun16'09tu tt
> 208 bb yyyy Jun 4'09su Jun15'09mo vvvvvvvv' > /tmp/trash
c@CW8:~$ pdate="Jun 4"
c@CW8:~$ pdate1=Jun04
c@CW8:~$ sed "s/$pdate/$pdate1/g" /tmp/trash
141 aaaa bb Jun0409su Jun1509mo yyyy
206 vvv nn Jun0409su Jun1609tu tt
208 bb yyyy Jun0409su Jun1509mo vvvvvvvv

Are you 100% sure your test was valid?

Yes, now it worked the nth time. Thank you for being persistent.


All times are GMT -5. The time now is 06:36 PM.