Skip to content
Advertisement

Find lines starting with “t”, continue with a vowel and total length 4

I have a file with containing 300+ words. I need to find lines starting with “t”, continue with a vowel and total length of 4. Then i need to convert them in to a format where each line have one word.

tr -s "[[:blank:]]" "n" < file | grep .

With this I’m able to format the file but i cant figure out how can i select words with the requirement above. I’m stuck :/

i.e. i have a file that includes “terra train chair tol mourn”. i need to format this file like this:

tera  
train  
chair  
tola  
mourn

and find the ones starting with a “t” and continue with a vowel with total length of 4. so out should be like this:

tera 
tola

Advertisement

Answer

Can use grep for this. If you just need the first word from a line:

grep -Eow '^t[aeiou]S{2}' file > formatted_file

If you need to match the whole line:

grep -Eow '^t[aeiou]S{2}$' file > formatted_file
  • ^ achors the search to the start of the line.
  • t matches exactly the letter “t”.
  • [aeiou] matches any one of the characters between [ and ].
  • S{2} matches 2 non-whitespace characters
  • $ matches the end of a line
  • -w means grep will only match whole words which in effect limits your search to the exact number of characters specified in PATTERN.
  • -o means you only output the exact match found (in this case, your 4 letter word)

EDIT

Can also use the -i option if you want grep to ignore case (uppercase/lowercase)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement