sed commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed commands
# 1  
Old 09-09-2004
sed commands

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
# 2  
Old 09-09-2004
Hammer & Screwdriver sed

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
# 3  
Old 09-09-2004
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
# 4  
Old 09-10-2004
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
# 5  
Old 09-13-2004
You can do inline editing without using a temporary file using perl PIE

perl -p -i -e 's/old/new/' somefile
# 6  
Old 09-28-2004
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
# 7  
Old 09-28-2004
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.

Code:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looking for sed commands to add some strings

Hi All, I have one file which has contents as following. I have now to add some strings into this file. In below there is a "Description" field, and I have to look line by line and as the description contents will be finished, I have to add the string "Hello world" followed by a TAB. So before... (2 Replies)
Discussion started by: wridler
2 Replies

2. Shell Programming and Scripting

Nested sed commands

Hi, I get the following response by gphoto2 and I would like to substract the index number of the current item. In this case 3. gphoto2 --get-config /main/imgsettings/iso Label: ISO Speed Type: RADIO Current: 200 Choice: 0 100 Choice: 1 125 Choice: 2 160 Choice: 3 200 Choice: 4 250 ..... (11 Replies)
Discussion started by: Nic2015
11 Replies

3. Shell Programming and Scripting

Grouping sed commands

Hello, would you please help me with why my SED command file is outputting the entire input file instead of only the text that I'm trying to block? cat testfile O 111111111-00 DUE-DATE METHOD: FREQUENCY: O 222222222-00 DUE-DATE METHOD: FREQUENCY: O 333333333-02 DUE-DATE METHOD:... (4 Replies)
Discussion started by: lneedh1
4 Replies

4. Shell Programming and Scripting

Using commands inside sed substitution

Dear Friends, Please let me know how to use the date command inside the substitution flag replacement string. echo "01 Jan 2003:11:00:06 +0100" | sed 's/\(.*\)/`date -d \1 "+%Y%m%d%H%M%S"`/' I want to supply \1 string to Here mention below as part of replacement string, date -d <Here>... (5 Replies)
Discussion started by: tamil.pamaran
5 Replies

5. Shell Programming and Scripting

merging sed commands

Hi, I've a shell that uses two sed commands to tailor a file. sed 's/ */ /g' | sed 's/%/%%/g' Is it possible to merge this in to a single sed? Thanks! (2 Replies)
Discussion started by: dvah
2 Replies

6. Shell Programming and Scripting

Running sed commands

Hello I need to run some sed commands but it involves "/" in the substitute or delete, any ideas how I get round the problem. Example: cat file1.txt | sed -e '/</Header>/d' > file2.txt This errors due to the forward slash before the Header text. Thanks (3 Replies)
Discussion started by: Dolph
3 Replies

7. Shell Programming and Scripting

Using variables within awk/sed commands

Can I use my own variables within awk and sed for example: I've written a while loop with a counter $i and I want to use the value of $i within sed and awk to edit certain lines of text within a data file. I want to use : sed '1s/$/texthere/g' data.csv Like this: sed '$is/$/$age/g' data.csv... (5 Replies)
Discussion started by: mustaine85
5 Replies

8. Shell Programming and Scripting

Using variables in sed commands

Hi there, I need to be able to put the hostid of my box into a file (replacing the text "enter_hostid_here" so i tried sed -e 's/enter_hostid_here/`hostid`/g' inputfile > outputfile but it takes the `hostid` literally as text .....how can I get this info into the file (ideally in a single... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

9. UNIX for Dummies Questions & Answers

combining sed commands

I would like to change the lines: originalline1 originalline2 to: originalline1new originalline1newline originalline2new originalline2newline To do this, id like to combine the commands: sed 's/^/&new/g' file > newfile1 and sed '/^/ a\\ newline\\ \\ (2 Replies)
Discussion started by: Dave724001
2 Replies

10. Shell Programming and Scripting

multiple sed commands

hello! I have a few sed commands sed '/^$/d' < $1 > tmp.t sed '/^ \{3,\}/d' < tmp.t > tmp1.txt ..... how can I write them in a single line? sed '/^$/d' < $1 > | '/^ \{3,\}/d' < $1 > tmp1.txt any idea? thanks. (5 Replies)
Discussion started by: george_
5 Replies
Login or Register to Ask a Question