Computing.Net > Forums > Unix > Sorting lines based on strict rule

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sorting lines based on strict rule

Reply to Message Icon

Name: balmbkm
Date: May 11, 2009 at 09:06:47 Pacific
OS: SUN
CPU/Ram: 2G
Product: Sun / Ultraax-i2
Subcategory: General
Comment:

Hello,

I need some help here please. I wrote a script that generates a fanout ratio, basically how many servers beind an FA pair (rule of 17 applies). Here is report.out
Type DIR Port Total
-----------------------------
HP 11A 0 0
HP 11A 1 0
HP 12B 0 10
HP 12B 1 14
HP 13B 0 8
HP 14B 0 12
HP 3B 0 13
HP 4B 0 8
HP 5B 0 10
HP 5B 1 15
HP 6A 0 0
HP 6A 1 0
SWAL 12A 0 4
SWAL 12A 1 2
SWAL 13A 0 7
SWAL 13A 1 12
SWAL 13B 1 8
SWAL 14A 0 14
SWAL 14A 1 0
SWAL 14B 1 3
SWAL 3A 0 14
SWAL 3A 1 0
SWAL 3B 1 3
SWAL 4A 0 7
SWAL 4A 1 12
SWAL 4B 1 8
SWAL 5A 0 4
SWAL 5A 1 2

I want to print this report using this pattern bellow:
HP 11A 0 0
HP 6A 0 0
HP 11A 1 0
HP 6A 1 0
HP 12B 0 10
HP 5B 0 10

so on ... You can see on column2 a patern of 17 for each two lines.

Thank you in advance



Sponsored Link
Ads by Google

Response Number 1
Name: balmbkm
Date: May 12, 2009 at 07:52:51 Pacific
Reply:

let me be more explicit. The rule of 17 is as follow:

Two FA pairs are like 11A/6A or 13B/4B or 14C/3C ....and a pair is either using port 0 or 1 on third column of file I uploaded. So having a sorted report make it much easier to see hosts distribution on each pair.

Any help I would appreciate.


0

Response Number 2
Name: lankrypt0
Date: May 12, 2009 at 08:26:23 Pacific
Reply:

ok, I think I get it now, let me know if this is correct. you want to sort based on: the sum of two rows, whose second column adds up to 17 AND where both rows have a 0 or a 1 on the third column?

What about the SWAL ones? Does the letter after the number in the second column make and difference?


0

Response Number 3
Name: balmbkm
Date: May 12, 2009 at 08:44:14 Pacific
Reply:

That is exactly correct.

A pair is like this:

HP 10A 0
HP 7A 0


0

Response Number 4
Name: lankrypt0
Date: May 12, 2009 at 09:00:55 Pacific
Reply:

ok, give me a bit, ill take a whack at it.


0

Response Number 5
Name: lankrypt0
Date: May 12, 2009 at 10:52:00 Pacific
Reply:

Ok, i pretty much have it done. just a few questions
1) Will something in the HP section ever match something in the SWAL section?
2) Would there ever be more than one match? Say:
HP 11A 0 0
HP 6A 0 0
HP 6A 0 8
3) Do the letters in the second column, after the number, have to match? Or can you have
HP 11A 0 0
HP 6B 0 0


0

Related Posts

See More



Response Number 6
Name: balmbkm
Date: May 12, 2009 at 11:11:26 Pacific
Reply:

Nope. The HP section is dedicated to HP but randomly defined on each storage array. This example you asked here is not a pair.

HP 11A 0 0
HP 6B 0 0

btw SWAL means SUN, WINDOWS,AIX, LINUX

on that section too you will have it displayed as

SWAL 12A 1 2
SWAL 5A 1 2
SWAL 13A 0 7
SWAL 4A 0 7
SWAL 13C 0 3
SWAL 4C 0 3
SWAL 8C 0 2
SWAL 9C 0 2

so on ....

What is 100% sure is file will always have an even # of lines as it must meet the 17 rule for a pair.

Thanks,


0

Response Number 7
Name: lankrypt0
Date: May 12, 2009 at 11:29:04 Pacific
Reply:

ok, try the following script. all you need to is enter the input filename as an argument when running the script

#!/usr/bin/ksh
fname=$1
if [[ $1 = "" ]];then
print "Enter a filename!"
else
cp $fname $fname.tmp
while read lines;do
## Break up the fields
 first=$(print $lines|cut -f1 -d" " )
 second=$(print $lines|cut -f2 -d" " )
 third=$(print $lines|cut -f3 -d" " )
 fourth=$(print $lines|cut -f4 -d" " )

## Break up the second field
 secnum=$(print $second|tr -d "[:alpha:]")
 seclet=$(print $second|tr -d "[:digit:]")

## Find what is needed
 numneed=$((17-$secnum))

## Find needed line
 isneeded="$first $numneed$seclet $third $fourth"
 isfound=$(grep "$isneeded" $fname.tmp)
 if [[ $isfound != "" ]];then
  print $first $second $third $fourth
  print $isneeded
  print ""

## Ensure no duplicates
  grep -v "$isneeded" $fname.tmp |grep -v "$first $second $third $fourth" > $fname.t
  mv $fname.t $fname.tmp
 fi
done < $fname
rm $fname.tmp
fi

also, if you don't mind, let me know if that worked.


0

Response Number 8
Name: balmbkm
Date: May 12, 2009 at 12:00:29 Pacific
Reply:

ok I ran the script and the output looks like this:
HP 10B 0 5
HP 10C 1 0
HP 7B 0 5
HP 7C 1 0
HP 8B 0 14
HP 9B 0 14
SWAL 10A 0 16
SWAL 10A 1 8
SWAL 10B 1 0
SWAL 10C 0 0
SWAL 7A 0 15
SWAL 7A 1 7
SWAL 7B 1 0
SWAL 7C 0 0
SWAL 8A 0 15
SWAL 8A 1 7
SWAL 8B 1 0
SWAL 8C 0 0
SWAL 8C 1 0
SWAL 8D 0 0
SWAL 8D 1 0
SWAL 9A 0 15
SWAL 9A 1 7
SWAL 9B 1 0
SWAL 9C 0 0
SWAL 9C 1 0
SWAL 9D 0 0
SWAL 9D 1 0

That is huge progress though and thank you for that. Here is what I expect to get:

HP 10B 0 5
HP 7B 0 5
HP 10C 1 0
HP 7C 1 0
HP 8B 0 14
HP 9B 0 14
SWAL 10A 0 16
SWAL 7A 0 15
SWAL 10A 1 8
SWAL 7A 1 7
SWAL 10B 1 0
SWAL 7B 1 0
SWAL 10C 0 0
SWAL 7C 0 0
SWAL 8A 0 15
SWAL 9A 0 15
SWAL 8A 1 7
SWAL 9A 1 7
SWAL 8B 1 0
SWAL 9B 1 0
SWAL 8C 0 0
SWAL 9C 0 0
SWAL 8C 1 0
SWAL 9C 1 0
SWAL 8D 0 0
SWAL 9D 0 0
SWAL 8D 1 0
SWAL 9D 1 0

You can see the patern on field 2,3 for each two lines from top to bottom.

Thank you again.


0

Response Number 9
Name: lankrypt0
Date: May 12, 2009 at 12:07:41 Pacific
Reply:

thats strange, it comes out ok when i run it based on the example you gave. does it throw any errors when you run it? you copied the script exactly? can you make sure tr and cut both work? independent of the script?


0

Response Number 10
Name: lankrypt0
Date: May 12, 2009 at 12:12:13 Pacific
Reply:

just tried it with the latest info you gave and got:

HP 10B 0 5
HP 7B 0 5

HP 10C 1 0
HP 7C 1 0

HP 8B 0 14
HP 9B 0 14

SWAL 10B 1 0
SWAL 7B 1 0

SWAL 10C 0 0
SWAL 7C 0 0

SWAL 8A 0 15
SWAL 9A 0 15

SWAL 8A 1 7
SWAL 9A 1 7

SWAL 8B 1 0
SWAL 9B 1 0

SWAL 8C 0 0
SWAL 9C 0 0

SWAL 8C 1 0
SWAL 9C 1 0

SWAL 8D 0 0
SWAL 9D 0 0

SWAL 8D 1 0
SWAL 9D 1 0

Remember, the output wont be in a file anywhere, it just displays live to the screen. to put it to a file run it with: scriptname > outputfile


0

Response Number 11
Name: balmbkm
Date: May 12, 2009 at 12:41:38 Pacific
Reply:

I am running it on a sun server like this:

./read.ksh 3449:FANOUT:051209.out > read.out

at them end of script, file is zero byte


0

Response Number 12
Name: balmbkm
Date: May 12, 2009 at 12:43:03 Pacific
Reply:

also the output is not displaying on my screen if not redirected to a file unless I ran ksh -x .....


0

Response Number 13
Name: lankrypt0
Date: May 12, 2009 at 12:54:23 Pacific
Reply:

I am just starting to see if there is any difference for KSH on sun, but I can see on my box that the filename 3449:FANOUT:051209.out causes a problem. can you try to rename it to something like file.out and try it?


0

Response Number 14
Name: lankrypt0
Date: May 12, 2009 at 13:03:50 Pacific
Reply:

Looking around, there may be a KSH version difference, taken from:
http://www.unix.com/sun-solaris/369...

the default ksh on solaris is a posix ksh88 (/usr/bin/ksh)

but there is another ksh on solaris, the ksh93  called dtksh (/usr/dt/bin/dtksh)

so failing all else, you may want to try:
/usr/dt/bin/dtksh


0

Response Number 15
Name: balmbkm
Date: May 12, 2009 at 13:30:06 Pacific
Reply:

I renamed the file and moved it to an AIX 5.1 and same result. I am puzzled by it. why it is not prining all the print statements to either screen or a file.

I have a question though would it be better to read to read 4 parameters instead of an entire line? Also I have seen in the past on Solaris reading the input file with < at end of the while loop.

I am not geeting any error while running it but just zero byte output file.

When I get home tonight I will do more investigation and let you know.

I REALLY appreciated your help.


0

Response Number 16
Name: lankrypt0
Date: May 12, 2009 at 15:03:41 Pacific
Reply:

sure it's worth a shot, i've always just been used to doing it the other way, old habits and all:

#!/usr/bin/ksh
fname=$1
if [[ $1 = "" ]];then
print "Enter a filename!"
else
cp $fname $fname.tmp
while read first second third fourth;do

## Break up the second field
 secnum=$(print $second|tr -d "[:alpha:]")
 seclet=$(print $second|tr -d "[:digit:]")

## Find what is needed
 numneed=$((17-$secnum))

## Find needed line
 isneeded="$first $numneed$seclet $third $fourth"
 isfound=$(grep "$isneeded" $fname.tmp)
 if [[ $isfound != "" ]];then
  print $first $second $third $fourth
  print $isneeded
  print ""

## Ensure no duplicates
grep -v "$isneeded" $fname.tmp |grep -v "$first $second $third $fourth" > $fname.t
  mv $fname.t $fname.tmp 
 fi
done < $fname
rm $fname.tmp
fi

if you want to contact me directly, feel free to do so at:
lankrypt0 at gmail dot com


0

Response Number 17
Name: lankrypt0
Date: May 12, 2009 at 15:04:28 Pacific
Reply:

and for curiosity's sake, if the above doesnt work see if dtksh is on the machine and works.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Sorting lines based on strict rule

Remove Lines based on a file www.computing.net/answers/unix/remove-lines-based-on-a-file/7419.html

renaming files based on date arguments www.computing.net/answers/unix/renaming-files-based-on-date-arguments/2499.html

Replace based on position www.computing.net/answers/unix/replace-based-on-position/7230.html