To get the last blank-delimited column with a POSIX compliant sed
(the equivalent of awk '{print $NF}'
) and assuming the input is valid text:
sed 's/[[:blank:]]*$//; # remove trailing blanks
s/.*[[:blank:]]//; # remove every thing up to the last blank
' < file
Note that most awk
implementations separate fields on blanks (horizontal spacing), but some (namely busybox awk
) separate on all spaces ([[:space:]]
), and some only on the ASCII blanks (space and tab) regardless of the locale. On files coming from Microsoft OSes, you may want to replace [[:blank:]]
with [[:space:]]
so it considers the trailing CR character as spacing and discards it.
To get the sixth field (equivalent of awk '{print $6}'
):
s='[[:blank:]]' S='[^[:blank:]]'
sed "s/^$s*\($S\{1,\}$s\{1,\}\)\{5\}\($S\{1,\}\).*/\2/; t
s/.*//; # flush the line if does not have 6 fields"
You could also use an approach like:
sed 's/[^[:blank:]]\{1,\}/\
&\
/6; s/.*\n\(.*\)\n.*/\1/;t
s/.*//'