Computing.Net > Forums > Unix > Unix sort

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.

Unix sort

Reply to Message Icon

Name: Andrew Taylor
Date: November 10, 2005 at 06:58:28 Pacific
OS: Solaris UNIX
CPU/Ram: Not Known
Comment:

Hi, I have a file constructed as follows:

header , N1
1 Contents
1220 SPC
8 SSN
1 Contents
1221 SPC
8 SSN
1 Contents
1222 SPC
8 SSN
footer , N2

The values preceeding contents and ssn never change. I have a sed command that inserts a new batch of three 3 rows (contents, spc, ssn) above the line that ends in N2. The problem is that the numbers preceeding SPC must be in ascending numerical order. Sometimes the spc value in the new batch is < the previous spc value giving me something like this:

header , N1
1 Contents
1220 SPC
8 SSN
1 Contents
1221 SPC
8 SSN
1 Contents
1222 SPC
8 SSN
1 Contents
1000 SPC
8 SSN
footer , N2

I therefore want to sort in batches of 3 lines by spc which would give me this:

header , N1
1 Contents
1000 SPC
8 SSN
1 Contents
1220 SPC
8 SSN
1 Contents
1221 SPC
8 SSN
1 Contents
1222 SPC
8 SSN
footer , N2

All is can achieve at the moment is:

header , N1
1 contents
1 contents
1 contents
1 contents
8 SSN
8 SSN
8 SSN
8 SSN
1000 SPC
1220 SPC
1221 SPC
1222 SPC
footer , N1

Does anybody have any ideas how to sort it as I want, or alternatively place the new batch of three lines in the correct order to start with?

Much appreciated

Andrew Taylor



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 10, 2005 at 10:27:16 Pacific
Reply:

I see no way of solving this without breaking apart the data file. It's a kludge, but this appears to work:

1) remove the header and footer, first and last lines:
sed -e '$d' -e '1d' data.file > newdata.file

2) In the directory where data.file resides create a directory, mytmp, and split the newdata.file into files with 3 lines:

split -l 3 newdata.file mytmp/x

3) rename all the files in the mytmp directory to the 'number preceding SPC'

#!/bin/ksh

find mytmp -type f |while read fname
do
mv $fname mytmp/$(sed -n '2p' $fname|awk ' { print $1 } ')
done

4) By default, the ls command sorts alphabetically so you can put the file back together with this:

#!/bin/ksh

ls -1 mytmp|while read ffile
do
cat mytmp/$ffile >> myfile
done
# end script

I'll leave it to you to put the header and footer back into myfile.

Finally, the above should work provided your numbers are four characters. If the number is NOT four characters you'll have to do something like this and sort numerically:

# untested
ls -1 mytmp|sort -n |while read ffile
.
.


0

Response Number 2
Name: Andrew Taylor
Date: November 10, 2005 at 23:38:13 Pacific
Reply:

Thanks for your help, I'll give it a go

regards

Andrew Taylor


0

Response Number 3
Name: Jim Boothe
Date: November 11, 2005 at 07:51:40 Pacific
Reply:

This awk solution outputs each set of 3 lines preceded by the desired sort field, which I have allowed to be as large as 6 digits. After sorting, the prepended sort field is taken off with the cut command.  It assumes that the first line is a header line, but you could easily add an if-statement to confirm that.

awk 'BEGIN \
{getline
 printf "       %s\n",$0}
{if ($1!="footer")
   {l1=$0
    getline
    sortkey=$1
    l2=$0
    getline l3
    printf "%6.6d %s\n",sortkey,l1
    printf "%6.6d %s\n",sortkey,l2
    printf "%6.6d %s\n",sortkey,l3
   }
 else
    printf "zzzzzz %s\n",$0
}
' file.in |
  sort -k1,1 -k3,3n |
  cut -c-8- > file.out

Below is the direct output from awk prior to being piped into sort:

      header , N1
001220 1 Contents
001220 1220 SPC
001220 8 SSN
001221 1 Contents
001221 1221 SPC
001221 8 SSN
001222 1 Contents
001222 1222 SPC
001222 8 SSN
001000 1 Contents
001000 1000 SPC
001000 8 SSN
zzzzzz footer , N2


0

Response Number 4
Name: Ibaul
Date: December 7, 2005 at 15:39:40 Pacific
Reply:

It doesn't work on my comp.

Ibaul, wbr


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Unix sort

Unix sort - limitations www.computing.net/answers/unix/unix-sort-limitations/7173.html

Need help on sort cmd in unix AIX www.computing.net/answers/unix/need-help-on-sort-cmd-in-unix-aix/6504.html

Sort Order Presevation www.computing.net/answers/unix/sort-order-presevation/7123.html