我想解析 mysources.list以提取存储库列表。我有:
## Some comment
deb http://some.vendor.com/ubuntu precise stable
deb-src http://some.vendor.com/ubuntu precise stable
deb http://some.othervendor.com/ubuntu precise experimental # my current favorite
Run Code Online (Sandbox Code Playgroud)
我想要:
http://some.vendor.com/ubuntu precise stable
http://some.othervendor.com/ubuntu precise experimental
Run Code Online (Sandbox Code Playgroud)
所以我需要: 只在行首到行尾或一个#字符带有“deb”的行,但不包括它。到目前为止,我有:
grep -o "^deb .*"
Run Code Online (Sandbox Code Playgroud)
但是如何在#不匹配 的情况下匹配或结束行#?
小智 7
使用grep:
grep -Po '(?<=^deb\s).*?(?=#|$)' inputFiles
Run Code Online (Sandbox Code Playgroud)
grep -Po '(?<=^deb\s)[^#]*' inputFiles
Run Code Online (Sandbox Code Playgroud)
使用sed:
sed -nr '/^deb\s/s;^deb\s([^#]*)#?.*$;\1;p' inputFiles
Run Code Online (Sandbox Code Playgroud)
使用awk(此解决方案基于固定字段的数量):
awk '/^deb /{print $2,$3,$4}' inputFiles
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5332 次 |
| 最近记录: |