【问题标题】:Extract specific rows from a file depending on some condition ( Command Line )根据某些条件从文件中提取特定行(命令行)
【发布时间】:2016-09-29 12:49:23
【问题描述】:

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


任何帮助/建议将不胜感激。在此先感谢。

【问题讨论】:

    标签: linux shell ubuntu command-line terminal


    【解决方案1】:

    呆呆的:

     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
    

    【讨论】:

    • 但是 gensub 是要安装的外部软件,是否有外部软件执行此操作的命令
    • gensubgawk 的内置函数,不是外部工具。
    • 是的,我们必须先安装 gawk,然后才能使用 gensub
    • 检查我的编辑,只使用grepcutbash
    • 很好的解释。
    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2020-01-13
    • 2015-10-31
    • 1970-01-01
    相关资源
    最近更新 更多