Adding data from a file based on some condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding data from a file based on some condition
# 1  
Old 02-12-2014
Adding data from a file based on some condition

I collect data in a file in below format(Month Day Year Size) in RedHat Linux. Now i want to calculate the data size date wise. As i code shell script after long time, i forgot the features and syntax. Can anyone help me regard this please.

Code:
Feb 8 2014 15
Feb 10 2014 32
Feb 10 2014 32
Feb 12 2014 15
Feb 12 2014 15

---------- Post updated at 11:24 AM ---------- Previous update was at 10:57 AM ----------

Data collected for only 5 days. So, i have to calculate with more data (for single day, may be 5-10 entry)

---------- Post updated at 11:48 AM ---------- Previous update was at 11:24 AM ----------

Output format will be like this:

Code:
Feb 8 2014 15
Feb 10 2014 64
Feb 12 2014 30


Last edited by Scrutinizer; 02-12-2014 at 01:53 AM.. Reason: code tags
# 2  
Old 02-12-2014
Try :

Code:
[akshay@aix tmp]$ cat file
Feb 8 2014 15
Feb 10 2014 32
Feb 10 2014 32
Feb 12 2014 15
Feb 12 2014 15

Code:
[akshay@aix tmp]$ awk 'FNR==NR{A[$1 FS $2 FS $3]+=$4;next}{f=(($1 FS $2 FS $3) in A)}f{$4 = A[$1 FS $2 FS $3];delete A[$1 FS $2 FS $3]}f' file file
Feb 8 2014 15
Feb 10 2014 64
Feb 12 2014 30

---------- Post updated at 12:30 PM ---------- Previous update was at 12:27 PM ----------

If order doesn't matter try this

Code:
[akshay@aix tmp]$ awk '{A[$1 FS $2 FS $3]+=$4}END{for(i in A)print i,A[i]}' file
Feb 12 2014 30
Feb 10 2014 64
Feb 8 2014 15

# 3  
Old 02-12-2014
If dates are grouped together try:
Code:
awk '{v=$NF; $NF=x; i=$0} i!=p{if(p)print p t; p=i; t=0} {t+=v} END{print p t}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding Extra Column in txt file base on Condition

HI Guys, I have below input. Output Base on Below Condition. 1> if forth column is empty and next coming line have same name with \es then add that column name on all rows 2>rest of all are es:vsDataEUtranCellFDD Input:- CCL01736 CCL01736_7A_1 es:vsDataEUtranCellFDD ... (3 Replies)
Discussion started by: pareshkp
3 Replies

2. Shell Programming and Scripting

Getting data from a flat file based on condition

Hi, I have a flaty file from which i am fetching few columns in tablular form as below code. Now i want to fetch the column 6 and 7 in below code only if it either of them is non zero.However below startement awk -F, '$6==0 && $7==0{exit 1}' ${IFILE} is not working..Not sure where is the... (36 Replies)
Discussion started by: Vivekit82
36 Replies

3. UNIX for Dummies Questions & Answers

Deleting the unwanted data based on condition

hi i have my input data like this aaa bbb ccc asa dff nmj mnj saa dff oik aax cdx saa oik asq sdf dssi want my output file to be like this mnj saa dff oik aax cdx saa oiki want to retain only those lines which will have oik just below them and i want oik to be as next column to those... (1 Reply)
Discussion started by: anurupa777
1 Replies

4. Shell Programming and Scripting

Help in adding a data after a particular line of data in a file.

Hi.. I'm into a bump after trying to solve this prob.. i've a file with contents like below. <blankline> 'pgmId' : 'UNIX', 'pgmData' : 'textfile', 'author' : 'admin', ....... Now i'm trying to insert a new data after pgmId. so the final output will be... (7 Replies)
Discussion started by: arjun_arippa
7 Replies

5. Shell Programming and Scripting

Help with File processing - Adding predefined text to particular record based on condition

I am generating a output: Name Count_1 Count_2 abc 12 12 def 15 14 ghi 16 16 jkl 18 18 mno 7 5 I am sending the output in html email, I want to add the code: <font color="red"> NAME COLUMN record </font> for the Name... (8 Replies)
Discussion started by: karumudi7
8 Replies

6. Shell Programming and Scripting

How can I change file value based on condition

Hi, Gurus, I got a problem to resolve following issue: I have one file file1as following: start_dt=2010-01-01 12:00:02 start_dt=2011-01-01 09:00:02 start_dt=2009-01-01 11:00:02I have another file file2 as following: title1, 2010-01-03 10:00:02 title2, 2011-01-04 11:00:02 title3,... (5 Replies)
Discussion started by: ken002
5 Replies

7. Shell Programming and Scripting

Update a field in a file based on condition

Hi i am new to scripting. i have a file file.dat with content as : CONTENT_STORAGE PERCENTAGE FLAG: /storage_01 64% 0 /storage_02 17% 1 I need to update the value of FLAG for a particular CONTENT_STORAGE value I have written the following code #!/bin/sh threshold=20... (1 Reply)
Discussion started by: kichu
1 Replies

8. Shell Programming and Scripting

Copy input file based on condition

Hi, I am new to unix shell programming. I want to write a shell script for a functionality existing in mainframe system. I have one file as below as input 123456 &__987 &12yuq abcdef _ referes to blank condition:whenever the input file is having &__ ,it should be replaced... (4 Replies)
Discussion started by: charan0703
4 Replies

9. Shell Programming and Scripting

Spliting file based on condition

Hi, I have a comma separated file with millions of records in it. I have a requirement to split the file based on the value in a one of the columns. Suppose i have a text file with columns like C1, C2,C3,C4 Column C4 can hold the values either 01 or 02 03 or 04. I nned to extract... (2 Replies)
Discussion started by: Raamc
2 Replies

10. Shell Programming and Scripting

Read file based on condition

Hi Friends, Can any one help with this: I have a huge file with the format as A SAM 4637 B DEPT1 4758 MILAN A SMITH 46585 B DEPT2 5385 HARRYIS B SAMUL 63547 GEORGE B DANIEL 899 BOISE A FRES 736 74638 I have to read this file and write only the records that starts with "B" only ... (5 Replies)
Discussion started by: sbasetty
5 Replies
Login or Register to Ask a Question