Hi guys,
How to add a line before a specific line (identified with the starting work ex: xxx) of a file and write it back to the same file?
Thanks
Hi guys,
How to add a line before a specific line (identified with the starting work ex: xxx) of a file and write it back to the same file?
Thanks
Do you mean add a blank line?
awk ' /^xxx/ {print ""}
NR>0 {print $0} ' oldfile > tmpfile
mv tmpfile oldfile
sed '/^xxx/i xy/' urfile
if your sed support -i, then you can use it to write it back to the same file.
sed -i '/^xxx/i xy/' urfile
doesn't work
So, are we supposed to guess what does not work? What exactly did you try?
Try:
sed -i '/^xxx/{x;s/^/Hi I am a newline/p;x}' file
the line im inserting has characters >> and a parametarized path which looks like ${DIR}/${FILE}. could that be the reason its not working?
sed '/^xxx/i New line' file1 > tmp; mv tmp file1
cheers,
Devaraj Takhellambam
ok i found out the issue. the problem is the first word(xxx) of the line im searching for starts in the middle of the line. any solution for this?
Thanks
I don't think so:
$ printf 'www\nxxx\nyyy\nzzz\n'|sed '/^xxx/i the line im inserting has characters >> and a parametarized path which looks like ${DIR}/${FILE}.'
www
the line im inserting has characters >> and a parametarized path which looks like ${DIR}/${FILE}.
xxx
yyy
zzz
Did you quote properly? Does it also not work with you insert something simple like e.g. a single character?
---------- Post updated at 15:15 ---------- Previous update was at 15:08 ----------
Just leave out the caret (^). That should do it. If that is not strict enough you can use surrounding characters or characteristics to tighten the search pattern. E.g. if there are only space characters you can use:
/^[[:space:]]*XXX/
yes Scrutinizer, that was the issue. i just removed ^ and its inserting fine. could you tel me how to insert the new line with some spaces(predifined) in the begining?
Thanks
You can use a backslash:
sed '/^[[:space:]]*xxx/i \ Hello I am a new line.'