You are not logged in.
Hi!
I could swear I somewhere saw a VERY short solution to this problem ("fill up or truncate string to a certain length"). Can't find the example any more... Tried to do it with variable parameters / regex and failed miserably.
The example I saw looked like "this typical zsh stuff that I don't fully understand yet" (${${stuff[]}10: :}" to me - in other words: "magic" :3), and any text or variable put in there would be set to the fixed length of 10 characters (either by adding spaces or truncating the string).
No matter how I try it... I need at least a few lines of code to do the same thing.
Any clues?
Thanks!
Last edited by whoops (2012-12-21 08:21:14)
Offline
Sorry, I dont know how to do it with an zsh specific option.
But
Tried to do it with variable parameters / regex and failed miserably.
well, I can suggest a regex:
echo 'abcdefghijklmnopqrstuvwxyz' | perl -pe 's/(.{10}).*/$1/'
will output abcdefghij (10 characters)
EDIT: Just noticed you mentioned "adding spaces". sorry, I didnt saw that part.
Last edited by chris_l (2012-12-20 18:42:32)
"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.
Offline
Hmm... tried in php now too, but that's even longer. I think I'm getting "closer" though. The "zsh magic thing" looked "a lot" like that:
${(stuff:((${(something)var}-10)):20)var}
Still haven't been able to figure out "stuff" though (and other things).
Argh, zsh is driving me nuts.
Last edited by whoops (2012-12-20 18:52:14)
Offline
OK, I think I did it:
echo 'a' | perl -e "printf '%10s', substr($_,0,10)"
that returns " a" (9 spaces and an a) and replacing with the 'abcdefghijklmnopqrstuvwxyz'
returns again abcdefghij
Last edited by chris_l (2012-12-20 19:08:54)
"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.
Offline
You don't need perl. This works, too:
string="some text"
trunc=$(printf "% 20s" "${string[1,20]}")
echo $trunc
Edit: Even better is this. The flag for padding can be found here http://zshwiki.org/home/scripting/paramflags
trunc="${(l:10:: :)${string[1,10]}}"
Last edited by progandy (2012-12-20 19:40:32)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
@progandy: Wow nice!
"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.
Offline
Thanks!
trunc="${(l:10:: :)${string[1,10]}}"
If that still works after I'm done with (pasting + modifying) it, it's exactly what I meant (can't test it ATM)! Couldn't get the parameter flags right if my life depended on it, guess I'll need a lot of practice.
edit: Yes, still works and it's short enough to use multiple instances of it in the output part of scripts without making a mess. Solved!
Last edited by whoops (2012-12-21 08:21:44)
Offline