check if a variable is in list
7,011
It is not very clear...
grep -owf champs.txt t.txt
Related videos on Youtube
Author by
Aomine Daiki
Updated on September 18, 2022Comments
-
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 keywordsselect insert into values delete drop from create table where set varchar number
-
rahul about 9 yearscan you paste the contents of t.txt and champs.txt and clarify what exactly are you trying to do?
-
jcbermu about 9 yearsI suspect it can be solved with a simple
grep
, but I would like to see an example of the contents oft.txt
andchamps.txt
. -
Aomine Daiki about 9 yearswhen i have to put the grep
-
-
Aomine Daiki about 9 yearsthank 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 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 about 9 yearsi 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 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 about 9 yearsLet us continue this discussion in chat.