LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack 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: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,288

Rep: Reputation: 802Reputation: 802Reputation: 802Reputation: 802Reputation: 802Reputation: 802Reputation: 802
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,970

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
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
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
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


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 04:15 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration