Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 08-20-2013, 03:39 PM   #1
LQ Newbie
Registered: Jun 2013
Posts: 29

Rep: Reputation: Disabled
change inside a file without opening

I have a shell file which text is

for a in 5.80 5.82 5.90 5.94 5.98 6.00 6.02 6.06 6.1
sed 's/5.85/$a/g' old_file > new_file

Basically I want to replace 5.85 by 5.80 then 5.82 and so on.
But in new_file it replace 5.85 by $a, that means $a does not work in 's/5.85/$a/g'.
How do I fix this problem?

Thank you
Old 08-20-2013, 04:11 PM   #2
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,964

Rep: Reputation: 539Reputation: 539Reputation: 539Reputation: 539Reputation: 539Reputation: 539
use double quotes, and protect a with {}

sed "s/5.85/${a}/g"

and don't forget ;do done
for a in ....;do
   sed ..

Last edited by Firerat; 08-20-2013 at 04:15 PM. Reason: ;do .. done
1 members found this post helpful.
Old 08-20-2013, 11:12 PM   #3
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
sed 's/5.85/$a/g' old_file > new_file
That does not work because surrounded by '----' and a is not protected so when parsing the line the shell interprets $a as it is without substituting the value.

Solution is already provided by @Firerat.
1 members found this post helpful.
Old 08-21-2013, 03:44 AM   #4
LQ Newbie
Registered: Jun 2013
Posts: 29

Original Poster
Rep: Reputation: Disabled
Thanks Firerat for answer
Thanks SAbhi for explanation.


command line, perl, sed, sed bash, shell script

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
[SOLVED] How to change default file path when opening or saving a file bassplayer69 Slackware 4 11-18-2011 05:27 PM
problem opening a file from inside program. avee137 Programming 2 09-23-2011 08:36 AM
mount ISO file and change files inside cccc Debian 4 02-02-2011 03:57 AM
emacs + auctex - opening output PDF inside emacs buffer coenvh Linux - Newbie 1 04-29-2010 05:52 AM
How do i change spaces inside a txt file, and replace space with backslash \? repolona Linux - Newbie 1 05-24-2006 05:08 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:06 AM.

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