using sed to replace values...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using sed to replace values...
# 1  
Old 09-02-2009
using sed to replace values...

I have a file:

Code:
$somevar=somevalue
$anothervar=
$someothervar=45

I'd like to be able to replace the values in the file. I just don't know how exactly to use sed. I was thinking along the lines of:

Code:
cat file | sed "s/$somevar=/anotherval/g"

I was hoping this would make the file:

Code:
$somevar=anotherval
$anothervar=
$someothervar=45


The thing is, I don't know what the values will be in this file. I wish I knew regular expressions and sed fully, but I don't.
# 2  
Old 09-03-2009
Code:
sed 's/serach_pattern/replace_pattern/g' filename

this will print the result on stdout.

Code:
sed 's/serach_pattern/replace_pattern/g' filename > tmp;mv tmp filename

will make the changes in file.
# 3  
Old 09-03-2009
If am not wrong, then the following is what you want.

Code:
sed 's/$somevar=.*$/$somevar=anotherval/' t1

To edit that file itself. ( but be careful )..
Code:
sed -i 's/$somevar=.*$/$somevar=anotherval/' t1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace hex values using sed command

File lalo.txt contains: Á I need to replace Á by A using sed command. od -x lalo.txt 0000000 c10a 0000002 sed -e 's/\xc1\x0a/A/g' lalo.txt > lalo2.txt Also tried: sed -e 's/\xc3\x81/A/g' lalo.txt > lalo2.txt Output file lalo2.txt still has Á Unix version: SunOS 5.11 ... (9 Replies)
Discussion started by: mrreds
9 Replies

2. Shell Programming and Scripting

Replace values

Gents, Can you please help with this. Input file 49955 2009 2 49957 2010 2 49959 2010 2 49961 2010 2 49963 2010 2 49789 2011 -174 49791 2011 2 49793 2011 2 49795 2011 2 49965 2011 170 49967 2011 2 49969 2011 2 49971 2011 2 49797 2012 -174 49799 2012 2 (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

Replace values

Gents, Please can you help me with this. When column 49 == 2 Need to do the following changes; Change previous row field (substr$0,45,4)-1 Change previous row field (substr$0,72,5)+2 Change actual row field (substr$0,40,4)+1 Change actual row field (substr$0,49,1)-1 Change actual... (6 Replies)
Discussion started by: jiam912
6 Replies

4. Shell Programming and Scripting

How to replace with "sed" some hex values by other hex values?

Assume I have a file \usr\home\\somedir\myfile123.txt and I want to replace all occurencies of the two (concatenated) hex values x'AD' x'A0' bytwo other (concatenated) hex values x'20' x'6E' How can I achieve this with the gnu sed tool? Additional question: Is there a way to let sed show... (1 Reply)
Discussion started by: pstein
1 Replies

5. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

6. Shell Programming and Scripting

sed replace values of column with ordered numbering

Hello, I am trying to write a shell script that will create a gnuplot file. My main problem is that I have a data file with two columns: 1.05929120E+09 5.0000701214792 1.05930096E+09 5.00006386985764 1.05930584E+09 5.00019465404908 1.05931072E+09 5.00031960589719 ... (2 Replies)
Discussion started by: pau
2 Replies

7. Shell Programming and Scripting

Replace blank values for string using sed

Hi everyone, I was wondering how could from a file where each row is separated by tabulations, the row values where are in blank replace them by a string or value. My file has this form: 26/01/09 13:45:00 0 0 26/01/09 14:00:00 1495.601318 0 26/01/09 14:15:00 1495.601318 0 ... (4 Replies)
Discussion started by: tonet
4 Replies

8. Shell Programming and Scripting

How do i use sed to replace string with values from a dictionary file

I have file 1 with one million rows. one of the fields is "FIRSTNAME" (the string) I have a second file with about 20 first names. JUDE DAVID HOMER CANE ABEL MARTY CARL SONNY STEVE BERT OSCAR MICKY JAMES JOHN GLENN DOUG (3 Replies)
Discussion started by: yoyolandre
3 Replies

9. Web Development

Replace xml values

Hallo all, I try to create a bash script but till now without any positiv results. The script should replace different variables in a text file with the right xml values Look at the following xml file: file.xml =================================== <?xml version="1.0"... (1 Reply)
Discussion started by: research3
1 Replies

10. Shell Programming and Scripting

replace the column values.

I have the below file ...where some of the column values should replaced with desired values ....below file u can find that 3 column where ever 'AAA' comes should replaced with ' CC ' NOTE : we have to pass the column number ,AAA,CC (modified value) as the parameters to the code. ... (6 Replies)
Discussion started by: charandevu
6 Replies
Login or Register to Ask a Question