asked    Warner     2018-10-22       bash       124 view        1 Answer

[SOLVED] grep a list into a multi columns file and get fully matching lines

not sure how to ask this question but an example would surely clarify. Suppose I have this file:

$ cat intoThat 
a   b
a   h
a   l
a   m
b   c
b   d
b   m
c   b
c   d
c   f
c   g
c   p
d   h
d   f
d   p

and this list:

cat grepThis 
a
b
c
d

now I would like to grepThis intoThat and I would do this:

$grep -wf grepThis intoThat

which will give an output like this:

**a b**
a   h
a   l
a   m
**b c**
**b d**
b   m
**c b**
**c d**
c   f
c   g
c   p
d   h
d   f
d   p

now the asterisks are used to highlight those lines that I would like grep to return. These are the lines that have a full match but...how to tell grep (or awk or whatever) to get only these lines? Of course it is possible that some lines do not match any pattern, e.g. in the intoThat file I may have some other letters like g, h, l, s, t, etc...

  1 Answer  

        answered    Beryl     2018-10-22      

With awk, you could do:

awk 'NR==FNR{ seen[$0]++; next } ($1 in seen && $2 in seen)' grepThis intoThat
a   b
b   c
b   d
c   b
c   d
  • NR is set to 1 when the first record read by awk and incrementing for each next records reading either in single or multiple input files until all records/line read.
  • FNR is set to 1 when the first record read by awk and incrementing for each next records reading in current file and reset back to 1 for the next input file if multiple input files.
  • so NR == FNR is always a true condition for first input file and the block followed by this will perform actions on the first file only.

  • The seen is an associated awk array named seen (you can use different name as you want) with the key of whole line $0 and value with occurrences of each line occurred (this way usually is using to remove duplicated records in awk too).

  • The next token skips to executing rest of the commands and those will only execute actually for next file(s) except first.

  • In next (....), we are just checking if both column$1 and $2 are present in the array, if so they will goes in output.





Your Answer





 2018-10-22         Jeff

Time Complexity of while loop

What is the Best case, Worst Case and Average Case scenario of the followingcode segment ? def sumInts(n): Count = 0 while i < n: Count = count + n Return count It doesn't run because there's neither an i nor a count defined anywhere.In other words: Best case: crash, Worst case: crash, Average case: crash. [XXX]
 python                     1 answers                     57 view
 2018-10-22         Jeffrey

Return the rows(Ids) which satisfies and has both the values in IN condition

I have a below table with sample data called Recipes which acts as a junction table between other 2 tables.Query - select recipeId, IngredientId from Recipes where IngredientId in (1,31) order by recipeIdWhen i execute above SQL statement it gives below Output. which is fine.What changes i have to make in the query to get below highlighted Output.Now why 6,7 & 21?As RecipeIds 6, 7 & 21 are the only Ids which has both IngrdientIds [i.e. 1,31] You can use group by clause :select RecipeIds from table twhere IngrdientIds in (1, 31)group by RecipeIds having count(dis...
 sql                     1 answers                     63 view
 2018-10-22         Enoch

SQL Relationship to Secondary Table Without Adding Duplicate Column

There are Cities, Job Types, and Tasks. Cities can have multiple Job Types. Tasks are created for a City that can have multiple Job Types. Cities can have Many Tasks. But when adding the Job Types for the Tasks assigned to a City, must make sure the City has that Job Type. How to create Relationship/Constraint when Adding/Updating Task_JobTypes to make sure the City associated with the Task has that Job Type allowed in City_JobTypes? The constraint in Task_JobTypes "FK_Task_JobTypes_JobTypes" need to be referencing that instead of just JobTypes.Cities - Id,Name JobType...
 sql                     2 answers                     60 view