I have a configuration file that when a certain script runs in updates. I want to use sed and can't seem to get the syntax right.
A line from the configuration file looks like:
DATE=20040909 12:00:10
When the script is run I want to change the date and time, i.e. removing the previous parameter of 20040909 12:00:10, but keeping the DATE= part.
Can variables be added into the sed command?
How would the sed command be constructed to do the above?
Thanks in advance
I think you can, it depends the way you're using sed,
for example:
Supouse I have a Text file and I want to replace a string "Cool" for MY_Variable, I'd do
My_Variable=20040909
sed "s/$My_Variable/Cool/g" Text_File.txt
As I said it depends.
Hope it works
google
September 9, 2004, 7:06pm
3
Keep in mind that the command listed above will apply globally to your file. You can narrow down its application by using addresses.
sed "/address_1/,/address_2/s/$My_Variable/$My_Replacement/" file
Thanks for your replies.
I have some other questions:
Can sed read and write to the same file?
Or would I have to redirect the output to another file and then rename it back to its original name?
How about awk, do you think that using awk would be a better option?
Thanks in advance
sssow
September 13, 2004, 11:12am
5
You can do inline editing without using a temporary file using perl PIE
perl -p -i -e 's/old/new/' somefile
sed by nature can not read and write to the same file, in fact the main idea of sed is not to change source files, I'd rather write to a temporary file and then change anything I want.
cheers
alex blanco
according to your post above, it should work. How come my line of code below doesn't work? I'm trying to use variables as inputs to the beginning and ending line numbers to print a report.
sed -n '"$var1", "$var2"p' "$rpt" > $HOME/rpt1.tmp
lp -d "$printer" $HOME/rpt1.tmp
When I echo the var1 and var2 within the script they print the values inputted for var1 and var2, just doesn't seem to work in that line of code. Any suggessions?
The single quotes are preventing variable expansion
Try
sed -n "$var1,$var2 p" "$rpt" > $HOME/rpt1.tmp
Pay attention to quoting and spaces in my code above.
Cheers
ZB