Anda dapat menyalurkan output dari perintah grep pertama ke perintah grep lain dan itu akan cocok dengan kedua pola. Jadi, Anda dapat melakukan sesuatu seperti:
grep <first_pattern> <file_name> | grep <second_pattern>
atau,
cat <file_name> | grep <first_pattern> | grep <second_pattern>
Contoh:
Mari kita tambahkan beberapa konten ke file kita:
$ echo "This line contains lemon." > test_grep.txt
$ echo "This line contains rice." >> test_grep.txt
$ echo "This line contains both lemon and rice." >> test_grep.txt
$ echo "This line doesn't contain any of them." >> test_grep.txt
$ echo "This line also contains both rice and lemon." >> test_grep.txt
Apa isi file:
$ cat test_grep.txt
This line contains lemon.
This line contains rice.
This line contains both lemon and rice.
This line doesn't contain any of them.
This line also contains both rice and lemon.
Sekarang, mari kita ambil apa yang kita inginkan:
$ grep rice test_grep.txt | grep lemon
This line contains both lemon and rice.
This line also contains both rice and lemon.
Kami hanya mendapatkan garis di mana kedua pola cocok. Anda dapat memperpanjang ini dan menyalurkan output ke perintah grep lain untuk kecocokan "DAN" lebih lanjut.