Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-10-2012
Registered User
 
Join Date: Jul 2012
Location: Sweden
Posts: 52
Thanks: 25
Thanked 0 Times in 0 Posts
Question if statement? Bash scripting :|

Hey every one!
I have a dataset like this :

Code:
1	100	1	0
5	100	1	8
7	50	1	0
7	100	2	0
10	20	1	8
10	30	1	8
10	100	3	8
15	50	5	0
20	90	1	0
20	99	9	0

I wanna check if the 4th column is 0 or 8
If it's zero write the 1st column itself, if it's 8 write sum of 1st and second
something like this:


Code:
1	1	0
105	1	8
7	1	0
7	2	0
30	1	8
40	1	8
110	3	8
15	5	0
20	1	0
20	9	0

Then after sorting based on first column write the first column and sum of numbers for the same values in 1st column in 2nd one to the output file. Like this:


Code:
1	1	0
7	3	0
15	5	0
20	10	0
30	1	8
40	1	8
105	1	8
110	3	8

At the end I wanna split the file to two files regarding the value in the 3rd column.

If you can help me with this, you saved my life!

Last edited by @man; 07-10-2012 at 09:51 AM..
Sponsored Links
    #2  
Old 07-10-2012
Registered User
 
Join Date: Jul 2012
Location: Chennai
Posts: 506
Thanks: 37
Thanked 64 Times in 62 Posts
@@man:
this should help you.

Code:
 awk '{if($4==8){i=$1+$2;} else{i=$1;} print i"\t"$3"\t"$4}' man.txt | sort -nk 3

Input:

Code:
1      100    1    0
5      100    1    8
7      50      1    0
7      100    2    0
10    20      1    8
10    30      1    8
10    100    3    8
15    50      5    0
20    90      1    0
20    99      9    0

Output:

Code:
1        1    0
15      5    0
20      1    0
20      9    0
7        1    0
7        2    0
105    1    8
110    3    8
30      1    8
40      1    8

May be there are faster ways than this

Last edited by PikK45; 07-10-2012 at 09:56 AM.. Reason: Formatting
The Following User Says Thank You to PikK45 For This Useful Post:
@man (07-10-2012)
Sponsored Links
    #3  
Old 07-10-2012
Registered User
 
Join Date: Jul 2012
Location: Sweden
Posts: 52
Thanks: 25
Thanked 0 Times in 0 Posts
This is so good Pikk45!
But can you also help me for the last step? I want the to sum the numbers in column2 for the same values in column1 and have something like this:

Code:
1	1	0
7	3	0
15	5	0
20	10	0
30	1	8
40	1	8
105	1	8
110	3	8

Can you help me with that also?!
Thanks!!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regular expression with shell script to extract data out of a text file vemkiran Shell Programming and Scripting 6 06-26-2012 07:41 AM
Perl: How to read from a file, do regular expression and then replace the found regular expression jessy83 Programming 1 12-05-2011 03:10 PM
Help | unix | grep | regular expression MykC UNIX for Dummies Questions & Answers 3 10-12-2009 08:09 PM
HELP | unix | regular expression - How to represent two whitespaces? MykC UNIX for Dummies Questions & Answers 3 10-12-2009 04:51 PM
Syntax Help | unix | grep | regular expression | repetition MykC UNIX for Dummies Questions & Answers 4 10-11-2009 09:54 PM



All times are GMT -4. The time now is 03:57 AM.