Vno Vno - 1 month ago 9x
Linux Question

Using grep to find words that have a certain prefix and suffix

I'm trying to figure out how to find certain words in a file that start with the letters

and end with the letters
. I'd like to print the words that it matches with into a new file called "excluded". I'm very new to this environment of command lines so i'm a bit lost. I've read the manual and cannot find a solution.

I was thinking something along the lines of

grep "air" | "ne" textfile.txt

but obviously it's not working out.

edit: I think I can use the
operators to find letters at the beginning and end of a word, however i'm unsure as to how to make it one command so I can simply paste the output into a new file.


In order to print the words into a new file, you'll want to use the ">" operator to send the output of grep into a file, so the command would be:

grep '^air.*ne$' textfile.txt > excluded.txt

or, if you prefer to use pipes, something along the lines of:

cat textfile.txt | grep '^air.*ne$' > excluded.txt

would also work. This assume of course, that you're in the folder containing textfile.txt.

For test data


the output is: