14

I've got a file containing:

Georgia
Unix
Google

The desired output is:

Georg
Un
Goog
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Sir Vic
  • 181

3 Answers3

19
sed 's/..$//' < input > output
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
1

The shell Parameter Expansion and using Substring removal ${parameter%word}/Substring Expansion ${parameter:offset:length} syntax.

"${line%??}"    # strip the shortest suffix pattern match the word 
"${line::-2}"   # strip last 2 characters (`bash 4.2` and above)
"${line: : -2}" # in older you could add spaces between OR
"${line::${#line}-2}"
αғsнιη
  • 41,407
  • "${line: : -2}" wouldn't work in older versions either. ${line:0:${#line}-2} would and also work in ksh93 (where that ${var:offset:length} syntax comes from) and recent versions of zsh. See also $line[1,-3] in zsh. – Stéphane Chazelas Aug 17 '17 at 07:36
0

Using grep and look-ahead option (PCRE):

grep -Po '.*(?=..$)'
αғsнιη
  • 41,407