sed -E 's/(([^;]*);){6}.*/\2/'
will do it, where 6
is the the field number that you want to capture.
(If you specify a field number greater than the number of fields in your input, it just echoes the input without doing any substitution.)
I've used the -E
option, which enables extended regular expressions. Depending on the version of sed you have, you might need to use -r
instead. Alternatively, skip the option, so that you're using basic regular expressions, and escape the parentheses and curly braces:
sed 's/\(\([^;]*\);\)\{6\}.*/\2/'
How it works:
sed will find a match at the earliest possible position, and in this case, there's a match starting at the first character (assuming that there are at least 6 fields in your input). The outer parenthetical expression matches a field followed by a ;
delimiter. The command will match 6
of these successively (or whatever number you specify). The .*
at the end matches the rest of the line. As a result, the entire line gets replaced.
What does it get replaced with? \2
refers to the inner parenthesized expression (the one that starts with the 2nd left parenthesis). That inner parenthetical expression actually gets matched 6 times, but sed will use the very last match, which is what you want.
A version with better functionality:
This version will replace the entire line with an empty string if the indicated field doesn't exist (in the example, if there are fewer than 6 fields in your input):
sed -E 's/(([^;]*);){6}.*/\2/;t;d'
On OS X's versions of sed (and maybe BSD in general?), this seems to need to be written on two lines:
sed -E 's/(([^;]*);){6}.*/\2/;t
d'
The command t
will terminate sed's processing of this input line if a substitution was made.
So if the 6th field exists, the substitution is made as before, and the t
command ends processing of this input line. But if the 6th field doesn't exist, no substitution is made by the s
command, so the t
doesn't branch; sed just goes on to the d
command, which deletes the input line (that's what we want to do if there are fewer than 6 fields in the input line).
A9S
not in the sixth column? – Cyrus Apr 21 '20 at 17:15awk -v c="6" -F ';' '{print $c}' file
– Cyrus Apr 21 '20 at 17:36