Visit Our UNIX and Linux User Community


Sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting
# 1  
Old 05-17-2010
Sorting

Let's say that I have a database that I call part ID. This database has the following grouping:

Code:
Dart1=4
Dart2=8
Dart3=12

Fork1=68
Fork2=72
Fork3=64

Bike1=28
Bike2=24
Bike3=20

Car1=44
Car2=40
Car3=36

I want to write a program that would read this database and tell me when the 3rd item of each group is smaller than the second item of the same group or when the 1st item is greater than the 2nd item of that group. Basically item1 < item2 < item3 is the standard within the same group. If this is not met within that group, The script would print the output of all the groups that are different. For example, the output would be:
Code:
Fork1=68
Fork2=72          
Fork3=64

Car1=44
Car2=40
Car3=36

Any suggestions will be appreciated!

Thanks!

Last edited by Scott; 05-18-2010 at 01:28 PM.. Reason: Code tags, please...
# 2  
Old 05-18-2010
Quote:
Originally Posted by Ernst
... Basically item1 < item2 < item3 is the standard within the same group. If this is not met within that group, The script would print the output of all the groups that are different. ...
One way to do it with Perl -

Code:
$ 
$ cat f9
Dart1=4
Dart2=8
Dart3=12

Fork1=68
Fork2=72
Fork3=64

Bike1=28
Bike2=24
Bike3=20

Car1=44
Car2=40
Car3=36
$ 
$ perl -lne 'BEGIN {undef $/} while (/(\w+\d=(\d+)\n\w+\d=(\d+)\n\w+\d=(\d+))/sg) {print $1,"\n" if $2>=$3 or $3>=$4}' f9
Fork1=68
Fork2=72
Fork3=64

Bike1=28
Bike2=24
Bike3=20

Car1=44
Car2=40
Car3=36

$ 
$

tyler_durden
# 3  
Old 05-18-2010
Code:
awk 'BEGIN{RS="";FS="="} {if ($2>$4 || $4>$6) print $0"\n" }' urfile

# 4  
Old 05-18-2010
Tyler,

Your script works. But I still have some issues. After I ran my surveys, my output file is as follows:
F
Code:
ork,1,68
Fork,2,72
Fork,3,64

Bike,1,28
Bike,2,24
Bike,3,20

Car,1,44
Car,2,40
Car,3,36

I used this sed command
Code:
sed -e 's/,/./' -e 's/,/=/' filename >> SW1_filename2


to make it look like this
Code:
Fork1=68
Fork2=72
Fork3=64

Bike1=28
Bike2=24
Bike3=20

Car1=44
Car2=40
Car3=36

However it won’t do it. With the commas in the format, your script does not work; with the equal sign, it works. Any idea what I am doing wrong?

---------- Post updated at 11:24 AM ---------- Previous update was at 09:46 AM ----------

Disregard my previous posting. I mistakenly enterer a comma instead of a semicolon. That's why it was not working.

Thanks!

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


Now, if you could explain the script to me, that would help. I need to modify it somehow. Basically what I am trying to do is to the following:
The output data for all the groups has 6 entries.But while most of the groups has only three entries filled, some groups have all 6 entries filled. What I want to do is Whenever the script reads the file and sees a group with only 3 entries filled, it needs to print the three rows that have data but strip the other three empty rows; for the groups with 6 entries filled, it just needs to print them out. That would be fantastic if you or someone else could help.


Thanks!

---------- Post updated at 07:56 PM ---------- Previous update was at 11:31 AM ----------

Hey rdcwayx,
I tried your script but got this error message.
Code:
awk: record `1.3=12
1.2=348
1.1=180
...' too long

For your info, my input file format was as follows:
Code:
554.3=432
554.2=264
554.1=96
555.3=452
555.2=284
555.1=116
556.3=488
556.2=320
556.1=152
557.3=340
557.2=172
557.1=4
558.3=356
558.2=188
558.1=20
559.3=108
559.2=276
559.1=444

Keep in mind that this represents a fraction of a huge database.

Let me know what you think.

Thanks!

Last edited by Scott; 05-19-2010 at 07:32 PM.. Reason: Code tags again...
# 5  
Old 05-19-2010
Quote:
Originally Posted by Ernst
... Basically what I am trying to do is to the following:
The output data for all the groups has 6 entries.But while most of the groups has only three entries filled, some groups have all 6 entries filled. What I want to do is Whenever the script reads the file and sees a group with only 3 entries filled, it needs to print the three rows that have data but strip the other three empty rows; for the groups with 6 entries filled, it just needs to print them out. ...

For your info, my input file format was as follows:
554.3=432
554.2=264
554.1=96
555.3=452
555.2=284
555.1=116
556.3=488
556.2=320
556.1=152
557.3=340
557.2=172
557.1=4
558.3=356
558.2=188
558.1=20
559.3=108
559.2=276
559.1=444

...
Firstly, the Perl one-liner posted earlier will *not* work for groups of 6 lines. It works *only* for groups of 3 lines.

Secondly, it may be beneficial if you could post some test data for groups of 3 lines as well as 6 lines. And post the output as well that shows what exactly is to be done for each group.

The input you've posted above is difficult to comprehend because it does not have the blank line that separates one group from the other.

tyler_durden
# 6  
Old 05-19-2010
Okay, I have 6 groups. Now I want the script to go through these groups and look at the structure of each group. If the .1 (within a group) is greater than the .2 or .3 OR if .2 is greater than .3 within a group; thus output these groups. In our case the output would be groups.

input file
Code:
1.6=176
1.5=172
1.4=168
1.3=14
1.2=13
1.1=12

230.3=146
230.2=147
230.1=148

3.3=20
3.2=19
3.1=18

5.6=166
5.5=122
5.4=160
5.3=103
5.2=102
5.1=100

100.6=176
100.5=172
100.4=168
100.3=20
100.2=12
100.1=16

117.3=24
117.2=82
117.1=79

Output file
Code:
117.3=83
117.2=82
117.1=79

100.6=176
100.5=172
100.4=168
100.3=20
100.2=12
100.1=16

230.3=146
230.2=147
230.1=148

I hope it is clear enough.

Last edited by vgersh99; 05-19-2010 at 07:29 PM.. Reason: code tags, please!
# 7  
Old 05-19-2010
nawk -f ernst.awk inputFile

ernst.awk:
Code:
BEGIN {
  RS=FS=""
  ORS="\n\n"
}
{
  n=split($NF,t, "=")
  one=t[n]
  n=split($(NF-1),t, "=")
  two=t[n]
  n=split($(NF-2),t, "=")
  three=t[n]

  if (one > two || one > three || two > three) print
}


Previous Thread | Next Thread
Test Your Knowledge in Computers #209
Difficulty: Medium
The development work for OSPF prior to its publication as open standard was undertaken largely by the Digital Equipment Corporation.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sorting

Hii guys, I need to sort my file and remove duplicates before writing to another file. The first line in the file are column names. I dont want this line to be sorted and should always be the first line in the output. sort -u file.txt > file1.txt. is the command that i am using... (4 Replies)
Discussion started by: just4u_sharath
4 Replies

2. Shell Programming and Scripting

sorting help

Hi, Please i need help in writing an 'awk' script in sorting the following data; traceroute6 to 2001:1ba0:2a0:5965:0:30:24:1 (2001:1ba0:2a0:5965:0:30:24:1) from 2001:418:1::62, 64 hops max, 16 byte packets 1 2001:418:1::4 0.342 ms 2 2001:418:1::1 0.630 ms 3 2001:504:16::1b1b 0.393 ms 4... (6 Replies)
Discussion started by: sam127
6 Replies

3. Shell Programming and Scripting

sorting

Hi all, Does anyone can help me the following question? I would like to write an AWK script. In the following input file, each number in "start" is paired with numbers in column "end". No Start End A 22,222,33,22,1233,3232,44 555,333,222,55,1235,3235,66... (7 Replies)
Discussion started by: phoeberunner
7 Replies

4. Shell Programming and Scripting

Sorting HELP

Hi, I have posted related topic but as i continue the research I find more need to sort the data. AS(2607:f278:4101:11:dead:beef:f00f:f), AS786 AS6453 AS7575 AS7922 AS(2607:f2e0:f:1db::16), AS786 AS3257 AS36252 AS786 AS3257 AS36252 AS(2607:f2f8:1700::2), AS786 AS6939 AS25795 ... (6 Replies)
Discussion started by: sam127
6 Replies

5. UNIX for Advanced & Expert Users

HELP on sorting

hi everyone, I am kind of new to this forum. I need help in sorting this data out accordingly, I am actually doing a traceroute application and wants my AS path displayed in front of my address like this; 192.168.1.1 AS28513 AS65534 AS5089 AS5089 .... till the last AS number and if possible... (1 Reply)
Discussion started by: sam127
1 Replies

6. UNIX for Dummies Questions & Answers

HELP on sorting

hi everyone, I am kind of new to this forum. I need help in sorting this data out accordingly, I am actually doing a traceroute application and wants my AS path displayed in front of my address like this; 192.168.1.1 AS28513 AS65534 AS5089 AS5089 .... till the last AS number and if possible... (1 Reply)
Discussion started by: sam127
1 Replies

7. Homework & Coursework Questions

Sorting help

i have list of files: Wang De Wong CVPR 09.pdf Yaacob AFGR 99 Second edition.pdf Shimon CVPR 01.pdf Den CCC 97 long one.pdf Ronald De Bour CSPP 04.pdf ..... how can i sort this directory so the output will be in the next format: <year>\t<conference/journal>\t<author list> - t is tab (its... (1 Reply)
Discussion started by: nirnir26
1 Replies

8. UNIX for Dummies Questions & Answers

Sorting help

i have list of files: Wang De Wong CVPR 09.pdf Yaacob AFGR 99 Second edition.pdf Shimon CVPR 01.pdf Den CCC 97 long one.pdf Ronald De Bour CSPP 04.pdf ..... how can i sort this directory so the output will be in the next format: <year>\t<conference/journal>\t<author list> - t is tab (its... (1 Reply)
Discussion started by: nirnir26
1 Replies

9. UNIX for Dummies Questions & Answers

Sorting help

how can i sort the next list just by look at the numbers (ignore letters) example: abc123 dff4f aaa2aa bbbb55555bb output: aaa2aa dff4f abc123 bbbb55555bb (1 Reply)
Discussion started by: nirnir26
1 Replies

10. Shell Programming and Scripting

Need immediate help with sorting!!!

hey, I have a file that looks smthng like this: /*--- abcd_0050 ---*/ asdfjk adsfkja lkjljgafsd /*---abcd_0005 ---*/ lkjkljbfkgj ldfksjgf dfkgfjb /*-- abcd_0055--*/ klhfdghd dflkjgd jfdg I would like it to be sorted so that it looks like this: /*---abcd_0005 ---*/ lkjkljbfkgj (9 Replies)
Discussion started by: sasuke_uchiha
9 Replies

Featured Tech Videos