How can one print the element after the last tab in a file?
Exemple: File1
A 3 8 6 7
B 4 6 2 3 6 8
c 1 9
would return:
7
8
9
You can make smart use of the NF
variable in awk
awk '{print $NF}' File1
From man awk
NF The number of fields in the current input record.
So NF
will give you the amount of fields and $NF
will then expand to $3
for example, which you can use in a print statement.
If you want the last field only and not both first and last, you could try:
rev file | cut -f 1
The rev
reverses each line of the file and the cut
prints only the first field (fields are defined by tabs by default). Since the line has been reversed, the first field is the original last field.
The above works in your given example but, as @Bernhard points out below, it will print fields that are longer than a single character reversed. In those cases, you will need a second rev:
rev file | cut -f 1 | rev
Bernhard's answer is the correct one. For fun and completeness, bash:
while read -a fields; do
echo ${fields[-1]}
done < file
awk
is really the tool for the job here but for the sake of completeness here's a GNU grep
way
grep -oE '[^[:space:]]+$' file
7
8
9
rev
. – Bernhard Jul 21 '14 at 11:01