繁体   English   中英

根据某些条件从文件中提取特定行(命令行)

[英]Extract specific rows from a file depending on some condition ( Command Line )

我在文件(实际上是一个很大的文件)中有一些内容,如下所示,有什么办法可以通过命令行 提取具有lastAccessed值大于特定值(例如1464682814617)的

"url":"https://www.google.co.in/","title":"Google","lastAccessed":1464675219253,"hidden":false,""
"url":"https://www.google.com/intl/en/mail/help/about.html","title":"Gmail - Free Storage and Email from Google","persist":true,"lastAccessed":1464679910117,"hidden":false
"url":"https://www.facebook.com/","title":"Facebook - Log In or Sign Up","persist":true,"lastAccessed":1464682240507,"hidden":false
"url":"https://www.linkedin.com/","title":"World’s Largest Professional Network | LinkedIn","lastAccessed":1464682814617,"hidden":false,""
"url":"http://stackoverflow.com/","title":"Stack Overflow","persist":true,"lastAccessed":1464682191245,"hidden":false
"url":"http://www.indeed.co.in/?r=us","title":"Job Search India | one search. all jobs. Indeed","docIdentifier":5,"persist":true,"lastAccessed":1464674503732
"url":"https://www.google.com/intl/en/mail/help/about.html","title":"Gmail - Free Storage and Email from Google","persist":true,"lastAccessed":1464674739300,"hidden":false
"url":"http://stackoverflow.com/","title":"Stack Overflow","persist":true,"lastAccessed":1464674774653,"hidden":false

附带说明:我正在开发节点应用程序。 通过命令行执行操作会更快,或者将其转换为json obj,然后寻找正确的记录?


任何帮助/建议将不胜感激。

使用gawk:

 awk '{if ( gensub(/.*lastAccessed":([0-9]*).*/,"\\1","g",$0) > 1464682814617) {print}}' File

gensub将提取字符串"lastAccessed":之后的数字"lastAccessed":并将其与极限值进行比较,如果值大于极限值,则打印行。

如果awksed不可用:

while read line; do 
    LASTA=$(echo "$line"| grep -o '"lastAccessed":[0-9]*'  | cut -d: -f2) ;
    if [ "$LASTA" -gt 1464682814617 ] ; then
        echo  $line
    fi
done < File

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM