I have the following command:

grep -owf champs.txt  t.txt

and I want to cut the result of this command from the file t.txt to another file. I know that it can be achieved by a nested command but I do not know how to do it.

Note that in the file t.txt, I have:

select * from student
insert name age from professors 
delete from departement where DPTNUM= 20

and in the file champs.txt I have:


so the desired result for the first line is:

* student
  • You can pipe the result to another command like this grep -owf champs.txt t.txt | <command2> – rahul Apr 14 '15 at 9:28
  • 3
    Please edit your question and show us an example of your input (champs.txt and t.txt) and your desired output. Also make sure you tell us how your fields are defined. Tabs? Spaces? Commas? Something else? – terdon Apr 14 '15 at 9:28
  • ¿Do you want to save the results of the command to a new file? Then you can use the command as: grep -owf champs.txt t.txt > newfile.txt – jcbermu Apr 14 '15 at 9:30
  • @terdon in this question unix.stackexchange.com/questions/195925/…`` you can see the contains of the two files – Aomine Daiki Apr 14 '15 at 9:31
  • 4
    I don't want to look at another question. Each question should be self contained. Please edit and include all necessary information here. In any case, the other question does not include your desired output nor the contents of t.txt. – terdon Apr 14 '15 at 9:37

Here is an awk way to achieve what you want:

awk '
  NR==FNR { k[$1] ; next }
  { for (i=1; i<=NF; i++) if($i in k) $i="" ; gsub(/ +/," ") }
' champs.txt t.txt

(The gsub is just for convenience, it compresses sequences of blanks.)

For you sample data the result is:

 * student
 name age professors
 departement DPTNUM= 20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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