1

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

OR

grep -v '^www\.' myfile.txt | grep -v '/' mfile.txt > result.txt
αғsнιη
  • 41,407

2 Answers2

2

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

Alexander
  • 1,416
2

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.

αғsнιη
  • 41,407
schweik
  • 1,250