Need simpler version of these commands

Hi all,

I am trying to grep a file with the word grand and get all the fields.. Then replace multiple spaces with single space and then get 8 th field and add all these numbers . I am able to do it but with so amny commands which i feel can be done in a simpler way

Please let me know if there is a simpler way

cat File1.$$|grep Grand > temp.txt
sed 's/ */,/g' temp.txt > temp1.txt
cut -d, -f8 temp3.txt >temp4.txt
cat temp4.txt
awk '{x+=$0}END{print x}' temp4.txt> temp5.txt 

Hello Hypesslearner,

Kindly use code tags while posting your codes and commands. We can help you more, if you can please provide us the input and expected output for same.

Thanks,
R. Singh

Try this:

awk '/Grand/{n+=$8;gsub(/ +/," ");print};END{print "TOTAL: "n+0}' File1.$$
1 Like

You don't specify how many (if any) of the tempN.txt files you need to keep.

I'm assuming you only want to keep temp5.txt (containing the grand total).

awk '/Grand/{$0=$8;n+=$0} 1; END{print n > "temp5.txt"}' File1.$$

If you want only the temp5.txt file,

awk '/Grand/ {sum += $8} END {print sum}' File1.$$ > temp5.txt