Thanks much for help, but is there any way that we can replace nth line without any conditions to check ? as your solution checking for strings and then replace, but is there any way, I can directly remove last but two lines and replace entire line as per my previous post ? what I mean remove completely last but two line before in that input file and append/replace these two lines in that place. The reason being stlib value may contain another strings also at times so wanted to remove that line and append
You seem to want to automatically update some sort of source-code. There is a specialised utility for that, it is called patch . In general it is fed a diff -output file and applies this diff to a sourcce file. This is the common way to update the source in a source-rpm to a higher version. The update will contain all the diffs which, when applied to an older version, give you the newer version. The man page of patch is rather exhaustive, so i suggest to read it carefully. If there are still questions please do not hesitate to raise them.
If you just want to replace fixed lines with a fixed text you could create a "changes-file" that might look like this:
<line-nr>|replacement-text
i.e.
25|replacement1
27|replacement2
and feed it the following script, which just an infinitely dumb version of patch :
#! /bin/ksh
typeset fIn="/some/input/file"
typeset fOut="/some/output/file"
typeset fChanges="/file/with/the/changes/as/mentioned"
typeset -i iLine=0
typeset chLine=""
typeset fTmp="/tmp/myscript.$$"
cp "$fIn" "$fTmp"
while IFS="|" read iLine chLine ; do
if sed "$iLine"' s/.*/'"$chLine"'/' "$fTmp" > "$fOut" ; then
cp "$fOut" "$fTmp"
else
exit 1
fi
done < "$fChanges"
exit 0
Notice that in this case the replacement text must not contain regexes, i.e. backreferences like \1