What syntax to use with sed c\

I know that I want to entirely replace line 3 in my file filename.txt. I have tried all sorts of variations of

sed 3,3,c\replacement stuff\ filename.txt with no success.

about the only thing that causes any reaction is

sed 3,3c\\ filename.txt
but it just prints out the whole file.

Your help would be much appreciated. I would like to run this from the command line and to run from within a shell script.

The text I actually want to put in line 3 is .run login.txt; just in case there are any peculiarities with dealing with any of those characters.

Many Thanks,

Susan

Did you try

sed 3c"replacement txt" filename.txt

I have now - the response was
sed: 0602-404 Function 3creplacement txt cannot be parsed.

I put your script

>sed 3c"replacement txt" filename.txt

directly into the command line

Is there another alternative?

Regards,

Susan

Try

sed '3c "replacement txt"' filename.txt

I get
sed: 0602-404 Function 3c "replacement txt" cannot be parsed.

for sed '3c "replacement txt"' filename.txt

Regards,

Susan

How about

sed '3c\
replacement txt' filename.txt

Note that you have to enter a newline after the backslash.

That's great - it works a treat. Thank you very much.

All the best,

Susan

Pludi, why does that happen?

No clue. I just went by the description of the 'c' command in the man page, where it's indicated as

c \
text

as opposed to

s/regexp/replacement/

and tried it on one of our HP-UX machines (where sed complained if the newline wasn't there)
My guess would be that this was originally intended to preserve any whitespace at the start of the replacement text that otherwise might be ignored by the tokenizer.

Sorry to be a pain but I now want to use a variable instead of a line number in my script and I am getting nowhere fast with combinations of " and ' and `.

What would be the correct way to write

sed '$vLineNumberc\
replacement txt' $vfilename

(not forgetting the essential line return after the \)

Hopefully,

Susan

-----

Change each single quote to double quotes. Shell doesn't do variable substitution inside single quotes. Also, changing $vLineNumber to ${vLineNumber} can help.

HTH

I tried
sed "${vLineNumber}c\
.run logon.txt" "$vScriptName" > $vScriptName2

and received this message

sed: 0602-404 Function 3c.run logon.txt cannot be parsed.

vLineNumber is successfully translating to 3 but it seems as if the line return is not being recognised anymore - or am I on the wrong track?

Without the {} the message is
sed: 0602-403 .run logon.txt is not a recognized function. So {} definitely seems necessary.

Hope you can help,

Susan

An extra \ does the job.

sed "${vLineNumber}c\\
replacement text" "$vScriptName" > $vScriptName2

I got the idea from Re: Illegal variable name & carriage return

Thanks to everyone for their help.

Bye,

Susan