Manipulating a text data file with the bash command?

I was given this text file, name the file stock.txt, the contents of the text file:

pepsi;drinks;3
fries;snacks;6
apple;fruits;9
baron;drinks;7
orange;fruits;2
chips;snacks;8

I will need to use a bash - script to output this result:

Total amount for drinks: 10
Total amount for snacks: 14
Total amount for fruits: 11
Total of everything: 35

My gut tells me that I will need to use sed, group, grep and something else.
Where to begin?

+3
source share
5 answers

I will break the exercise into steps

Step 1: Read the file one line at a time

while read -r line
do
    # do something with $line
done

Step 2: pattern matching (drinks, snacks, fruits) and simple arithmetic. This step requires you to label each line that I left for you to figure out.

if [[ "$line" =~ "drinks" ]]
then
    echo "matched drinks"
    .
    .
    .
fi 
0
source

Clean Bash. Good app for associative array:

declare -A category                  # associative array
IFS=';'
while read name cate price ; do
  ((category[$cate]+=price))
done < stock.txt

sum=0
for cate in ${!category[@]}; do       # loop over the indices
  printf "Total amount of %s: %d\n" $cate ${category[$cate]}
  ((sum+=${category[$cate]}))
done

printf "Total amount of everything: %d\n" $sum
+1

bash :

http://www.cyberciti.biz/faq/unix-linux-bash-read-comma-separated-cvsfile/

- . IFS .

, bash: man - . , ( ) .

: man read . less, , q ( , , )

0

- -, bash 4.x , , awk perl. -, : , , .

. , awk, sed perl. , , - cut, sort uniq. cut . 5-9 grep, (grep $kind stock.txt), , bash.

for kind in $(cut -d\; -f 2 stock.txt | sort | uniq) ; do
    total=0
    while read d ; do
        total=$(( total+d ))
    done < <(
        while read line ; do 
            [[ $line =~ $kind ]] && echo $line
        done < stock.txt | cut -d\; -f3
    )

    echo "Total amount for $kind: $total" 
done

. , .

: - , cut. stock.txt , ;, \;, . , , stock.txt. sort, uniq. "", , , .

for: , , , kind. , "Total" .

total , , .

"totaling", kind . , d in stdin .

: shell , d total.

5 while, . <, , , , read, . , , .

, while-read. while-read, line. . [[ =~, . , $line $kind.

while-read , stock.txt, , , $kind, cut , . 9 , , , kind.

, , , .

0

OP. , OP 6 , wiki .


My answer is to get the total price, I use this:

...
PRICE=0
IFS=";"     # new field separator, the end of line   
while read name cate price
do
let PRICE=PRICE+$price
done < stock.txt
echo $PRICE

When I echo it: 35, which is true. Now I will move on to using awk to get the result of the subcategory.

Complete solution:

Thank you guys, I succeed. Here is my code:

#!/bin/bash
INPUT=stock.txt
PRICE=0
DRINKS=0
SNACKS=0
FRUITS=0
old_IFS=$IFS      # save the field separator   
IFS=";"     # new field separator, the end of line   
while read name cate price
do
    if [ $cate = "drinks" ]; then   
        let DRINKS=DRINKS+$price
fi

if [ $cate = "snacks" ]; then
        let SNACKS=SNACKS+$price
fi

if [ $cate = "fruits" ]; then
        let FRUITS=FRUITS+$price
fi

# Total
let PRICE=PRICE+$price
done < $INPUT

echo -e "Drinks: " $DRINKS
echo -e "Snacks: " $SNACKS
echo -e "Fruits: " $FRUITS
echo -e "Price " $PRICE 
IFS=$old_IFS
0
source

Source: https://habr.com/ru/post/1778792/


All Articles