grep in several bash lines [closed]

1

I want to find several lines within a file.

If I have 3 lines I do not have any problems because I use the following to show the last line and the previous 2:

grep -B 2 "lo que busco" 

But my question is: what happens when I want to look for the first line, the middle line and the end line? This is by way of example, since they can be in any location.

Should I concatenate or use an & & amp; with a grep?

I tried for example

cat archivo.txt | grep -i "primera busqueda" | grep -i "segunda busqueda" > busqueda.txt

and like that, but it does not work for me.

    
asked by Fabian Feriks 16.04.2018 в 06:19
source

1 answer

0

If I understood correctly, what you want is an indeterminate number of lines that contain some of the content you are looking for.

To do this you must use the option -e of the grep. This option allows you to search several different items:

cat archivo.txt | grep -ie "primera busqueda" -e "segunda busqueda" > busqueda.txt

For each string you want to search you have to put a -e

When doing two pipes, you search for "second search" as a result of searching for "first line".

You can also remove the cat if you want:

grep -ie "primera busqueda" -e "segunda busqueda" archivo1.txt archivo2.txt > busqueda.txt
    
answered by 16.04.2018 в 16:22