Linux – removing first and last character on each line / sed

bashlinuxsed

I need sed to remove first and last character of the line
for instance
source

(192.168.3.0)

result

192.168.3.0

trying this way:

sed 's/^.\(.*\).$/\1/'

but then it removes 0 character as well

how to-avoid this behavior ?

Best Answer

As others have pointed out, the sed pattern you used is not correct, and it can be fixed to remove the first and last characters correctly.

Alternatively, in your particular example, if all you really want to do is remove the parentheses (leaving only the IP address), then a simpler approach can do the job.

For example, this tr filters out all parentheses from its input:

tr -d '()'

This is equivalent to the following sed:

sed -e 's/[()]//g'

If you really want to know how to remove the first and last characters, then see the other answers.