This question already has an answer here:

I have a file. I want to select the lines that does not start with www. and does not contains slashes. I want to output the result into result.txt

To achieve the first requirement:

grep -v '^www\.' myfile.txt > result.txt.

To achieve the second, I will take result.txt and execute:

grep -v '/' result.txt > result2.txt

Is there any better shorcut to execute several commands on the file and store the result into one output file: result.txt

I am aware of | to execute several commands where the output of the command on the left of | is input of the command in the right of |. What I do not know is, in case of grep or any other command, should I use the file name in the command on the right of |. In other words, should it be:

grep -v '^www\.' myfile.txt | grep -v '/' > result.txt


grep -v '^www\.' myfile.txt | grep -v '/' mfile.txt > result.txt

marked as duplicate by αғsнιη, schily, G-Man, Romeo Ninov, msp9011 Aug 17 '18 at 18:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


grep -v -e '^www\.' -e '/' myfile.txt > result.txt


No fear to use pipes. Alexander answer is the best and purrest, but in case I look for the best formula first, I use some step:

  1. head file |grep 'first-condition' |grep 'next-conditon' :: gives you overwiev
  2. cat file |grep 'first-condition' |grep 'next-conditon' |wc -l :: gives amount
  3. grep --color 'complex-condition' :: highlights the real selection

In final step you can create a very complex regexp e.g.:

egrep -v '(^www\.)|(/)' myfile.txt > result.txt

Sometimes it is necessary to use other string analysing commands awk, sed, cut etc. Using the pipe is then very usefull.

Not the answer you're looking for? Browse other questions tagged or ask your own question.