json文本如下(保存在 goods.txt 中):
{"name":"蓝牙耳机","type":"goods"}
{"name":"无线蓝牙耳机","type":"goods"}
{"name":"耳机","type":"goods"}
{"name":"头戴式耳机","type":"goods"}
{"name":"手机","type":"goods"}
{"name":"运动耳机","type":"goods"}
{"name":"跑步耳机","type":"goods"}
需求:
从以上文本中,抽取出包含耳机
的文本
sed命令如下:
cat goods.txt | sed -e $'s/,/\\\n/g' | sed -e $'s/:/\\\n/g' | grep "耳机" |sed 's/"//g'
解析:
命令 | 含义 |
---|---|
cat goods.txt | 查看 goods.txt 的内容 |
sed -e $'s/,/\\\n/g' | 把逗号 替换为换行 |
sed -e $'s/:/\\\n/g' | 把冒号 替换为换行 |
grep "耳机" | 查找包含”耳机“的内容 |
sed 's/"//g' | 把双引号 替换为空 |