awk-calculate records of a text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk-calculate records of a text
# 1  
Old 06-11-2010
awk-calculate records of a text

I want to make a bash shell script that accepts as argument a file name (cars.txt) and: 1) calculates the total price per year and per model.
2) For each car that it's number starts with TK, I want to print the surname and name of the owner and the total cost for them.

Code:
[Number] [Surname] [Name] [Cubic] [Price] [Date when bought car] [Model]

NBE3452 PANAGIWTOY NIKOLAOS 1200 20000 12/05/2008 BMW
TKH2345 VAFEIADIS GEWRGIOS 1400 23400 09/5/2007 BMW
TKE3423 STYLIANOY DHMHTRA 1800 14000 23/11/2006 FIAT
NTN7800 MARKOY KWNSTANTINOS 2300 34000 23/12/2008 FIAT
PIN1245 KARAGEWRGIOY EYAGGELOS 1800 23000 11/5/2009 GOLF
NKN4200 MARGARITH STAYROULA 1200 19000 22/5/2009 BMW
KNB4506 KALPAKIS NIKOLAOS 1400 23000 30/7/2008 BMW
XKA9128 MYLWNAS HLIAS 1600 21000 12/9/2008 AUDI

I want to make a bash shell script that accepts a file and: 1) calculates the total price per year and per model.
2) For each car that it's number starts with TK, I want to print the surname and name of the owner and the total cost for them.

Code:
#!/bin/sh

if [ $# -ne 1 ]; then

echo " give argument"

BEGIN  awk' {FS=" "}
{a[$7]+=$5}END{for (i in a){print i, " total cost per model "a[i]}}' 

exit 1
fi

help me...
# 2  
Old 06-11-2010
Code:
awk '/^TK/{print $2,$3;c+=$5}{a[$7]+=$5}END{x="Total cost ";print x" for TK is "c ORS;for (i in a){print i"\t" x" per model "a[i]}}' file


Last edited by danmero; 06-11-2010 at 01:46 PM.. Reason: update
This User Gave Thanks to danmero For This Post:
# 3  
Old 06-12-2010
Smilie You are genius ! Smilie

---------- Post updated 06-12-10 at 01:05 PM ---------- Previous update was 06-11-10 at 04:38 PM ----------

And how to find the total cost per year ? 2007, 2008, 2009 etc ?

Code:
awk '/^TK/{print $2,$3;c+=$5}{a[$7]+=$5}END{print "Total cost for TK is "c;for (i in a){print i, " total cost per model "a[i]}}' cars.txt

# 4  
Old 06-13-2010
Code:
 awk '{split($(NF-1),a,"/");b[a[3]]+=$(NF-2)}END{for(i in b) print i,"$"b[i]}' file
2006 $14000
2007 $23400
2008 $98000
2009 $42000

This is only for cost per year.
This User Gave Thanks to danmero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

Calculate average for rows in a text file

Dear Gurus, I have tab-delimited text files with matrix containing values. The first column is a identifier and other columns have the corresponding values. I would like to calculate the average value (total number/number of entries) for all entries from 2nd column to the last column in row... (3 Replies)
Discussion started by: Unilearn
3 Replies

2. Shell Programming and Scripting

Calculate Average AWK

I want to calculate the average line by line of some files with several lines on them, the files are identical, just want to average the 3rd columns of those files.:wall: Example file: File 1 001 0.046 0.667267 001 0.047 0.672028 001 0.048 0.656025 001 0.049 ... (2 Replies)
Discussion started by: AriasFco
2 Replies

3. Shell Programming and Scripting

Using AWK to Calculate Correct Responses

Hello, I am trying to count how many times a subject makes a correct switch or a correct stay response in a simple task. I have data on which condition they were in (here, labeled "IMAGINE" and "RECALL"), as well as whether they made a left or right button response, and whether the outcome was... (5 Replies)
Discussion started by: Jahn
5 Replies

4. Shell Programming and Scripting

Calculate P Value -Awk

Is there any awk command to calculate P Value ?(Probability) Is it possib;e to calculate P va;ue for this data for ex? 7.891284 8.148193 7.749575 7.958188 7.887702 7.714877 8.141548 7.51845 8.27736 7.929853 7.92456 8.249126 7.989113 8.012573 8.351206 (2 Replies)
Discussion started by: stateperl
2 Replies

5. Shell Programming and Scripting

awk script to calculate total

Hi First field is the Record Type. A Record Type 5 can have multiple Record Type 6's before another Record Type 5 appears. I want to calculate the total of fields at position 8-11 on Record type 6 when Record Type 5 has a field at position 11-14 equals to '2222'. then it should delete the lines... (2 Replies)
Discussion started by: appsguy616
2 Replies

6. Shell Programming and Scripting

how can i calculate a file using awk

hi all Am new to scripting... So,i have a file named file1 its contents are as follows: joy 55 66 77 ruby 77 88 99 saloni 88 44 33 I would require a script which will calculate its percentage,its total and the average with awk script Many thanks in advance.. Please reply me at... (4 Replies)
Discussion started by: whizkidash
4 Replies

7. Shell Programming and Scripting

How to findDuplicate Records in a text file

Pls Any one help me in (2 Replies)
Discussion started by: G.Aavudai
2 Replies

8. UNIX for Dummies Questions & Answers

Use awk to calculate average of column 3

Suppose I have 500 files in a directory and I need to Use awk to calculate average of column 3 for each of the file, how would I do that? (6 Replies)
Discussion started by: grossgermany
6 Replies

9. Shell Programming and Scripting

How to calculate with awk

Hi, I have below awk statement and I need to convert the second field ( substr($0,8,6))from minutes to hours with 2 decimail place. How can I achieve this? /usr/bin/awk '{print substr($0,23,4),substr($0,8,6)}' /tmp/MANAGER_LIST.$$ >> /tmp/NEWMANAGER_LIST.$$ Thanks for any help! (4 Replies)
Discussion started by: whatisthis
4 Replies

10. Shell Programming and Scripting

How to calculate a sum of certain records?

Hi, i have a file where the records are like this. vt100 2048 D402 MG0010 0 586 262144 D403 MG0011 1000 486 8192 D404 MG0012 270 386 8192 A423 CC0177 40 586 65536 A424 CC0182 670 486 16384 A423 CC0183 100 486 16384 A425 CC0184 80 65000 4096 B407 EE1027 80 I want firstly, to count how... (10 Replies)
Discussion started by: sickboy
10 Replies
Login or Register to Ask a Question