LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to substitute a value in a text file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-substitute-a-value-in-a-text-file-4175438412/)

eskape 11-23-2012 04:58 AM

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?

pixellany 11-23-2012 05:35 AM

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

RaviTezu 11-23-2012 05:43 AM

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

Esc key

:%s/4.14/3.12/g

**g=change globally.

allend 11-23-2012 06:10 AM

Or "sed -i 's/4.14/3.12/g' filename"

eskape 11-26-2012 04:37 AM

Quote:

Originally Posted by pixellany (Post 4835417)
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.

markush 11-26-2012 05:04 AM

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

deswarf 11-26-2012 07:28 AM

Try using variables like
Code:

sed -i "s/kkk/$i/g" file

chrism01 11-26-2012 07:40 PM

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.


All times are GMT -5. The time now is 10:55 AM.