记一次awk 、sed、egrep截取处理文本字段的经历
程序员文章站
2022-05-14 07:49:44
...
群里面有个老哥,提出了个需求:筛选name=“中间的内容”
<project name="airliner/vendor/ts/prebuilt/Iap/Iapprotocol" path="lagvm/LINUX/android/vendor/ts/prebuilt/Iap/Iapprotocol" remote="prebuilt" revision="728e84ef2864978add6e323f3179fd5154c5d820" upstream="26.26.X.verify"/>
<project name="airliner/vendor/ts/prebuilt/Iap/include" path="lagvm/LINUX/android/vendor/ts/prebuilt/Iap/include" revision="2274f1d98c6881debeeedb74036a18be8ee987dc" upstream="bayes"/>
<project name="airliner/vendor/ts/prebuilt/bt/BluetoothService" path="lagvm/LINUX/android/vendor/ts/prebuilt/bt/BluetoothService" remote="prebuilt" revision="d664206ec028fd2353c0316c819e26f3efce9f81" upstream="26.26.X.0.0.0.11"/>
<project groups="src" name="airliner/vendor/ts/proprietary/AudioManagement/CarAudioExtService/plugin-impl" path="lagvm/LINUX/android/vendor/ts/proprietary/AudioManagement/CarAudioExtService/plugin-impl" revision="ee90c9d587dfe0d5c3088947d54e60431b194fd2" upstream="cauchy"/>
<project groups="src" name="airliner/vendor/ts/proprietary/AudioManagement/CarAudioExtService/service" path="lagvm/LINUX/android/vendor/ts/proprietary/AudioManagement/CarAudioExtService/service" revision="7cff3690e3c9029e3da1892ec4b89d196423bff5" upstream="cauchy"/>
<project groups="ref" name="airliner/vendor/ts/proprietary/AudioManagement/ReferenceCode/AudioDemo" path="lagvm/LINUX/android/vendor/ts/proprietary/AudioManagement/ReferenceCode/AudioDemo" revision="a8ec98a923fbef59e822eef0fe833adb9e5c94af" upstream="cauchy"/>
<project groups="src" name="airliner/vendor/ts/proprietary/AudioManagement/audioext_daemon/components" path="lagvm/LINUX/android/vendor/ts/proprietary/AudioManagement/audioext_daemon/components" revision="00bca24c74aecbb724c661fb692da933829c8ac9" upstream="cauchy"/>
<project name="airliner/vendor/ts/proprietary/AudioManagement/audioext_daemon/config" path="lagvm/LINUX/android/vendor/ts/proprietary/AudioManagement/audioext_daemon/config" revision="03b88e8a52fc67afef3753be5c46061956cf0582" upstream="cauchy"/>
<project groups="src" name="airliner/vendor/ts/proprietary/AudioManagement/audioext_daemon/factory" path="lagvm/LINUX/android/vendor/ts/proprietary/AudioManagement/audioext_daemon/factory" revision="d026112dde151d034171a15fd8f5bb5a72738433" upstream="cauchy"/>
我:
在外面将 group="src" 替换为空格, 然后再awk切. (讲道理,对比下面的,我这方法有丶捞)
awk -F '"' '{print $2}' ceshi.txt
下面列出群大佬给的解答方法:
方法 ① 先切 name=" , 然后再分
awk -F 'name="' '{print $2}' 11.sh | awk -F '"' '{print $1}'
方法② (egrep -o 相当于grep -Eo) 筛选后,再切
egrep -o name=".*" ceshi.txt | awk '{print $1}'
或
egrep -o name=".*" falcon.xml | awk -F '"' '{print $2}'
群里巨佬: (看不懂巨佬的,各种正则骚操作)
① grep
grep -Po '(?i)(?m)(?<=name\=")[^"]*(?="?)'
② sed
sed -r 's/.*name=\"(.*)(\".*path.*)/\1/g' text
③ awk
awk '{split($0,arr,"\"");if($2~/groups/)print arr[4];else print arr[2]}'
Linux三剑客真滴顶
END