【问题标题】:Awk using shell variable to grep string [duplicate]awk使用shell变量来grep字符串[重复]
【发布时间】:2019-09-13 06:30:45
【问题描述】:

我在下面尝试过,但即使字符串模式不匹配,它也会打印多行。有什么不对吗?

我有输入文件infile1.txt,其中包含字符串。 我有一个包含1000fixfile.txt 的固定文件,其中存在字符串。

我想根据要求找出几个字符串。

cat infile1.txt

Iam_happy
excited.abouttrip
Travelling is passion

猫修复文件.txt

John said iam_happy today
Tom was excited.abouttrip
Chick is delicious
I love cartoon
.... 
.
.

预期输出

John said iam_happy today
Tom was excited.abouttrip

Shell 脚本已尝试

while read line
do
grep $line /home/fixfile.txt > output1.txt
done < infile1.txt

【问题讨论】:

  • 没有看到示例几乎不可能正确理解它,请在您的帖子中添加输入示例和预期输出。
  • 你看过-f选项到grep吗?您的循环不是必需的。 grep(1) - Linux manual page
  • 为更好理解而编辑

标签: shell awk grep


【解决方案1】:
grep -Fif infile1.txt fixfile.txt > output1.txt

F 用于匹配固定字符串,
i 用于不区分大小写匹配,
f 用于指定模式文件。

【讨论】:

    猜你喜欢
    • 2016-05-07
    • 2020-03-23
    • 2021-07-05
    • 2019-03-23
    • 2019-12-25
    • 2013-01-26
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多