liamhawkins
liamhawkins

Reputation: 1381

Edit CSV column with unix command

I have a CSV table structured as below (I aligned columns for clarity):

AAAA, BBBB,                CCCC, DDDD
EEEE, FFFF;1111,           GGGG, HHHH
IIII, JJJJ;2222;3333;4444, KKKK, LLLL

And in the second column I would like to remove everything after the first occurrence of a semi-colon, resulting in:

AAAA, BBBB, CCCC, DDDD
EEEE, FFFF, GGGG, HHHH
IIII, JJJJ, KKKK, LLLL

I've tried various awk commands inspired from here, here, and here, but can't quite get it.

I understand I could select the second column and edit it by awk -F, '{print $2}' file | cut -d ';' -f 1, but can't seem to figure out how to replace the second column with that command

Upvotes: 1

Views: 582

Answers (1)

karakfa
karakfa

Reputation: 67567

$ awk '{sub(";[^,]+","")}1' file

or, perhaps better

$ sed -E 's/;[^,]+//' file

Upvotes: 3

Related Questions