Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

rahulbbbb

IS-IT--Management
May 13, 2020
1
IN
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
 
You can use associative array, where every element is list of lines which contain specific receiverN value, e.g.:

rahulbbbb.tcl
Code:
[COLOR=#804040][b]set[/b][/color] [COLOR=#804040][b]filename[/b][/color] [COLOR=#ff00ff]"rahulbbbb.txt"[/color]
[COLOR=#804040][b]set[/b][/color] input_file [[COLOR=#804040][b]open[/b][/color] [COLOR=#008080]$filename[/color] [COLOR=#ff00ff]"r"[/color]]

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

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"* sorted lines of [/color][COLOR=#008080]$filename[/color][COLOR=#ff00ff]"[/color]
[COLOR=#804040][b]foreach[/b][/color] key [[COLOR=#804040][b]lsort[/b][/color] [[COLOR=#804040][b]array[/b][/color] names my_array]] {
  [COLOR=#804040][b]foreach[/b][/color] line [COLOR=#008080]$my_array[/color]([COLOR=#008080]$key[/color]) {
    [COLOR=#804040][b]puts[/b][/color] [COLOR=#008080]$line[/color]
  }
}

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top