0

I have a bunch of csv files which look sort of like this:

date,receiver,client,article_id,amount
5-12-2019,thomas,thomas,2,23
5-12-2019,joe,joe,2,14
6-12-2019,tobias,bob,4,1

now I would like to search for every line where receiver and client are identical. Can this be done with a regex search?

4

So you want to skip the first column (comma delimited) and search for everything, where the second column is the same as the third column.

As a regex this can be written like this:

^[^,]*,\zs\([^,]*\),\1
|  |  |  | |  |   || └ Same match as what was captured
|  |  |  | |  |   |└   Comma 
|  |  |  | |  |   └    End of captured group
|  |  |  | |  └        Any number of non-commas
|  |  |  | └           Capture the following
|  |  |  └             Start the match here
|  |  └                A comma
|  └                   Any number of non-commas
└                      Start of line

You can use :set hls to have a visual indication of what lines match the pattern.

Now if you want to delete those matching lines, you can simply use a :g command, which basically means, do for all lines that match a certain pattern:

:g/^[^,]*,\zs\([^,]*\),\1/d

Or, if you already have the pattern in the search register (e.g. all matching lines are already colored), you can even simplify this call to:

:g//d

Which means, delete all the lines that match the last entered search pattern.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.