osx - How to grep multiple terms and output in the order of the search? -
i have tried few things, don't seem making progress - have text file lines of data, , want lines of data file. each line has unique identifier can grep.
if use
grep 'name1\|name2\|name3\|name4' file.txt > newfile.txt
it job , greps desired lines want, however, want lines in order in specified - example want name1 lines first, name2 lines, name3 lines , name4 lines.
however, example in original file order of lines name2, followed name4, followed name3, followed name1, output file seems have lines in order.
is there way order grep easily?
the ids block-sorted, lines name1 example occur next each other.
thanks advice!
use loop read words file
given file of words grep such following:
root lp syslog nobody
you can use read loop repeatedly grep fixed strings in file. example, using bash shell's default reply variable , word file stored in /tmp
directory, work:
while read; grep --fixed-strings "$reply" /etc/passwd done < /tmp/words
notes
- the posted example won't prevent multiple matches, will ensure matches made in order defined in
/tmp/words
. - the example uses gnu grep , fixed strings performance reasons. mileage may vary other greps , regular expressions.
Comments
Post a Comment