check if a variable is in list

7,011

It is not very clear...

grep -owf champs.txt  t.txt
Share:
7,011

Related videos on Youtube

Aomine Daiki
Author by

Aomine Daiki

Updated on September 18, 2022

Comments

  • Aomine Daiki
    Aomine Daiki over 1 year

    to check if a word is in a list of words or no I wrote this script:

    tr -s '[[:blank:]]' '\n' < t.txt |
    
    
    while IFS= read -r word; do
    
    
    if [[ "$word" =~ $(echo ^\($(paste -sd'|' ./champs.txt)\)$) ]]; then
    

    but checking is not done. Even the word list was not checked any time

    The file t.txt contains a list of sql query:

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

    and the file champs.txt contains the query keywords

    select
    insert
    into
    values
    delete
    drop
    from
    create
    table
    where
    set
    varchar
    number
    
    • rahul
      rahul about 9 years
      can you paste the contents of t.txt and champs.txt and clarify what exactly are you trying to do?
    • jcbermu
      jcbermu about 9 years
      I suspect it can be solved with a simple grep, but I would like to see an example of the contents of t.txt and champs.txt.
    • Aomine Daiki
      Aomine Daiki about 9 years
      when i have to put the grep
  • Aomine Daiki
    Aomine Daiki about 9 years
    thank you, but i used read because I must parse all the text file word by word, and check every word and if that is not the case then I want to run other commands on the desired word.
  • PM 2Ring
    PM 2Ring about 9 years
    @AomineDaiki. Understood, however you can do that more efficiently (and with more safety) using awk, which has great features for running shell commands. See this question for why using a shell loop to process text is considered bad practice, especially Stéphane Chazelas's answer.
  • Aomine Daiki
    Aomine Daiki about 9 years
    i do not know how to use awk to parse a text file word by word can you give me the command line please
  • PM 2Ring
    PM 2Ring about 9 years
    @AomineDaiki: Here's a simple awk program that will print each whitespace-delimited word from one (or more) text files, with each word on a separate line. If no filenames are supplied on the commandline it will read from stdin. awk '{for(i=1; i<=NF; i++)print $i}' filename
  • Aomine Daiki
    Aomine Daiki about 9 years