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

时间:2016-05-31 13:16:58

标签: linux shell ubuntu command-line terminal

我在文件中有一些内容(实际上是一个巨大的文件),如下所示,有什么方法可以提取行,其中 lastAccessed value 超过某些值(例如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然后寻找正确的记录??


任何帮助/建议都会非常感谢。谢谢。

1 个答案:

答案 0 :(得分:2)

gawk:

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

gensub将在字符串"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