Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text file editing, with 'sed'

Status
Not open for further replies.

cgswong

MIS
Nov 27, 2000
202
US
Hi all,

I have a text file which is just a journal to be filled in by the operator after a backup, when it's printed. I want to write a script which will either insert a '*', or the host name (using environemnt variable HOST) in a certain row/column; or edit the journal file replacing "( ) Home" with "(*) Home", or "Server: "with "Server: $HOST" where $HOST is an environemnt variable. I have been trying to use 'sed', but without much success.

I don't have much (very little really :) ) experience with sed. The following is the command I use and the error message I keep getting:

# sed -e '15,s/( ) Home/(*) Home' /tmp/bkuplog.msg.bak
sed: command garbled: 15,s/( ) Home/(*) Home
#

Does anyone have any ideas or perhaps a script already written which can help me. Any help is appreciated, thanks.

- Stuart
 
Try this:
sed -e 's/()Home/(*)Home/' -e "s/SERVER:/SERVER:$HOST/" file

Pay attention to ' and ". This makes a big difference.
 
Try this:

sed -e "15,s/()Home/(\*)Home;15,s/SERVER:/SERVER:$HOST/" file

all special char must be escaped with a $HOST will be replace with variable if in "



Tony ... aka chgwhat
tony_b@technologist.com

When in doubt,,, Power out...
 
I've tried both these ideas and I still get the error message "command garbled". I even simplified it to many various variations of both, and still the same message. Any other ideas? This thing is now officially frustrating.

Regards.

- Stuart
 
Ok, hold the phone! It does work guys :) ! I left out the last bracket, so it's

sed -e '9,s/( ) Home/(*) Home/' file

The '*' doesn't have to be delimited and spaces are allowed. Thanks again guys, I'm a happy man!

- Stuart
 
Sure, but
sed -e 's/SERVER:/SERVER:$HOST/' file
will not work :)
Actually it will of cause but you will have
"SERVER: $HOST" instead of "SERVER: My_host"
If HOST="My_host"
 
This is true, does anyone know how to get it working properly? Also, how do you insert/append using 'sed'? I've tried using a\ and i\ but get either 'command garbled' or no change to the file depending on my syntax (too embarassing to show). Please help!

- Stuart
 
does :
sed -e "15,s/()Home/(\*)Home;15,s/SERVER:/SERVER:"$HOST"/" file

work? :)
 
Sorry to get back so late, we've been having trouble with our internet service. I don't now about what you've got jad, but I got mine to work after some fiddling. You need to have the sed commands in double quotes to get the variable/OS command to work. The following works:

sed -e "15,15s/( ) Home/(*) Home/;22,22s/SERVER:/SERVER: `echo $HOST`/"

The 'echo' was left out previously as well. If I use single quotes (') it does work, so sed must have double to work (").

I do however want to know how to insert/append. I've tried a\ (append) and i\ (insert) from the man page but this doesn't work. It may need to be in a command file. Anyone have any ideas?

Thanks for all the help.

- Stuart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top