Skip to main content
Fix broken quoting
Source Link
tripleee
  • 185.2k
  • 36
  • 295
  • 342

Simplest is

echo $A"$A" | awk '{print $NF}'

Edit: explanation of how this works...

awk breaks the input into different fields, using whitespace as the separator by default. Hardcoding 5 in place of NF prints out the 5th field in the input:

echo $A"$A" | awk '{print $5}'

NF is a built-in awk variable that gives the total number of fields in the current record. The following returns the number 5 because there are 5 fields in the string "Some variable has value abc.123":

echo $A"$A" | awk '{print NF}'

Combining $ with NF outputs the last field in the string, no matter how many fields your string contains.

Simplest is

echo $A | awk '{print $NF}'

Edit: explanation of how this works...

awk breaks the input into different fields, using whitespace as the separator by default. Hardcoding 5 in place of NF prints out the 5th field in the input:

echo $A | awk '{print $5}'

NF is a built-in awk variable that gives the total number of fields in the current record. The following returns the number 5 because there are 5 fields in the string "Some variable has value abc.123":

echo $A | awk '{print NF}'

Combining $ with NF outputs the last field in the string, no matter how many fields your string contains.

Simplest is

echo "$A" | awk '{print $NF}'

Edit: explanation of how this works...

awk breaks the input into different fields, using whitespace as the separator by default. Hardcoding 5 in place of NF prints out the 5th field in the input:

echo "$A" | awk '{print $5}'

NF is a built-in awk variable that gives the total number of fields in the current record. The following returns the number 5 because there are 5 fields in the string "Some variable has value abc.123":

echo "$A" | awk '{print NF}'

Combining $ with NF outputs the last field in the string, no matter how many fields your string contains.

Simplest is

echo $A | awk '{print $NF}'

Edit: explanation of how this works...

awk breaks the input into different fields, using whitespace as the separator by default. Hardcoding 5 in place of NF prints out the 5th field in the input:

echo $A | awk '{print $5}'

NF is a built-in awk variable that gives the total number of fields in the current record. The following returns the number 5 because there are 5 fields in the string "Some variable has value abc.123":

echo $A | awk '{print NF}'

Combining $ with NF outputs the last field in the string, no matter how many fields your string contains.

Simplest is

echo $A | awk '{print $NF}'

Simplest is

echo $A | awk '{print $NF}'

Edit: explanation of how this works...

awk breaks the input into different fields, using whitespace as the separator by default. Hardcoding 5 in place of NF prints out the 5th field in the input:

echo $A | awk '{print $5}'

NF is a built-in awk variable that gives the total number of fields in the current record. The following returns the number 5 because there are 5 fields in the string "Some variable has value abc.123":

echo $A | awk '{print NF}'

Combining $ with NF outputs the last field in the string, no matter how many fields your string contains.

Source Link
gammay
  • 6.1k
  • 7
  • 33
  • 53

Simplest is

echo $A | awk '{print $NF}'