This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

sort a file, spaces to new line

Old posts that have not been replied to for several years.
Locked
B
Burke
Voice
Posts: 21
Joined: Mon Feb 07, 2005 9:33 pm

sort a file, spaces to new line

Post by Burke »

hi

i have a file with names like:

Code: Select all

name1 name2  name3     name4
name 5 name6  name6
  name8 name9 name10  name11 name 11
i want to remove the spaces. for every space, a new line.

how to set a var to my paste here i know. (open file etc.)
or how i can close file and save.

but i have no idea which commands will help me to make the " " or " " to a new line.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

Code: Select all

string map {" " "\n"} "name1 name2"
or, better yet

Code: Select all

regsub -all " {1,}" [string trim "  name1 name2   name3 "] "\n"
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

trim will only remove leading and trailing spaces, best will probably be if you first just [split $list] and then you will get a few empty list entrys which can be delted within a for loop and lreplace and all left is a clean, safe list of the names.

to show what split does:

Code: Select all

[14:15:03] tcl: evaluate (.tcl): split "name1   name2\nname3"
Tcl: name1 {} {} name2 name3
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

De Kus wrote:trim will only remove leading and trailing spaces, best will probably be if you first just [split $list] and then you will get a few empty list entrys which can be delted within a for loop and lreplace and all left is a clean, safe list of the names.

to show what split does:

Code: Select all

[14:15:03] tcl: evaluate (.tcl): split "name1   name2\nname3"
Tcl: name1 {} {} name2 name3
you didn't bother to test that regsub, did you?

it takes care of arbitrary number of spaces in-between, and [string trim] takes care of leading and trailing spaces - just what that guy needs, nothing more, nothing less
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

sorry, didnt pay too much attention to the " {1,}" expression, since it never worked that way I liked :D. my regsubs with RE never replaced anything :(. but this one works, sorry for bother.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Locked