bash - Grep N times from pipe using xargs -


i have file named input contains list of wikipedia or substring of wikipedia titles. want print out lines wikipedia titles, not substring.

i have file named wikititle contains list of wikipedia titles. want grep each line input , if matches ^{string}$, want print out line.

i came below command:

cat input | xargs -0 -i{} bash -c 'grep -q -w ^{}$ wikititle && { echo {}; }' 

but gives me error of:

 xargs: command long 

how make happen? thanks!

the right way print out lines found in both of 2 files comm:

comm -12 <(sort input) <(sort wikititle) 

this vastly more efficient trying do: runs single pass, , needs store little content in memory @ time (sort can have larger memory requirements, gnu implementation supports using disk-backed temporary storage).


another more efficient approach following:

grep -f -x -f input wikititle 

...this run grep only once, using (newline-separated) strings given in input, against contents of wikititle.

using grep -f avoids treating arguments regexes, strings foo [bar] match when anchored (with wouldn't grep treated [bar] character class). using -x requires full-line matches (thank you, @tripleee!).


...and, if really wanted use xargs , whole bunch of separate grep calls , shell-level echo no reason...

<input xargs bash -c \   'for line; grep -q -f -x -e "$line" wikititle && printf '%s\n' "$line"; done' _ 

note doesn't use -i '{}', option makes xargs far less efficient (forcing run command once every single match), , introduces potential security bugs when used bash -c (if line in input file contains $(rm -rf ~), don't want execute it). instead, uses for loop in bash iterate on filenames passed arguments.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -