2

I have RHEL and i wish to run a shell script that will add several lines (over 10 lines) to the /etc/hosts file. I tried to use

sed -i "10.161.61.111 acr111" /etc/hosts 
sed -i "10.161.61.110 acr110" /etc/hosts

and so on, but i get

sed: -e expression #1, char 3: unknown command: `.'

Any idea how to fix this? Or maybe another way to run sh file which will add hosts to the hosts file? Thanks,

Cyrus
  • 77,979
  • 13
  • 71
  • 125

4 Answers4

3

Have you read the man page for sed? You're not using the -i parameter correctly.

Instead, why don't you just use:

echo "10.161.61.111 acr111" >> /etc/hosts
echo "10.161.61.110 acr110" >> /etc/hosts
Sam
  • 3,060
  • 1
  • 16
  • 22
2

Try this to append (>>) multiple lines to /etc/fstab:

cat << EOF >> /etc/fstab
10.161.61.111 acr111
10.161.61.110 acr110
10.161.61.109 acr109
10.161.61.108 acr108
EOF
Cyrus
  • 77,979
  • 13
  • 71
  • 125
2

If data comes from a file do:

cat newdata >> /etc/hosts

If data comes from a variable:

echo "$newdata" >> /etc/host
Jotne
  • 39,326
  • 11
  • 49
  • 54
2

You most include -i[SUFFIX], --in-place[=SUFFIX] as follow

sed -i "3i10.161.61.111 acr111" /etc/hosts

ni is the line number where the text will be appen

johnnymnmonic
  • 744
  • 8
  • 11