Command to trim the first and last character of a line in a text file

I am looking for one liner, I hope that can trim the first and last character of a line, on multiple lines, for example. test.txt

Before:

xyyyyyyyyyyyyyyyyyyyx
pyyyyyyyyyyyyyyyyyyyz

After:

yyyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyy
+3
source share
3 answers
$ cat /tmp/txt
xyyyyyyyyyyyyyyyyyyyx
pyyyyyyyyyyyyyyyyyyyz

$ sed 's/^.\(.*\).$/\1/' /tmp/txt
yyyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyy
+7
source
sed -ne 's,^.\(.*\).$,\1,p'

This command will delete all lines with less than two characters, since you cannot delete the first and last characters from them.

0
source

There is a little trick :)

sed 's / ^. (. *). $ / \ 1 / 'file> file1; rm file; echo file1> file; rm file1

0
source

All Articles