How to split by awk can sum latest field
- User
How to split by awk can sum latest field
Assistant
To split fields using
awkand sum the last field, you can useawk's built-in field splitting and arithmetic capabilities.Example Input:
apple 10 banana 20 cherry 30Goal:
Sum the last field (in this case, the numbers).
✅ Basic Command:
awk '{ sum += $NF } END { print sum }' file.txt$NFrefers to the last field in each line.sum += $NFadds the last field to the running total.END { print sum }prints the total after processing all lines.
Example Output:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.