Data analysis, Regular Expression - Unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Data analysis, Regular Expression - Unix
# 1  
Old 07-10-2012
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! Smilie

Last edited by @man; 07-10-2012 at 10:51 AM..
# 2  
Old 07-10-2012
@@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 Smilie

Last edited by PikK45; 07-10-2012 at 10:56 AM.. Reason: Formatting
This User Gave Thanks to PikK45 For This Post:
# 3  
Old 07-10-2012
This is so good Pikk45! Smilie
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?! Smilie
Thanks!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SHELL: UNIX : Ls regular expression not working when used with variables

If i do below command in unix prompt which static values (ie 27..97), it is working fine and gives desired output >ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null somefilename_27.sometxt somefilename_28.sometxt somefilename_29.sometxt .. somefilename_97.sometxt But if i want... (2 Replies)
Discussion started by: haiderali
2 Replies

2. Shell Programming and Scripting

Regular expression in UNIX

How can I define a regular expression of a string which can start with Capital alphabet or integer (A-Z) or (0-9) and can be of any number of characters I have tried * but its not working could anyone please suggest? (2 Replies)
Discussion started by: Pratik4891
2 Replies

3. Shell Programming and Scripting

Pattern search (regular expression in UNIX)

Hello , Could anyone help me to define the string in regular expression way . Below is my string \rtf1\ansi\deff0{\fonttbl{\f0\fswiss Helv;}{\f1\fnil MS Sans Serif;}} {\colortbl ;\red0\green0\blue0;} \viewkind4\uc1\pard\cf1\lang1033\f0\fs16 The string will always start as \rtf1 and... (6 Replies)
Discussion started by: Pratik4891
6 Replies

4. Shell Programming and Scripting

PERL: retrieve the data based on regular expression

Hi Friends i have a code below sample $text contains the values test1 PIC X test1 PIC XX test1 PIC XXX test1 PIC X(8) test1 PIC X(12) test1 PIC X test1 X(8) test1 PIC X VALUE 'N'. $text =~ /^\d{6} +(\d{2}) +(+) +PIC +(+)(\((\d+)\)(V(+)| +(COMP\-3).|\.)|( +(COMP\-3).|... (4 Replies)
Discussion started by: i150371485
4 Replies

5. Shell Programming and Scripting

regular expression with shell script to extract data out of a text file

hi i am trying to extract some specific data out of a text file using regular expressions with shell script that is using a multiline grep .. and the tool i am using is pcregrep so that i can get compatibility with perl's regular expressions for a sample data like this, i am trying to grab... (6 Replies)
Discussion started by: vemkiran
6 Replies

6. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

7. UNIX for Dummies Questions & Answers

Help | Unix | grep | regular expression | backreference | Syntax/Logic

Hello, I'm working on learning regular expressions and what I can do with them. I'm using unix to and its programs to experiment and learn what my limitations are with them. I'm working on duplicating the regular expression: ^(.*)(\r?\n\1)+$ This is supposed to delete duplicate lines... (2 Replies)
Discussion started by: MykC
2 Replies

8. UNIX for Dummies Questions & Answers

Help | unix | grep | regular expression

I have the following code: ls -al /bin | tr -s ' ' | grep 'x' ls -al: Lists all the files in a given director such as /bin tr -s ' ': removes additional spaces between characters so that there is only one space grep 'x': match all "x" characters that are followed by a whitespace. I was... (3 Replies)
Discussion started by: MykC
3 Replies

9. UNIX for Dummies Questions & Answers

HELP | unix | regular expression - How to represent two whitespaces?

ls -l generates something like... -rwxr-xr-x 1 root root 19304 Jan 21 2009 true -rwsr-xr-x 1 root root 40208 Jan 21 2009 umount -rwxr-xr-x 1 root root 22336 Jan 21 2009 uname -rwxr-xr-x 1 root root 1273 Jan 21 2009 unicode_start -rwxr-xr-x 1 root root 79 Jan 21 2009... (3 Replies)
Discussion started by: MykC
3 Replies

10. UNIX for Dummies Questions & Answers

Syntax Help | unix | grep | regular expression | repetition

Hello, This is my first post so, Hello World! Anyways, I'm learning how to use unix and its quickly become apparent that a strong foundation in regular expressions will make things easier. I'm not sure if my syntax is messing things up or my logic is messing things up. ps -e | grep... (4 Replies)
Discussion started by: MykC
4 Replies
Login or Register to Ask a Question