Dan
Dan

Reputation: 129

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

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

Before:

xyyyyyyyyyyyyyyyyyyyx
pyyyyyyyyyyyyyyyyyyyz

After:

yyyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyy

Upvotes: 5

Views: 12367

Answers (3)

backtrack1010
backtrack1010

Reputation: 11

There is little trick :)

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

Upvotes: 1

Roland Illig
Roland Illig

Reputation: 41625

sed -ne 's,^.\(.*\).$,\1,p'

This command will delete all lines that have less than two characters, since one cannot really strip the first and last character from them.

Upvotes: 0

regularfry
regularfry

Reputation: 3268

$ cat /tmp/txt
xyyyyyyyyyyyyyyyyyyyx
pyyyyyyyyyyyyyyyyyyyz

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

Upvotes: 14

Related Questions