Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Grep: check if a string comes up twice

Shell Programming and Scripting


Tags
grep, read, wc

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-14-2017
tons92 tons92 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 21 June 2017, 1:14 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Grep: check if a string comes up twice

I have the following files
list.txt

Code:
string1<TAB>ABC
string2<TAB>DEF
string3<TAB>GHI

query.txt

Code:
ABC
DEF
GHI
ABC

Now I want to check, if a string in the first column of list.txt is twice in query.txt

so my command is:

Code:
while IFS=$'\t' read k v ; do  if (($(grep -i '$v' query.txt | wc -l)>=2)); then echo "$v more than 2"; fi; done<list.txt

but nothing is returned here, so where is the error?
Sponsored Links
    #2  
Old Unix and Linux 06-14-2017
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 August 2017, 7:46 PM EDT
Location: Houston, Texas, USA
Posts: 950
Thanks: 0
Thanked 322 Times in 306 Posts

Code:
while IFS=$(printf "\t") read k v ; do if (($(grep -i "$v" query.txt | wc -l) >= 2)); then echo "$v more than 2"; fi; done < list.txt


Last edited by rdrtx1; 06-14-2017 at 06:07 PM..
Sponsored Links
    #3  
Old Unix and Linux 06-15-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 18 August 2017, 12:35 PM EDT
Location: Lancashire, UK
Posts: 3,208
Thanks: 1,369
Thanked 616 Times in 557 Posts
Beware that for large input files, you are reading the entire list.txt for every entry in query.txt

There may be better ways to approach this, but it depends how much you need to know. If you just need to know that ABC (or whatever) has been repeated and you don't care what the first string part is, then you might be better with this:-
Code:
cut -f2 -d"$(printf "\t")" list.txt | sort | uniq -c | grep -f query.txt | grep -Ev "^      1 "

It is a few pipes, but will read list.txt once.

If you then need to go back and get the leading string information, we can probably work on that.


So,
  • Does that help?
  • Does it confuse?
  • Is it irrelevant because the files are trivial?
  • Does anyone else have a better way? I'm open to suggestions too!


I hope that this helps,
Robin
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep string in files and list file names that contain the string apenkov Shell Programming and Scripting 8 03-27-2013 09:08 AM
Check file for string existence before appending it with string sahil_shine Shell Programming and Scripting 2 03-04-2013 03:12 AM
grep exact string from files and write to filename when string present in file JC_1 Shell Programming and Scripting 4 08-06-2012 10:52 PM
Grep a string from input file and delete next three lines including the line contains string in xml greet_sed Shell Programming and Scripting 4 09-28-2011 05:18 AM
read string, check string length and cut ozzy80 Shell Programming and Scripting 9 03-21-2007 05:56 PM



All times are GMT -4. The time now is 07:53 AM.