Sample Header Ad - 728x90

How to use cut command to get the first and last elements of a row?

3 votes
5 answers
24794 views
I've asked almost the same question already , but this time, I want to retrieve the X **latest** elements of a row of a CSV file. For example, with an input file as this one: 1;foo;bar;baz;x;y;z 2;foo;bar;baz;x;y;z 3;foo;bar;baz;x;y;z What would be the command (eventually using cut) to get the last 2 columns, so I get: y;z y;z y;z In fact, my real target is to retrieve the first 3 **and** the last 2 fields of each row, so I get: 1;foo;bar;y;z 2;foo;bar;y;z 3;foo;bar;y;z Unfortunately, I cannot use a command like cut -d \; -f 1-3,10-11 (if there are 11 elements in the row), because the CSV file does not respect the *real* CSV format. Indeed, some fields in the middle of the rows are *encrypted*, and their encrypted value may sometimes contains a ; characters (and of course, they are not wrapped inside "). In others words, my lines may look like that: 1;foo;bar;#@$"é&^l#;baz;x;y;z 2;foo;bar;#¤=é;)o'#;baz;x;y;z 3;foo;bar;#]]'~é{{#;baz;x;y;z and as you can see, on the second line, there is an additional ; character, so I can't use here a command like cut -d \; -f 1-3,7-8, because if will return that, which is wrong: 1;foo;bar;y;z 2;foo;bar;x;y (-> Wrong here, there is a shift) 3;foo;bar;y;z So how can I use cut to solve my problem? Thanks ps: I am specially in love with the cut command, so if you have a command that does what I want but that is not cut, then it's fine too :) *Edit* It seems important to note that the machine is quite old: uname -a give this message: SunOS ###### 5.10 Generic_142900-05 sun4u sparc SUNW,Sun-Fire-V240 and some commands may not be present (like rev)
Asked by Romain Linsolas (235 rep)
Jan 7, 2013, 02:32 PM
Last activity: Mar 20, 2024, 05:14 AM