×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

I want to write simple code to sort me huge file and place in another file in tcl

I want to write simple code to sort me huge file and place in another file in tcl

I want to write simple code to sort me huge file and place in another file in tcl

(OP)
File has below elements

Name1 Name2 Name3 Name4 Name5

rahul receiver1 30 20 50
ramesh receiver2 30 20 50
rajesh receiver3 30 20 50
Vidya receiver1 30 20 50
Reshma receiver2 30 20 50
rahul_1 receiver3 30 20 50
rahul_2 receiver1 30 20 50
rahul_6 receiver2 30 20 50
rahul_7 receiver3 30 20 50
rahul_8 receiver4 30 20 50


Output I want is below ... I want name2 filed same elements line together ..like who is holding reciever1 should come first


Name1 Name2 Name3 Name4 Name5

rahul receiver1 30 20 50
Vidya receiver1 30 20 50
rahul_2 receiver1 30 20 50
ramesh receiver2 30 20 50
Reshma receiver2 30 20 50
rahul_6 receiver2 30 20 50
rajesh receiver3 30 20 50
rahul_1 receiver3 30 20 50
rahul_7 receiver3 30 20 50
rahul_8 receiver4 30 20 50

RE: I want to write simple code to sort me huge file and place in another file in tcl

You can use associative array, where every element is list of lines which contain specific receiverN value, e.g.:

rahulbbbb.tcl

CODE

set filename "rahulbbbb.txt"
set input_file [open $filename "r"]

puts "* reading $filename line by line"
puts "  and storing lines into associative array"
while { [gets $input_file line] != -1 } {
  puts $line
  set key [lindex $line 1]
  if {[info exists my_array($key)]} {
    puts "\t\t..appending line to list my_array($key)"
    lappend my_array($key) $line    
  } else {
    puts "\t\t..creating list my_array($key) and adding line to the list"
    set my_array($key) [list $line]
  }
}
close $input_file
puts "\n"

puts "* sorted lines of $filename"
foreach key [lsort [array names my_array]] {
  foreach line $my_array($key) {
    puts $line
  }
} 

Output:

CODE

$ tclsh rahulbbbb.tcl
* reading rahulbbbb.txt line by line
  and storing lines into associative array
rahul receiver1 30 20 50
                ..creating list my_array(receiver1) and adding line to the list
ramesh receiver2 30 20 50
                ..creating list my_array(receiver2) and adding line to the list
rajesh receiver3 30 20 50
                ..creating list my_array(receiver3) and adding line to the list
Vidya receiver1 30 20 50
                ..appending line to list my_array(receiver1)
Reshma receiver2 30 20 50
                ..appending line to list my_array(receiver2)
rahul_1 receiver3 30 20 50
                ..appending line to list my_array(receiver3)
rahul_2 receiver1 30 20 50
                ..appending line to list my_array(receiver1)
rahul_6 receiver2 30 20 50
                ..appending line to list my_array(receiver2)
rahul_7 receiver3 30 20 50
                ..appending line to list my_array(receiver3)
rahul_8 receiver4 30 20 50
                ..creating list my_array(receiver4) and adding line to the list


* sorted lines of rahulbbbb.txt
rahul receiver1 30 20 50
Vidya receiver1 30 20 50
rahul_2 receiver1 30 20 50
ramesh receiver2 30 20 50
Reshma receiver2 30 20 50
rahul_6 receiver2 30 20 50
rajesh receiver3 30 20 50
rahul_1 receiver3 30 20 50
rahul_7 receiver3 30 20 50
rahul_8 receiver4 30 20 50 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close