[英]Create pattern list dynamically from file for running in grep -f
可以說我有這個輸入
11
12
31
40
42
90
我需要從文件中獲取所有數字行並添加|
每個字符后面的字符都可以用作與grep -f
一起運行的模式列表。
我正在考慮使用xargs
但我不知道如何使用|
作為分隔符或將 args 放在字符串的中間
grep -P "(11|12|31|40|42|90)\|" o.txt
您可以使用
grep -E -f <(sed -E '/^[0-9]+$/!d;s/.*/&\\|/' numbers.txt) file.txt
首先,從numbers.txt
中獲取所有僅包含數字的行,append 到每個找到的行 a \|
,然后將其用作帶有-f
選項的模式文件grep
。 file.txt
是您要查找匹配項的文件。
查看在線演示:
#!/bin/bash
text='Some 11 number and 11| that we need
Redundant line'
s="11
12
31
40
42
90"
grep -E -f <(sed -E '/^[0-9]+$/!d;s/.*/&\\|/' <<< "$s") <<< "$text"
# => Some 11 number and 11| that we need
grep -oE -f <(sed -E '/^[0-9]+$/!d;s/.*/&\\|/' <<< "$s") <<< "$text"
# => 11|
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.