我有一个文件包含:
Georgia
Unix
Google
期望的输出是:
Georg
Un
Goog
答案1
sed 's/..$//' < input > output
答案2
这shell Parameter Expansion
并使用子字符串删除${parameter%word}
/子字符串扩展${parameter:offset:length}
语法。
"${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}"
答案3
使用grep
前瞻选项 (PCRE):
grep -Po '.*(?=..$)'