Lets say I have this input
11
12
31
40
42
90
I need to get all number lines from the file and add the |
character after each that can be used as a pattern list to run with grep -f
.
I was thinking of using xargs
but I have no idea how to use |
as a separator or put the args in the middle of a string
grep -P "(11|12|31|40|42|90)\|" o.txt
You can use
grep -E -f <(sed -E '/^[0-9]+$/!d;s/.*/&\\|/' numbers.txt) file.txt
First, get all lines that only contain digit from the numbers.txt
, append to each found line a \|
, and then use this as a pattern file with -f
option to grep
. The file.txt
is a file where you are looking for matches.
See an online demo :
#!/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|
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.