Score:0

Grep pattern Matching Doesn't work?

bd flag

I have a text file like so:

"Id","District Name","Neighborhood Name","Street","Weekday","Month","Day","Hour","Part of the day","Mild injuries","Serious injuries","Victims","Vehicles involved","Longitude","Latitude"
"2017S008429    ","Unknown","Unknown","Número 27                                         ","Friday","October",13,8,"Morning",2,0,2,2,2.12562442,41.34004482
"2017S007316    ","Unknown","Unknown","Número 3 Zona Franca / Número 50 Zona Franca      ","Friday","September",1,13,"Morning",2,0,2,2,2.12045245,41.33942606
"2017S010210    ","Unknown","Unknown","Litoral (Besòs)                                   ","Friday","December",8,21,"Afternoon",5,0,5,2,2.1673561,41.3608855
"2017S006364    ","Unknown","Unknown","Número 3 Zona Franca                              ","Friday","July",21,2,"Night",1,0,1,2,2.12452894,41.33766786
"2017S004615    ","Sant Martí","el Camp de l'Arpa del Clot","Las Navas de Tolosa                               ","Thursday","May",25,14,"Afternoon",1,0,1,3,2.185272,41.416365
"2017S007775    ","Sant Martí","el Camp de l'Arpa del Clot","Indústria / Trinxant                              ","Wednesday","September",20,12,"Morning",1,0,1,2,2.183245,41.416336
"2017S004484    ","Sant Martí","el Camp de l'Arpa del Clot","Trinxant / Indústria                              ","Saturday","May",20,21,"Afternoon",1,0,1,2,2.183245,41.416336
"2017S010680    ","Sant Martí","el Camp de l'Arpa del Clot","Indústria                                         ","Tuesday","December",26,20,"Afternoon",2,0,2,1,2.183561,41.416372
"2017S005152    ","Sant Martí","el Camp de l'Arpa del Clot","Indústria                                         ","Monday","June",12,15,"Afternoon",1,0,1,1,2.184059,41.416763
"2017S003932    ","Sant Martí","el Camp de l'Arpa del Clot","Maragall                                          ","Wednesday","May",3,20,"Afternoon",1,0,1,1,2.181225,41.413958
"2017S010348    ","Sant Martí","el Camp de l'Arpa del Clot","Indústria                                         ","Thursday","December",14,20,"Afternoon",1,0,1,2,2.18229,41.415772
"2017S000245    ","Sant Martí","el Camp de l'Arpa del Clot","ST ANTONI M CLARET / Pg Maragall                  ","Wednesday","January",11,7,"Morning",1,0,1,2,2.180453,41.41579
"2017S005695    ","Sant Martí","el Camp de l'Arpa del Clot","Sant Antoni Maria Claret / Guinardó               ","Friday","June",30,12,"Morning",1,0,1,2,2.18114,41.416295
"2017S007384    ","Sant Martí","el Camp de l'Arpa del Clot","Sant Antoni Maria Claret                          ","Monday","September",4,16,"Afternoon",0,1,1,2,2.176324,41.412546
"2017S008080    ","Sant Martí","el Camp de l'Arpa del Clot","Sant Antoni Maria Claret                          ","Saturday","September",30,19,"Afternoon",1,0,1,2,2.176324,41.412546
"2017S003439    ","Sant Martí","el Camp de l'Arpa del Clot","Còrsega / Còrsega                                 ","Monday","April",17,14,"Afternoon",1,0,1,2,2.178314,41.410784
"2017S002410    ","Sant Martí","el Camp de l'Arpa del Clot","CÒRSEGA / Dos de Maig                             ","Saturday","March",25,14,"Afternoon",4,0,4,2,2.177719,41.410323
"2017S009296    ","Sant Martí","el Camp de l'Arpa del Clot","Indústria                                         ","Thursday","November",9,22,"Night",1,0,1,1,2.17674,41.411445

I tried something like:

grep "May ,25" $1

where $1 is the filename which the user inputs in. So basically it is supposed to find the rows that contain the date May, 25, but it displays absolutely nothing. I know for a fact that there are rows that contain the date May, 25.

I also tried the following, but it still does not work:

grep "\"May\" ,25" $1

What am I doing wrong?

Score:2
hr flag

Quotes are characters too - your files doesn't contain May,25, it contains "May",25. So

grep '"May",25' "$1"

or

grep "\"May\",25" "$1"

(but not grep "\"May\" ,25" "$1" - spaces are characters as well).

Or you can use a CSV aware tool that understands CSV quoting:

mlr --csv filter 'Month == "May" && Day == 25'

csvgrep -c Month -m May "$1" | csvgrep -c Day -m 25
Yunfei Chen avatar
bd flag
Just wondering I got a file like file1.txt that has all the dates formatted like this one, So like it has the "May",25 and I wanted to find the dates in this file that matches the one on the other one, so I tried ```cat file1.txt | xargs -I{} grep -E {} "$1"``` but that obviously doesnt work because the quotes need to be escaped is there a simple fix for this??
hr flag
@YunfeiChen `grep` already has the ability to read a list of patterns from a file (they need to be one per line though) - `grep -f file1.txt "$1"` (or `grep -F -f file1.txt "$1"` since the dates are simple fixed strings rather than regular expressions).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.