How to cut (select) a field from text line counting from the end?
Reverse the input before and after cut
with rev
:
<infile rev | cut -d, -f2 | rev
Output:
d
i
n
Try doing this with awk :
awk -F, '{print $(NF-1)}' file.txt
Or using perl :
perl -F, -lane 'print $F[-2]' file.txt
Or using ruby (thanks manatwork) :
ruby -F, -lane 'print $F[-2]' file.txt
Or using bash
(thanks manatwork) :
while IFS=, read -ra d; do echo "${d[-2]}"; done < file.txt
Or using python :
cat file.txt |
python -c $'import sys\nfor line in sys.stdin:\tprint(line.split(",")[-2])'