1、已知如下命令结果:
[[email protected] ~]# echo "I am oldboy myqq is 48939492" >>oldboy.txt
[[email protected] ~]# cat oldboy.txt
I am oldboy myqq is 48939492
现在需要从文件中过滤“oldboy”和“48939492”字符串
解答:
方法1:
[[email protected] ~]# awk -F " " ‘{print $3,$6}‘ oldboy.txt -->F:指定分隔符 oldboy 48939492
方法2:
[[email protected] ~]# cut -d" " -f3,6 oldboy.txt -->-d选项是:指定分隔符 oldboy 48939492
方法3:
[[email protected] ~]# cut -c 6-11,20- oldboy.txt -->使用cut的-c选项过滤出来,根据字符数匹配。 oldboy 48939492
方法4:
[[email protected] ~]# echo `grep -oE "oldboy|48939492" oldboy.txt` -->-E:代表使用扩展正则表达式 oldboy 48939492
或者:
[[email protected] ~]# echo `egrep -o "oldboy|48939492" oldboy.txt` -->grep -E等于 egrep oldboy 48939492
方法5:
[[email protected] ~]# sed -r ‘s#.*(oldboy).*(48939492)#\1 \2#g‘ oldboy.txt -->使用sed后相引用,-r代表使用扩展正则表达式 oldboy 48939492 或者: [[email protected] ~]# sed ‘s#.*\(oldboy\).*\(48939492\)#\1 \2#g‘ oldboy.txt oldboy 48939492
2、已知如下命令结果:
[[email protected] ~]# cat oldboy.txt
I am oldboy,myqq is 48939492
现在需要从文件中过滤“oldboy”和“48939492”字符串
解答:
方法1:
[[email protected] ~]# cut -c 6-11,20- oldboy.txt oldboy 48939492
方法2:
[[email protected] ~]# sed ‘s#,# #g‘ oldboy.txt|cut -d " " -f3,6 oldboy 48939492
方法3:
[[email protected] ~]# awk -F "[ ,]" ‘{print $3,$6}‘ oldboy.txt -->这里使用多个分隔符 oldboy 48939492
方法4:
[[email protected] ~]# echo `grep -oE "oldboy|48939492" oldboy.txt` oldboy 48939492 或者: [[email protected] ~]# echo `egrep -o "oldboy|48939492" oldboy.txt` oldboy 48939492
方法5:
[[email protected] ~]# sed ‘s#.*\(oldboy\).*\(48939492\)#\1 \2#g‘ oldboy.txt oldboy 48939492 或者: [[email protected] ~]# sed -r ‘s#.*(oldboy).*(48939492)#\1 \2#g‘ oldboy.txt oldboy 48939492
时间: 2024-10-17 08:47:58