awk with pipe delimited file (specific column matching and multiple pattern matching)
Hi all, so I am working on a bash script, and am currently stuck trying to figure out how to deal with the following file:
Code:
467487|field1|more fields|first pattern|stuff|470061|more text|text with spaces|None|Red|another_field|8/30/2011 5:12:30 PM|9/6/2011 5:12:30 PM|one more text field with spaces|651463| Assuming both of these patterns match, (in the specific column, as they exist in other columns I do not care about) I also need to increment a counter by one. Now, assuming I have been learning right, I need something like this to start with: awk -F'|' { '$4=="first pattern" print $0 >> count.txt }' then, I did a linecount on count to find out how many. Now, I know there is a way to do counts in awk, so I dont need to waste clock cycles similar to adding { ++x }END { print x } at the end of my expressions, however, I do not know how I can combine all this so I can add to a counter if both '$4=="first pattern" and $12==$date. Any help that could steer me in the right direction would be truly fantastic. -lolmon |
Hi, welcome to LQ!
Almost there! Code:
awk -F'|' -v date="$date" '$4=="first pattern" && $12==date {x++}END{print x}' file Cheers, Tink |
Quote:
I tried doing ^date, but it does not like that format, something I would assume has to do with the ==, which I am looking up right now, but I figured I should post anyway and thank you for the first part. EDIT: it is clearly that, I have removed the extra time info and the script works just as planned, so all I need to do is figure out how to tell it that that column only must start with the date... EDIT2: it works if I use the ~ match op instead of ==, and that should be fine considering the data file will look like my example all the time anyway. thanks again! |
Well ... the problem is that you need to populate the shell variable $date
with the exact time and date you're looking for. If it's just a date then, yes, the '~' for the date part is adequate :} Cheers, Tink |
Incidentally, you could also do the same thing entirely in bash, using an array.
Code:
#!/bin/bash I imagine awk would still be more efficient for a large file, however. |
All times are GMT -5. The time now is 02:33 PM. |