Replacing a string in nth line a file Replacing a string in nth line a file unix unix

Replacing a string in nth line a file


This might work for you:

sed -i '2s/hi/Hello/' file


Above answer is correct, but I am tempted to put AWK variant just for reference.

awk 'NR==2{gsub("hi","Hello",$1)}; {print $0}' file > newfile