cut and sort


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users cut and sort
# 1  
Old 08-26-2011
cut and sort

Please help.

I have a file containing rows of information. The row needs to be broken down into blocks of 5 and then sorted.
Example:
Code:
10381                                                                           
1042010046                                                                      
10391100211037010055                                                            
10391100211037010055                                                            
10373                                                                           
1032510055

I need it to look as follows:
Code:
10381                                                                           
1004610420                                                                      
10021100551037010391                                                               
10021100551037010391                                                            
10373                                                                           
1005510325

I'm sure there is a way of cutting the row up (also need to cater for any number of blocks of 5 chars in a row. Could be from 5 to 35 chars). Or using awk but I am struggling to find it.

Last edited by radoulov; 08-26-2011 at 03:30 PM.. Reason: Code tags.
# 2  
Old 08-26-2011
Code:
perl -lne 'print sort {$a <=> $b} /.{5}/g' INPUTFILE

I don't see a easy way to do it with shell and awk.

----

You do not need numeric sorting for numbers with the same number of digits. So:
Code:
perl -lne 'print sort /.{5}/g' INPUTFILE


Last edited by yazu; 08-26-2011 at 11:59 AM.. Reason: You do not need numeric sorting for numbers with the same number of digits.
# 3  
Old 08-26-2011
Quote:
Originally Posted by yazu
I don't see a easy way to do it with shell and awk.
I cannot think of a shell-based approach that is as succinct and efficient as your perl solution, but the following isn't too bad (with regard to complexity):

Code:
while read x; do
    printf '%s\n' "$x" | fold -w5 | sort -n | paste -s - | tr -d \\t
done < INPUTFILE

Quote:
Originally Posted by yazu
You do not need numeric sorting for numbers with the same number of digits.
Hmmmm. Hypothetically, it's possible for someone to create (if it doesn't already exist) a locale in which the collation sequence specified breaks the numerical sort of equi-width numbers. The C/POSIX 0,1,2...,8,9 order is not mandated. Although, I can't think of a reason why anyone would deviate from that sequence.

I suspect that, in practice, you are probably 100% correct, but I decided to play it safe with -n.

Regards,
Alister
# 4  
Old 08-26-2011
With the latest GNU awk 4.0.0:

Code:
awk 'BEGIN {
  PROCINFO["sorted_in"] = "@val_num_asc"
  }
{
  patsplit($0, x, /...../)
  for (X in x) printf "%s", x[X]
  print null
  }'  infile

# 5  
Old 08-26-2011
You can do it in awk too as long as you sort inside of it...
Code:
awk '{
   n=gsub(".{5}","& ")
   if (n==1)
      print
   else {
      for (i=1;i<=NF;i++) x[i]=$i
      for (j=1;j<NF;j++)
         for (k=1;k<=NF-j;k++)
            if (x[k] > x[k+1]) {
               t=x[k]
               x[k]=x[k+1]
               x[k+1]=t
            }
      for (i=1;i<=NF;i++)
         printf("%s",i==NF?x[i]"\n":x[i])
  }
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

3. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

4. Homework & Coursework Questions

Help with cut and sort

<B>andan100:Anders:Andersson:800101-1234:TNCCC_1:TDDB46 TDDB80:berbe101:Bertil:Bertilsson:800102-1234:TNCCC_1:TDDB46 TDDB80:The top is how it looks right now I want it t look like this under and I want it to be sorted. I have tried with cut -f -d studenter.txt and so on but it still doesnt work... (2 Replies)
Discussion started by: aannaann
2 Replies

5. UNIX for Dummies Questions & Answers

Using the Sort, Cut, and Awk commands

Hello, I am trying, utilizing the few commands I know, to extract all records within my file that were generated in November of 2007. Each record within the file has a "date" field giving the month, day, and year (9-8-88). How do I extract those records to their own file? Once I extract... (4 Replies)
Discussion started by: babbabooey
4 Replies

6. Solaris

concatenate/sort/cut

I have the following requirement. 1. I have to concatenate the 10 fixed width files. 2. sort based on first 10 characters 3. after that i have remove first 10 chacters from the file. can you please tell me how to do it. Thanks in Advance Samba (1 Reply)
Discussion started by: samba
1 Replies

7. Shell Programming and Scripting

sort / cut question

Hi All, I have a small problem, hope you can help me out here. I have a file that contains the same format of lines in 99% of the cases. 906516 XYZ.NNN V 0000 20070711164648 userID1 userID2 hostname 20070711164641 There are unfortunately several lines with these... (5 Replies)
Discussion started by: BearCheese
5 Replies

8. Shell Programming and Scripting

using cut command with sort

how to cut for pattern in the file and then count each occurance? say, each line has unique pattern and u want to grep but at last, you want to see how many of them occur? say, cut -d'\" -f15 filename | sort -? or.. do i need to use sed or something.. i need to count lets say how... (5 Replies)
Discussion started by: hankooknara
5 Replies

9. UNIX for Dummies Questions & Answers

Help with Last,uniq, sort and cut

Using the last, uniq, sort and cut commands, determine how many times the different users have logged in. I know how to use the last command and cut command... i came up with last | cut -f1 -d" " | uniq i dont know if this is right, can someone please help me... thanks (1 Reply)
Discussion started by: jay1228
1 Replies
Login or Register to Ask a Question