Last updated 5 years ago
Was this helpful?
tail指令提供了获取文件最后几行数据的功能,例如 tail -10 file 则显示文件最后10行。
tail -10 file
实际上,tail加上参数-c就可以按照字符来获取最后的数据
tail
-c
echo "hello" | tail -c 5 ello
注意:获取最后4个字符使用的是-c 5是因为echo指令显示的字符串组后有隐含换行\n。如果要避免机上新行(newline character),则使用echo -n。
-c 5
echo
\n
echo -n
另外,shell支持切片方法:
someone@mypc:~$ str="A random string*"; echo "$str" A random string* someone@mypc:~$ echo "${str:$((${#str}-1)):1}" * someone@mypc:~$ echo "${str:$((${#str}-2)):1}" g
其实比较简明的是结合使用head -c和tail -c,例如要截取hello的倒数第四个字符e:
head -c
tail -c
hello
e
echo -n "hello" | tail -c 4 | head -c 1