H5W3
当前位置:H5W3 > 其他技术问题 > 正文

linux有什么命令可以提取指定的字符串? 如何操作

有个日志文件如下:

// demo.log
"time":"2017","id":"AA2342ds34234","content":"asdfasdfafwerfasdf"
"time":"2017","content":"asdfasdfafwerfasdf","id":"AAd234s234234"
"time":"2017","id":"AA23as423d4234","content":"asdfasdfafwerfasdf"
"id":"BB234234234","content":"asdfasdfafwerfasdf","time":"2017"

我想只输出id的值:
例如这样

AA2342ds34234
AAd234s234234
AA23as423d4234
BB234234234

有类似功能的的命令有awk, gawk,sed, grep等,看了半天也没找到我想要的这种功能的示例。
请问有什么命令可以提取,麻烦给个例子,谢谢

回答:

grep -Po '(?<="id":")[^"]+' demo.log
awk -F'[,:]' '{gsub(/"/,"");for(n=0;n++<NF;)if($n=="id")print $++n}' demo.log
sed 's/.*"id":"\([^"]\+\)".*/\1/' demo.log

回答:

cat demo.log  | sed 's/.*"id":"\([0-9]*\)".*/\1/'

回答:

javascript@linux:~/test$ cat demo.log
"time":"2017","id":"234234234","content":"asdfasdfafwerfasdf"
"time":"2017","content":"asdfasdfafwerfasdf","id":"234234234"
"time":"2017","id":"234234234","content":"asdfasdfafwerfasdf"
"id":"234234234","content":"asdfasdfafwerfasdf","time":"2017"
javascript@linux:~/test$ sed -r 's/.*"id":"([0-9]+)".*/\1/g' demo.log
234234234
234234234
234234234
234234234

回答:

awk是个比较经典的文本分析工具,支持二次编程,我做数据采集是经常使用它!
云南十大名贵野生菌

本文地址:H5W3 » linux有什么命令可以提取指定的字符串? 如何操作

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址