Basically, I extract a string and an integer from a text file with:
cat file.txt | cut -d " " -f 1,3 | sort
The output looks like this:
white 3
white 2
white 54
red 23
red 2
yellow 22
How can I add the values per the unique strings with bash scripting?
CodePudding user response:
Sample data file:
$ cat file.txt
white stuff 3 more stuff
white stuff 2 more stuff
white stuff 54 more stuff
red stuff 23 more stuff
red stuff 2 more stuff
yellow stuff 22 more stuff
One awk
solution to replace all of the current code and generate totals:
awk '
{ totals[$1] =$3}
END { for (i in totals)
print i,totals[i]
}
' file.txt
# or as a one-liner
awk '{totals[$1] =$3} END {for (i in totals) print i,totals[i]}' file.txt
This generates:
red 25
yellow 22
white 59
If sorting is required either pipe the output to sort
, eg:
awk '{totals[$1] =$3} END {for (i in totals) print i,totals[i]}' file.txt | sort
Or if using GNU awk
:
awk '
{ totals[$1] =$3}
END { PROCINFO["sorted_in"]="@ind_str_asc"
for (i in totals)
print i,totals[i]
}
' file.txt
Both of these generate:
red 25
white 59
yellow 22
CodePudding user response:
First, get out of the UUoC habit.
declare -A totals=()
while read -r color _ count _; do
totals[$color]=$(( "${totals[$color]:- 0}" count ))
done < file.txt
for color in ${!totals[@]}; do echo "$color ${totals[$color]}"; done | sort
output:
red 25
white 59
yellow 22
If it's very big, it would be more efficient in awk
.