LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-23-2012, 04:58 AM   #1
eskape
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
How to substitute a value in a text file


I have to edit a numerical value e.g. (4.14 for 3.12) that is written in a text file.

The value should be typed and then changed by the value in the text file.

The value is part of a line, the location should be found and only the value can be changed.

Any ideas?
 
Old 11-23-2012, 05:35 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Can you provide some more detail? For example, I'm guessing that you want a script that prompts the user for the new value, and then inserts that value in the file.

Will you find the right place in the file based on the actual data, or on the file position (or location on a line)?

Are you familiar with basic BASH** scripting?---If not, start here: http://tldp.org/LDP/Bash-Beginners-G...tml/index.html


**Are you in fact using Linux? If so, then BASH is the most common scripting language
 
1 members found this post helpful.
Old 11-23-2012, 05:43 AM   #3
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Just open the file in vi editor(vi filename) & use the following commands:

Esc key

:%s/4.14/3.12/g

**g=change globally.
 
Old 11-23-2012, 06:10 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Or "sed -i 's/4.14/3.12/g' filename"
 
Old 11-26-2012, 04:37 AM   #5
eskape
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
Can you provide some more detail? For example, I'm guessing that you want a script that prompts the user for the new value, and then inserts that value in the file.

Will you find the right place in the file based on the actual data, or on the file position (or location on a line)?

Are you familiar with basic BASH** scripting?---If not, start here: http://tldp.org/LDP/Bash-Beginners-G...tml/index.html


**Are you in fact using Linux? If so, then BASH is the most common scripting language
Yes, I should have been more specific.

I am using Linux and Bash scripting, just starting.

I now the location in the file but I can't refer to this with sed i '27s/.../.../g' because the value I want to change won't be the same each time.

I would need to read a value, store it and be able to substitute the previous value.
 
Old 11-26-2012, 05:04 AM   #6
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
You should post an example and explain in more detail what you want to achieve. i.e. which values will be substituted and where comes the new value from?

Markus
 
Old 11-26-2012, 07:28 AM   #7
deswarf
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Rep: Reputation: Disabled
Try using variables like
Code:
sed -i "s/kkk/$i/g" file
 
Old 11-26-2012, 07:40 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think you need to bookmark+read
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

If you're talking about interacting with the user to get the new value, read up on while loops and the 'read' cmd in those links.
 
  


Reply



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] Substitute Text from Master File dougp23 Programming 11 05-16-2010 12:20 AM
[SOLVED] BASH: how to substitute just one occurency of a pattern in a text file carolflb Linux - Newbie 8 11-19-2009 03:31 AM
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Linux - Newbie 6 10-20-2009 01:28 PM
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Programming 2 10-20-2009 10:21 AM

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

All times are GMT -5. The time now is 04:46 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
Open Source Consulting | Domain Registration