Greg Lance
Greg Lance

Reputation: 67

Using grep to list lines starting with

I am trying to Use the GREP command to list all lines which contain the letter 'd' at least twice in a row (that is, next to each other), from file "test". But i can't get it to work. Can someone please help?

I can't display what I want. And am not sure how this works. I tried grep [dd] but that didn't work.

This is extremely confusing for me

Upvotes: 5

Views: 13798

Answers (3)

Alex North-Keys
Alex North-Keys

Reputation: 4363

Your grep [dd] was specifying any line with a character from the set (set = []) containing "d" and "d". So simply putting them side-by-side without the square brackets provides the method for all lines containing a "d" with another "d" right after it.

cat yourfile | grep dd
grep dd yourfile
grep dd <yourfile

should all work, assuming you replace "yourfile" with your file's name.

If you really were looking for lines starting with two or more "d"s, using Sagar's "^dd" instead of just "dd". Similarly, you could use "dd$" for lines ending with two or more "d"s.

You might try "man regexp" or http://www.zytrax.com/tech/web/regex.htm for some more info.

Upvotes: 1

Rishabh Sagar
Rishabh Sagar

Reputation: 1814

In regex:

^ character means, start of data line. all characters match themselves.

So, if you are trying to find lines starting with 'dd', try:

grep ^dd <your_file>

However if you just interested in finding out lines with 'dd' pattern, try:

grep dd <your_file>

Upvotes: 7

JonasVautherin
JonasVautherin

Reputation: 8033

The following should make it:

grep dd <your_file>

Upvotes: 1

Related Questions