1

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:

select
insert
into
values
delete
drop
from
create
table
where
set
varchar
number

so the desired result for the first line is:

* student
terdon
  • 234,489
  • 66
  • 447
  • 667
Aomine Daiki
  • 125
  • 4
  • 6
  • 17
  • You can pipe the result to another command like this `grep -owf champs.txt t.txt | ` – rahul Apr 14 '15 at 09: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 09: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 09:30
  • @terdon in this question http://unix.stackexchange.com/questions/195925/check-if-a-variable-is-in-list`` you can see the contains of the two files – Aomine Daiki Apr 14 '15 at 09:31
  • @jcbermu not save but cut the result because the `grep` command is a verification of some fields – Aomine Daiki Apr 14 '15 at 09:33
  • 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 09:37

1 Answers1

2

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(/ +/," ") }
  1
' 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
Janis
  • 14,014
  • 3
  • 25
  • 42