LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-13-2010, 07:28 PM   #1
btacuso
Member
 
Registered: May 2009
Posts: 32

Rep: Reputation: 15
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
 
Old 03-13-2010, 07:59 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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.

Last edited by crts; 03-13-2010 at 08:02 PM.
 
Old 03-14-2010, 03:15 AM   #3
BeacoN
Member
 
Registered: Nov 2008
Distribution: Linux Mint
Posts: 56

Rep: Reputation: 17
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.
 
Old 03-14-2010, 11:51 AM   #4
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by crts View Post
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?
 
Old 03-14-2010, 12:22 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by btacuso View Post
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.
 
Old 03-14-2010, 12:22 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by btacuso View Post
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?
 
Old 03-16-2010, 01:39 AM   #7
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Xpert in 8-16 bit microcontroller embedded system now inclining toward embedded linux priyadarshan Linux - Embedded & Single-board computer 2 02-12-2009 05:28 AM
LXer: Embedded Linux as Dependable for Developers as RTOSes, says Embedded Market For LXer Syndicated Linux News 0 01-23-2008 12:40 PM
Replace multiple blanks with single blank ckoniecny Programming 1 09-05-2006 02:12 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration