Setting a SSS string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Setting a SSS string
# 1  
Old 03-11-2010
Setting a SSS string

Hi all,
I would like to build a string like

S_string=SSS...S

so repeating the S character n times where n is the value of a variable.
Is there a way to do it in one shot?
Cheers,
Sarah
# 2  
Old 03-11-2010
Yes,
which scripting/programming language are you using?

Last edited by radoulov; 03-11-2010 at 10:28 AM..
# 3  
Old 03-11-2010
I'm using bash
# 4  
Old 03-11-2010
Code:
S_string=$(printf '%013d'|tr 0 S)
S_string=$(awk 'BEGIN{$13=OFS="S";print}')

# 5  
Old 03-11-2010
Or:

bash >= 3.1:

Code:
$ c=S n=10
$ eval printf -vs '$c%.0s' {1..$n}
$ echo $s
SSSSSSSSSS

A shell function with Perl:

Code:
gen_n_of_x() {
  perl -le'
    print $ARGV[0] x $ARGV[1]
    '  "$2" "$1"
    }

Code:
$ gen_n_of_x 10 S
SSSSSSSSSS

Special characters should be quoted:

Code:
$ gen_n_of_x 7 \#
#######

Chris F.A. Johnson wrote a pure shell function for this: _repeat.

Last edited by radoulov; 03-12-2010 at 03:43 AM..
# 6  
Old 03-12-2010
Quote:
Originally Posted by vgersh99
Code:
S_string=$(printf '%013d'|tr 0 S)
S_string=$(awk 'BEGIN{$13=OFS="S";print}')

Thank you all,
If I have n=5, how do I have to change the commands?
I tried
Code:
n=5
S_string=$(printf '%0${n}d'|tr 0 S)

but it gives me an error message
# 7  
Old 03-12-2010
Quote:
Originally Posted by f_o_555
Thank you all,
If I have n=5, how do I have to change the commands?
I tried
Code:
n=5
S_string=$(printf '%0${n}d'|tr 0 S)

but it gives me an error message
Use double, instead of single quotes:

Code:
S_string="$(printf "%0${n}d"|tr 0 S)"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Solaris

Is there a difference between setting a user as nologin and setting it as a role?

Trying to figure out the best method of security for oracle user accounts. In Solaris 10 they are set as regular users but have nologin set forcing the dev's to login as themselves and then su to the oracle users. In Solaris11 we have the option of making it a role because RBAC is enabled but... (1 Reply)
Discussion started by: os2mac
1 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

4. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

5. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

8. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

9. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

10. Shell Programming and Scripting

Help with setting variables extracted from a string

Hi guys, I'm using tcsh. I have a string that contains variable names: "var1:var2:var 3", I want to be able to do different operations on the content of those variables: I extract the variable names with: foreach var ( `echo $string | sed 's/:/\n/g'`) now in the variable `var` I have the... (1 Reply)
Discussion started by: gofmarat
1 Replies
Login or Register to Ask a Question