【问题标题】:extract strings pattern from file in Linux [duplicate]从Linux中的文件中提取字符串模式[重复]
【发布时间】:2015-09-03 09:48:51
【问题描述】:

我想从文件中提取字符串模式。我的输入文件如下所示:

Name = Apple Is Red
Hidden = True
Name = Banana Is Yellow
Hidden = False
Name = Orange Is Orange
Hidden = True
Name = Guava Is Green
Hidden = False

我想要在输出中创建两个文件,一个是 true.log 和 false.log:

true.log

Name = Apple is Red
Hidden = True
Name = Orange Is Orange
Hidden = True

false.log

Name = Banana Is Yellow
Hidden = False
Name = Guava Is Green
Hidden = False

bash 脚本和 Linux 的新手。

【问题讨论】:

  • Name = blah blah(换行) Hidden = True(换行) Name = foo foo(换行) Hidden = False(换行)文件是这种格式

标签: linux bash shell scripting


【解决方案1】:

简单的grep-B(在上下文之前)

grep -B1 --no-group-separator 'Hidden = True' myfile

(--no-group-separator 所以没有多余的连字符)

【讨论】:

    【解决方案2】:

    试试

    apple=$(cut inputfile -d"=" -f2) #what stands after the = into a variable apple
    if [ $apple = "Apple Is Red" ] ; then
        echo "Hidden = true" >> true.txt
    fi
    

    编辑:sry 我的连接挂断了

    【讨论】:

    • 它不起作用,伙计
    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 2017-11-26
    • 2020-10-25
    • 1970-01-01
    • 2020-11-02
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多