I have a CSV filled with text messages, and I want to be able to easily filter by sender/receiver. The last field indicates "SMS", "iMessage" or "WhatsApp"; the previous fields may spill over multiple lines.
E.g: "Sent","Aug 4, 2021 12:34:56","Some Person","+11234567890","Hello!","SMS"
So I can easily :g/.*Some Person.*
to get everything with that person. But if the message ends up being long enough to spill over into multiple lines, I need to tell global to keep matching until it hits that last field. E.g.:
"Sent","Aug 4, 2021 12:34:56","Some Person","+11234567890","Hello!
Nice
to
see you","SMS"
I've tried things along the lines of:
:g/\"Sent.*Some Person\(.*\)\"SMS|\WhatsApp\|iMessage\"
however it seems like because the .*
only matches until newline, this only matches one full line at most.
What's the correct way around this?
EDIT 1: I've also tried g:/.*some person.*\_\"imessage\|sms\|whatsapp\"\{-}
: it matches lines that don't only contain "some person". I have ignorecase
set.
EDIT 2: here's a sample.
"Received","Nov 26, 2021 15:00:28","Foo Bar cell","11234567890","A couple of words","WhatsApp"
"Sent","Nov 26, 2021 14:57:22","Foo Bar cell","11234567890","Word word word word
Sentence goes here
another ones goes here","WhatsApp"
"Sent","Nov 26, 2021 14:56:52","Dad Cell","11234567890","Word word word word. Word!","WhatsApp"
I should be able to search for messages from 'Foo Bar cell' and have it return lines one through four (the first two messages).
:global
only marking the first line of a multi-line match (meaning the associated command will only affect that line...try it with:g//d
to see what I mean). That's why I recommend sticking to one line per record.:g
. Naively, if what:g
does is check whether each line matches the pattern, then the middle lines do not match the pattern, since it's the combination of all of those lines that does match. You should probably rework whatever you're doing with:g
to instead read up until the message ends.:g
would do is still only on the first line, because that's where the pattern matches -g
has no idea other lines were gobbled up by the regex. I meant that if you were doing something like:g/.../ w some-file
, instead of justw file
, you should probably use a command there that would instead start on the line on whichg
would run and end on the message ending line. But that depends on what you you're doing with:g
here, and you haven't shown us that.g/"Sent.*Some Person/ s/^\_.\{-}\(SMS\|WhatsApp\|iMessage\)"\n//
to remove the matching lines, perhaps. It'll be trickier to keep only them instead