replacing line with variable

All I want to do is replace the 2nd line in a file with a variable, eg,

var=xxx

the file 'test' containing:
aaa
bbb
ccc

replace bbb with xxx

aaa
xxx
ccc

I had it working with sed on a redhat machine, but it doesn't work on a mac machine.

you can use sed or awk...

awk -v var="$your_var" 'NR==2{gsub($0,var)}{print}' filename

That is just writing to standard out, not editing the actual file. I could redirect and mv it, but the files are large and I'd like to avoid having to do that.

can be achieved using a tee

awk '{.....}' filename | tee filename

I don't have much idea on "perl"..check for more options on editing on a particular line .

This might help you for inline coding

perl -i -pe 's/bbb/xxx/i;' file_name.txt

---------- Post updated at 10:07 PM ---------- Previous update was at 10:04 PM ----------

Good Rakesh ... I was thinking there will be some alternative to "perl"..since i prefer to do it in awk :slight_smile: