0

I am trying to make a simple replacement for an IPv4 address script.

Here is my code.

#!/bin/sh

sed 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g/'

What is happening is every time I call:

example.sed example1 > example.output 

I get:

sed: -e expression #1, char 75: unknown option to `s'

where the 75th char is the 1 from 192.100.100.100.

Why?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • This is not a duplicate, but in case you were googling the error message, the usual cause is in this related question: http://stackoverflow.com/questions/9366816/sed-unknown-option-to-s – tripleee Oct 20 '15 at 08:37

1 Answers1

5

Drop the trailing slash!

sed -e \
   's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g'

(The split over two lines attempts to ensure visibility of the end of the string.)

Note that sed starts counting at the s at the start of the s/// string, not at the start of the sed word. The trailing / was at character 75 in that string.

The full script should probably add "all the command line arguments" (aka "$@") too:

sed -e \
   's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g' \
   "$@"

This will read files if they're specified (and would also accept extra editing commands such as -e 's/.*//'!) or standard input if no arguments are specified. You can put it all on one line in your script; the breaks are for improved clarity on Stack Overflow.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278