sorting fields of a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sorting fields of a line
# 1  
Old 03-11-2008
sorting fields of a line

Hi all,

I have a file that looks like this...

##########
1zz2_15-43 1ouy_0-13-35 1.12619901947
2gfs_41-7 1yqj_3 0.793602121208
1bl7_11-3 1wbo_1-3-4 0.791065168287
1ywr_16-3 2ghl_22 0.956896171134
2exc_11-35 1pmq_13-15-87 0.597677672501
2bal_25-7 1ouk_17-19-21-228-58 0.668388304836
##########

I would like to sort the 1st and 2nd field alphanumerically so that...
1zz2_15-43 1ouy_0-13-35 1.12619901947
becomes...
1ouy_0-13-35 1zz2_15-43 1.12619901947

What do you suggest?
# 2  
Old 03-11-2008
I guess you could loop through the file,
then make a nested loop through each field, writing the fields to a file, sort the file, then another nested loop to read the file back into a line.
# 3  
Old 03-11-2008
Python Solution

Here's something I whipped up in Python. I hope the forum doesn't kill the formatting too badly. It doesn't check to validate whether the input file actually exists, nor does it check to make sure you're passing at least three fields. If you pass it fewer than two, it will blow up. If you pass it exactly two, the newline will break it into two lines. But it works as-is with your test file.


My usage:

cat temp.txt | ./digby01.py

or

./digby01.py -i temp.txt

Shawn


Files (forum killed formatting):

Index of /shawn/unix_forums/digby
# 4  
Old 03-11-2008
Try this ugly hack. I did this in bash, YMMV:

Code:
cat file.txt | while read line
do
  echo $line | \
  awk '{print $1" "$2}' | \
  tr " " "\n" | \
  sort -d | \
  tr "\n" " "
  echo -ne "`echo $line | awk '{print $3}'`\n"
done

# 5  
Old 03-11-2008
Code:
awk '{if($2 < $1) print $2,$1,$3;else print}' file
1ouy_0-13-35 1zz2_15-43 1.12619901947

# 6  
Old 03-12-2008
Thanks for the help guys - the python worked a treat Shawn. Smilie
# 7  
Old 03-12-2008
Awesome! I'm glad it helped. For easy reference for others, here is the code. When I posted it originally, I didn't know about the code (#) option in these forums.

It's a lot longer than it has to be because it allows a file name to be passed in as a parameter, and validates that it has a file or piped-in input. Still, it's a fairly short script. I've also simplified it a little bit from the yesterday's version.

Only the last five non-comment lines are really important. The rest is just validating the input file.

ShawnMilo

Code:
#!/usr/bin/env python

""" 

Sort the first two space-separated fields alphanumerically.

"""



import sys
import os
import getopt

#Get input file (optional)
opts, args = getopt.getopt(sys.argv[1:], 'i:')

input_file = ""

for value in args:

    #input file
    if os.path.isfile(value):
        input_file =  value
        break

#did the user pass an input file?
if input_file == "":

    #if not, did the user pipe in a file?
    if sys.stdin.isatty():
        sys.stderr.write("An input file name is required, or a file must be piped in.\n")
        sys.stderr.write("Exiting program.\n")
        sys.exit(1)

    else: 
        #use piped file
        input = sys.stdin

else:
    #use file name passed by user
    input = open(input_file, 'r')




for line in input:

    #split fields into an array
    fields = line.split(' ');

    #reverse first two fields if not in alphabetical order
    if fields[1] < fields[0]:
        fields[1], fields[0] = fields[0], fields[1]

    output = " ".join(fields)
    print output,

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sorting on fields for last date

Hi all, I have a file with a list of rpm's that have different dates. I am trying to just grab the latest rpm and install date, and discard the rest. The file has 1000's of entries all with different names and dates. I have tried sort -k on the file and I am not grabbing the info, ... (4 Replies)
Discussion started by: gartie
4 Replies

2. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

3. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

4. Shell Programming and Scripting

[Solved] Sorting by several fields

Hello, I have a file with information separated by ";" like this: ABC;20110126000008;00-10-95-29-17-C6;2;37190292 ABC;20110126000008;00-10-95-29-17-C6;1;53140866 ABC;20110126000008;00-10-05-01-11-38;2;11182251 ABC;20110126000008;00-10-05-01-11-38;1;25952816... (3 Replies)
Discussion started by: rubber08
3 Replies

5. Shell Programming and Scripting

AWK multiple line fields sorting

I have a bash script which takes a log file with each record separated by a #. The records have multiple fields but field $1 is always the date and time. When the script is run it prints the record just fine from oldest to newest. I need to have records print out from newest first. Here is the... (7 Replies)
Discussion started by: numele
7 Replies

6. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

7. Shell Programming and Scripting

sorting(both Ascending & Descending) files based on multiple fields

Hi All, I am encountered with a problem while sorting a file based on multiple columns . I need to sort like: (field2,ascending) , (field3,ascending) ,(field8,descending) , (field7,ascending),(field13,ascending). So far i was sorting only in ascending order but here i need to use one... (1 Reply)
Discussion started by: apjneeraj
1 Replies

8. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

9. UNIX for Dummies Questions & Answers

Sorting 2 positional fields

Hi Friends, I've a large datafile, I've to sort the entire records of this file based on the positions. For ex: ccc112IVEAGH VETERINARY SERVICES cca110SHOOTER PROPERTY SERVICES bbb111JUNIOR GOLF ACADEMY LIMITED aaa110AULD PROPERTIES T/A R&J AULD ccb111LISBURN FUELS aac112P & MRS C... (1 Reply)
Discussion started by: ganapati
1 Replies

10. UNIX for Dummies Questions & Answers

Sorting Compressed Fields

Are any of you guys aware of any problems when trying to sort compressed fields? Why I uncompress the file I am trying to sort, I have no problem sorting but when I try to sort compressed fields it doesnt work properly. I need to be able to sort these compressed fields. Any explanation why? (1 Reply)
Discussion started by: ndoggy020
1 Replies
Login or Register to Ask a Question