简体   繁体   中英

Why grep gives error for search pattern i.e. grep: <some pattern>: No such file or directory

I want to print the selected lines using grep pattern matching. I am using following command -

cat MyTest.txt | grep -v -E B1 "EEB|SET|PET"
grep: EEB|SET|PET: No such file or directory

I am always getting above grep error.

    1. I want to print the line which matches pattern or patterns I have mentioned ie EEB or SET or PET or All of these and
    1. A single line prior to matching line. hence option -B1

You can use this command without useless cat :

grep -v -E -B1 "EEB|SET|PET" MyTest.txt

Note - before B1 .

However from your description it appears you may not need -v (inverse results) and want this:

grep -E -B1 "EEB|SET|PET" MyTest.txt

Grep has the following syntax:

grep options pattern input_file_names

where options are optional, but not pattern. So B1 without "-" is used as pattern and "EEB|SET|PET" as file_names.

You should change "B1" to "-B1".

As recommendation

cat MyTest.txt | grep -v -E -B1 "EEB|SET|PET"

to

grep -v -E -B1 "EEB|SET|PET" MyTest.txt

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM