Need help with sed to match and replace a string

friends I am struck in a situation where I need to comment a line start with space as below in a file

      root@LOCALHOST * rw
          LOCALHOST* r

I should comment second line only

Any help please

Hello mallak,

Not sure as you haven't given any complete condition, following may help you(not tested though).

awk '/^ / {$0="#"$0} 1'  Input_file

Thanks,
R. Singh

Ravinder

Thanks for your response. I didn't got the expected result.

This is what I am trying to achieve

I have a file with many lines and among them Above are two lines.

Out of which I need to write a single liner so I can comment one line

Please tell us the conditions to apply. Should it be the second line having LOCALHOST in it? The one with 9 spaces in front? The one without "root"? without "@"? "rw"?
How can we identify THAT line amongst the "many lines"?

Thanks for the response

I could see the condition as a

[space]LOCALHOST[space]*[space]r

Replace it with

#LOCALHOST[space]*[space]r

This one allows leading whitespace (spaces or tabs).
$1 (the first field in the line) must begin with LOCALHOST.

awk '$1~/^LOCALHOST/ {$0="#"$0} {print}'

---------- Post updated at 03:00 PM ---------- Previous update was at 02:52 PM ----------

Some versions of sed can recognize character classes, then you can do

sed 's/^[[:space:]]*LOCALHOST/#&/'

This translates to e.g.

awk '/^ *LOCALHOST \* r$/ {sub(/^ */,"#"} 1' file

Sure that's what you want?

EDIT: It wouldn't work on your sample above for two reasons: a) no space after LOCALHOST b) <CR> chars as line terminators

Yeah that right . Not working

So?
How about refining your spec?

Hi Rudi,

Unfortunately that file is created by IBM TSA software, so plan is to modify that as is through automation process.

Thanks

Did you consider what I said in post#9?

Sorry Rudy, I didn't get what that mean. The file in which I am trying to manuplate is generated by a software which I don't have control on how to generate the lines.

What is not working? Please be specific. Also please post the result that you got and the result that you expected..

Getting error below

 awk '/^ *LOCALHOST \* r$/ {sub(/^ */,"#"} 1' <File> 
awk: /^ *LOCALHOST \* r$/ {sub(/^ */,"#"} 1
awk: ^ syntax error

In a file i have quite a few line and two of them are as below.. I want to Comment LOCALHOST * r

root@LOCALHOST * rw
LOCALHOST * r 

There is a right parenthesis missing:

awk '/^ *LOCALHOST \* r$/ {sub(/^ */,"#")} 1'

Sorry for being so dumb.

No error now, but didn't match my requirement

It seems to work with the sample provided:

$ echo "root@LOCALHOST * rw
LOCALHOST * r" | awk '/^ *LOCALHOST \* r$/ {sub(/^ */,"#")} 1'
root@LOCALHOST * rw
#LOCALHOST * r

What result do you get?