【问题标题】:Compare a string from a csv file againt a lookup file in unix shell scripting将 csv 文件中的字符串与 unix shell 脚本中的查找文件进行比较
【发布时间】:2017-06-11 17:27:32
【问题描述】:

我是 unix 脚本的新手。我有一个要求,我需要根据另一个 csv 文件中的一组字符串检查 csv 文件中的字符串。我的 file1 将具有如下值, 文件1

task desc
1    network error in there in line one.
2    data error is there in line three.
3    connection is missing from device.
4    new error is there

file2 将有标准的查找数据 文件2

Network error
data error
connection is 

file2 是静态的,我的file1 每天都在变化。我的要求是检查file1 中的每一行并检查file2 中的字符串。如果file1 中有任何新的错误条目,则需要将整行写入一个名为file3 的新文件中。这种从日志文件中检查的日常标准错误并报告新错误。例如,在我上面的示例中,输出 file3 需要如下所示。

文件3

4    new error is there

【问题讨论】:

  • grep 是你的朋友。了解其选项-v-i-f

标签: bash shell csv unix


【解决方案1】:

tail + grep 解决方案:

tail -n +2 file1 | grep -ivf file2 > file3

> cat file3
4    new error is there

  • tail -n +2 file1 - 从第 2 行开始输出 file1 的最后一部分

grep 选项:

  • -f - 从文件中获取模式

  • -i - 不区分大小写的匹配

  • -v - 反转匹配的意义,选择不匹配的行

【讨论】:

    猜你喜欢
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    相关资源
    最近更新 更多