how to generate var len strings of a given char


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to generate var len strings of a given char
# 1  
Old 10-29-2008
how to generate var len strings of a given char

I am looking for a clever way to generate a string of a given chararcter repeated N times in a shell,
for example 12 of '-' would produce '------------'

I saw a good example in CFAJ book, a function called repeat that would run a cycle and concatenate a char to the string N times. It would defenitely work, but I am just in hope of some kind of a clever one-liner type of trick. Anybody have an idea?
# 2  
Old 10-29-2008
With shells that support brace expansion:

Code:
printf '-%.0s' {1..12}

With some awk implementations (GNU awk, nawk):

Code:
awk 'BEGIN{$12=OFS="-";print}'

With Perl:

Code:
perl -le'print"-"x12'

zsh:

Code:
print ${(l.44..\055..\055.)}

Ruby:

Code:
ruby -e 'puts"-"*12'

There is another thread here.
# 3  
Old 10-29-2008
Nice!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Perl regex problem on strings with several occurences of one char

Hi all, i have the following line in a record file : retenu=non demande=non script=#vtbackup /path=/save/backup/demande position=140+70 and i want to use Perl regex to have the following output key : "retenu" value : "non" key : "demande" value "non" key : "script" value :... (2 Replies)
Discussion started by: Fundix
2 Replies

2. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

3. Shell Programming and Scripting

isql "len" not working

Hi guys, I'm running into an issue where a query works on rapid sql 7x but not on isql. select substring(1,len(column-5)) from table_a where column = "xyz 1111" --The output on raipd sql is xyz --The same query on isql returns xyz 1111 any help is appreciated Cheers Irishboy (2 Replies)
Discussion started by: Irishboy24
2 Replies

4. Red Hat

monitoring search strings /var/log/messages

Hello Everyone, I am setting up monitoring system for linux server, I need to add few some common error message in monitoring file which can search those strings in /var/log/messages file on the client. can someone suggest few error messages. Thanks, (2 Replies)
Discussion started by: bobby320
2 Replies

5. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

6. Solaris

Difference between /var/log/syslog and /var/adm/messages

Hi, Is the contents in /var/log/syslog and /var/adm/messages are same?? Regards (3 Replies)
Discussion started by: vks47
3 Replies

7. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

8. UNIX for Dummies Questions & Answers

Extracting substring using ${var:start:len}

Hi All, I am trying to extract substring from a variable by using the pattern as var3=${var2:3:3} var2 is a variable already declared and having the value. From var2 i need to extract 3 characters from 3rd position. When ever i give the expression var3=${var2:3:3} on shell prompt, the... (3 Replies)
Discussion started by: Raamc
3 Replies

9. Shell Programming and Scripting

How to generate random strings from regx?

Hi, Guz! I'm working on a scripts compiler which needs a function to generate random strings. I think REGX may be a good solution to restrict the string format. Before DIYing I'd like asking for any existing libs or codes. Any help will be appreciated! (7 Replies)
Discussion started by: wqqafnd
7 Replies

10. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies
Login or Register to Ask a Question