You are not logged in.

#1 2012-12-20 17:34:32

whoops
Member
Registered: 2009-03-19
Posts: 891

[solved]zsh: "magic" fill up or truncate string to a certain length?

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

#2 2012-12-20 18:39:56

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: [solved]zsh: "magic" fill up or truncate string to a certain length?

Sorry, I dont know how to do it with an zsh specific option.
But

whoops wrote:

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

#3 2012-12-20 18:51:02

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved]zsh: "magic" fill up or truncate string to a certain length?

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

#4 2012-12-20 19:07:35

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: [solved]zsh: "magic" fill up or truncate string to a certain length?

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

#5 2012-12-20 19:18:52

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: [solved]zsh: "magic" fill up or truncate string to a certain length?

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

#6 2012-12-20 19:29:19

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: [solved]zsh: "magic" fill up or truncate string to a certain length?

@progandy: Wow nice! big_smile


"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

#7 2012-12-20 19:47:59

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved]zsh: "magic" fill up or truncate string to a certain length?

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

Board footer

Powered by FluxBB