UNIX commands question

I am using HP-UXsystem. I am unable to use the below sed commands. could you please let us know the alternate commands for it.

>sed '/unix/ c "Change line"' file.txt
 
sed '/unix/ i "Add a new line"' file.txt
 

any inputs please

Hello ramkumar15,

Assuming that your requirement is to search for a text and then substitute it with a new text and edit the input file too, if this is the exact requirement then following may help.

sed -i '/unix/s/Add a new line/' file.txt

Also if you want to change the text for all matches then following will help.

sed -i '/unix/s/Add a new line/g/' file.txt

Thanks,
R. Singh

Add a line before a match
 
sed -i '/unix/s/Add a new line/' file.txt
 
 
to Change a line
 
sed '/unix/ c "Change line"' file.txt
 

Some sed s accept above syntax, others don't. man sed :

So - you might want to try this syntax with the \ .

On HPUX systems sed does not have -i switch.

For 'instant' substitution you will have to use perl with -s -i switches, ed or a sed with temporary file.

sed "s/string/newstring/g" inputfile > inputfile_tmp && mv inputfile_tmp inputfile

i and a and c commands need multi-line (not possible in t/csh!), where the previous line ends with a \ character:

sed '/unix/ i\
Insert a new line
' file.txt
sed '/unix/ a\
Append a new line
' file.txt
sed '/unix/ c\
Change to line1\
and line2
' file.txt

They can as well be done by the s (substitute) command.
where the c (change) with a one-line replacement can be done in one line:

sed '/unix/ s/.*/Change line/' file.txt

To write the changes back to the input file, it is most safe to overwrite it (retaining the inode/owner/permissions):

sed '...' file.txt > file.txt.new && cp file.txt.new file.txt && rm file.txt.new

or

cp file.txt file.txt.old && sed '...' file.txt.old > file.txt && rm file.txt.old