El Dude
El Dude

Reputation: 5618

grep with variable numbers of whitespaces

I want to grep for a string which I know exists in my files. However, the source it comes from managed to change the number of whitespaces so that the content per se is identical in the string, but the length differs. => ordinary grep does not find it. Is there a way to adjust for it?

I dont's see a system behind the additional whitespace effect

Here's the original string

4FD0-A tr|A5ZLA0|A5ZLA0_9BACE Bacterial 

and here's the modified string

4FD0-A    tr|A5ZLA0|A5ZLA0_9BACE Bacterial

Upvotes: 0

Views: 1594

Answers (3)

Walter A
Walter A

Reputation: 20032

You can use all kind of utilities for deleting spaces.
Try this:

cat file |tr -s '\ ' | grep '4FD0-A tr|A5ZLA0|A5ZLA0_9BACE Bacterial'

Upvotes: 0

aethercowboy
aethercowboy

Reputation: 444

I believe that egrep would be your friend here. Try the following command:

egrep  '4FD0-A\s+tr[|]A5ZLA0[|]A5ZLA0_9BACE\s+Bacterial'  filename

I used a rather simple pattern for my example. Feel free to change it to suit.

Upvotes: 1

Stephen P
Stephen P

Reputation: 14820

You can use the "one or more" + operator. For example

grep '^4FD0-A\s\+tr|' myfile

The \s searches for any whitespace. If you want to limit it to only spaces just use a single space in place of the \s

Upvotes: 0

Related Questions