gexcen
gexcen

Reputation: 31

Finding patterns in files with Unix

How do I search for lines in files in UNIX that start with e or y and contain exactly four characters in one line?

For the first part I tried

ls /usr/cont | grep ^[ey] 

I'm not sure how I am supposed to make it recognize two different letters.

For the other part I only know how to search for one exact character.

For the second part I used:

ls /usr/cont | grep ^.$

Upvotes: 0

Views: 87

Answers (4)

nagato
nagato

Reputation: 93

Although I didn't get your question completely and not enough reputation points to comment. If you want to find file with file names either start with letter "e" or "y" and have exactly four character then try "find",

find /path_to_search_for -name "????"  \( -name "y*" -o -name "e*" \)

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753725

For file names:

ls /usr/cont/[ey]???

The file name must start with e or y and consist of 4 characters in total (plus the path, of course).

For lines within files:

grep '^[ey]...$'

The line must start with e or y and consist of 4 characters in total.

Upvotes: 1

Ashita
Ashita

Reputation: 31

For an example :

gps.log and pps.log

I have tried the following and it works.

$ ls -ldtr [gp][a-z][a-z].*

You can add A-Z0-9 in the range of characters as well if your files have camel case or numeric characters in it as well.

Upvotes: 0

Alexis King
Alexis King

Reputation: 43842

Use this pattern:

grep "^[ey]...$"

Alternatively, use the range syntax:

grep "^[ey].\\{3\\}$"

Upvotes: 1

Related Questions