0

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 ^.$ 
4
  • Please provide an example, what result you are expecting Commented Jan 21, 2015 at 5:53
  • 1
    file names or file contents ? grep for contents, find for names (or ls | grep depending what you want to do) Commented Jan 21, 2015 at 5:58
  • Im using ls | grep for finding files in usr/cont directory. Not sure how to combine the syntax. Commented Jan 21, 2015 at 6:03
  • I'm expecting a list of file names to show Commented Jan 21, 2015 at 6:03

4 Answers 4

1

Use this pattern:

grep "^[ey]...$" 

Alternatively, use the range syntax:

grep "^[ey].\\{3\\}$" 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I knew the first part made sense but didn't realize you needed to specify more dots for the range.
When i get 15 reputation I surely will :)
Sorry I thought I could accept all answers so logically I would accept the first. I really appreciate your contribution.
1

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.

Comments

0

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.

Comments

0

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*" \) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.