linux - 如何使用 grep 提取第三列?
问题描述
如何使用grep
仅获取第三级 ID?
"id": 2,
"id": 12,
"id": 136,
"id": 229,
"id": 230,
"id": 231,
"id": 232,
"id": 233,
"id": 234,
我只想得到:
136
229
230
231
232
233
234
我用 grep 和 cut 尝试了不同的方法。
解决方案
使用 awk 提取数值(因为$NF+0
要删除逗号):
$ awk '/^ {10}/{print $NF+0}' file
输出:
136
229
230
...
解释:
$ awk '
/^ {10}/ { # records started by (at least) 10 spaces
print $NF+0 # print the numeric value from the last field
}' file
如果值不是数字或有 4 级 ID,则需要调整:
$ awk '
/^ {10}[^ ]/ { # starts with exactly 10 spaces
sub(/,$/,"") # remove comma from the end
print $NF # output last awk default defined field
}' file
推荐阅读
- javascript - 如果输入为空,则添加和删除类
- php - 致命错误:内存不足(已分配 377487360)(试图分配 371200000 字节)
- javascript - 如何使用函数或变量重用模板文字?
- json - HTTP post 未完成时表单被阻止
- php - 如何与关联数组值进行比较
- javascript - Jquery 在 $each 内独立迭代
- javascript - 如何触发 slideToggle(); 当访问者访问另一个页面时?
- ffmpeg - 在分段 MP4 中运行的曲目是否必须以关键帧开始?
- angularjs - 将 AngularJS 绑定升级到 Angular 2
- c++ - 使用可变参数包实例化函数模板