Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sed

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
like image 667
Dan Avatar asked Jun 03 '11 16:06

Dan


2 Answers

$ cat /tmp/txt
xyyyyyyyyyyyyyyyyyyyx
pyyyyyyyyyyyyyyyyyyyz

$ sed 's/^.\(.*\).$/\1/' /tmp/txt
yyyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyy
like image 147
regularfry Avatar answered Sep 19 '22 15:09

regularfry


There is little trick :)

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

like image 42
backtrack1010 Avatar answered Sep 23 '22 15:09

backtrack1010