LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-20-2013, 04:39 PM   #1
hoi
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, 05:11 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

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

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


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

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

Rep: Reputation: 81
Quote:
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, 04:44 AM   #4
hoi
LQ Newbie
 
Registered: Jun 2013
Posts: 29

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


Reply

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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:02 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