关于一般文本的增删改查,我们可以看看:https://www.yuque.com/mugpeng/linux/plfwh1
但这些主要是用来处理列表上的操作。(其实够强大它们也是可以的)
就像R 中的dplyr 和stringr 一样,是比较专门的用来两种操作的方式。
这里学习一下linux 字符串的一些操作,参考:https://www.cnblogs.com/gaochsh/p/6901809.html
1)expr 命令
expr 不仅可以用来进行简单数学运算,还可以用来计算字符串长度、抓取字符串、抓取第一个字符数字串出现的位置:
# 长度
expr length "this is a test" # mac 中无法使用
14
> expr substr “this is a test” 3 5
is is
> expr index "sarasara" a
2
2)利用变量的#、%、/ 查看长度、替换、删除
a="this is a test"
echo ${#a} # 计算字符a 的长度
14
${a#*is} # 从左最短删除任意出现is 的字符
$ echo ${a#*is}
is a test
${a##*is} # 从左最长删除任意出现is 的字符
${a##*is}
a test
${a%is*} # 从右最长删除任意出现is 的字符
echo ${a%is*}
this
${a%%is*} # 从右最长删除任意出现is 的字符
echo ${a%%is*}
th
${a/is/at} # 将从左到右的第一个is 字符替换为at
echo ${a/is/at}
that is a test
${a/is/IS} # 将所有的is 字符替换为IS
$ echo ${a//is/IS}
thIS IS a test
{a:0:10} # 切片,从0 到10,包头包尾
$echo {a:0:10}
this is a
3)其他截取字符串的方式
# 截取1,8 字符
1. awk substr()
$ echo “this is a test” |awk '{print substr($0,1,8)}'
this is
2. cut -c # 以字符为单位截取
$ echo $a | cut -c1-8
this is