1

Below is my text file:

example.txt
INTERFACESv4=""
INTERFACESv6=""

Below is my code (it inserts an IP address after the 14th character):

ip_add="192.168.1.2"
file_name="$HOME\Documents\example.txt"
sed -i "s/^\(.\{14\}\)/\1${ip_add4}/" "$file_name"

Below is the output of my text file:

example.txt
INTERFACESv4="192.168.1.2"
INTERFACESv6="192.168.1.2"

How do I insert the text in just the first line?

1 Answer 1

0

The minimal change would be to force the command to execute only on line 1:

sed -i "1s/^\(.\{14\}\)/\1${ip_add4}/" "$file_name"

You might consider a stricter search pattern if you only want to change the value for INTERFACESv4, no matter what line that value appears on:

sed -i "s/INTERFACESv4=.*/INTERFACESv4=${ip_add4}/" "$file_name"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .