Creating a blank string of a specified size


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating a blank string of a specified size
# 1  
Old 01-19-2012
Creating a blank string of a specified size

I want to have a string which has n blank spaces

For example
Code:
set N = 3
create str = "   "

So the length depends on the value of N. I am in tcsh.
# 2  
Old 01-19-2012
Code:
perl -e 'print " "x'$N

# 3  
Old 01-19-2012
I have tried this for getting 20 A's and am getting a problem

Code:
echo 20 | awk 'BEGIN{while (a++<$1) s=s"A"; print s}'

# 4  
Old 01-19-2012
What problem are you getting?
# 5  
Old 01-19-2012
I sorted out the problem of doing the mistake of using $1, but now the string is still a single blank

Code:
set nBlk = "5"
set blankStr = `awk -v n=$nBlk 'BEGIN{while (a++<n) s=s" "; print "."s"."}'`
echo "$blankStr"

---------- Post updated at 11:51 AM ---------- Previous update was at 11:40 AM ----------

Quote:
Originally Posted by Corona688
What problem are you getting?
I provide this example to help.

The table is not shifted when using " " instead of "B".

Code:
#!/bin/tcsh

set msg = "-v,--verbose=level -nv,--no-verbose -q,--quiet -u,--usage -e,--examples -h,--help"

echo ""

set nBlk = "5"
set blankStr = `awk -v n=$nBlk 'BEGIN{while (a++<n) s=s"B"; print s}'`
echo "blankStr = .$blankStr."

echo ""

set len = ${%msg}
if ($len > 0) then
  printf "%-s\t" $msg | fold -s -w 100 | awk '{sub(/^[ \t]+/, "")};1'  \
    | awk -v s=$blankStr '{gsub(/,/,", ");print s $0}'
endif

echo ""

set len = ${%msg}
if ($len > 0) then
  printf "%-s\t" $msg | fold -s -w 100 | awk '{sub(/^[ \t]+/, "")};1'  \
    | awk -v s=$blankStr '{gsub(/,/,", ");print s $0}' \
    | awk '{gsub(/B/," ");print}'
endif

echo ""
echo "nBlk = $nBlk"
set blankStr = `awk -v n=$nBlk 'BEGIN{while (a++<n) s=s"B"; print s}'`
echo "blankStr = .$blankStr."
set blankStr = `awk -v n=$nBlk 'BEGIN{while (a++<n) s=s" "; print s}'`
echo "blankStr = .$blankStr."

set len = ${%msg}
if ($len > 0) then
  printf "%-s\t" $msg | fold -s -w 100 | awk '{sub(/^[ \t]+/, "")};1'  \
    | awk -v s=$blankStr '{gsub(/,/,", ");print s $0}'
endif

echo ""


Last edited by kristinu; 01-19-2012 at 01:12 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bc in shell script is creating blank files

Hi, I am creating a shell script which will check the processing of the main script if the error count is more than 2% of the record count in a feed. Part of the code is below: err_tol=`echo $feed_cnt \* 0.02 |bc` while read line do err_cnt=$(grep -i "$line" $err_log | wc -l) ... (5 Replies)
Discussion started by: member2014
5 Replies

2. Shell Programming and Scripting

Insert a string instead of blank lines

how can i insert a string sush as "###" instead of blank lines in a file? i try this code but it doesn't work! awk 'NF<1 {$1=="###" ; print$0}' in_file > out_file (13 Replies)
Discussion started by: oreka18
13 Replies

3. Shell Programming and Scripting

test for a blank string

Hi guys I am performing a simple test for a blank string with the following code: if ] ]] ; then echo "Blanks are NOT a valid input " return 1 fi The above fails giving a syntax error message: syntax error at line 142 : `=~' unexpected I am in ksh88 is there... (2 Replies)
Discussion started by: aoussenko
2 Replies

4. UNIX for Dummies Questions & Answers

Creating directory with specific size?

Hello world, I just learnt we can create a directory with custom size in a Linux server (say Redhat). Is it true? I'm asking because the only data (I can think of) a directory's inode holds is the files and 'sub-dir's. How can a new empty directory be of some required size? :wall: PS : In... (2 Replies)
Discussion started by: satish51392111
2 Replies

5. Shell Programming and Scripting

creating/using libraries Perl...blank outout?

Good morning!! Im trying to create a script that should get a list of numbers from the user (using STDIN or a list of arguments), and call my library function. #!use/bin/perl require 'my-lib.pl'; @userArray = <STDIN>; while() { chomp; last if ! /\d/; push(@userArray,&_); }... (2 Replies)
Discussion started by: bigben1220
2 Replies

6. Shell Programming and Scripting

Creating large number of files of specific size

Hi I am new to shell scripting.I want to create a batch file which creates a desired number of files with a specific size say 1MB each to consume space.How can i go about it using for loop /any other loop condition using shell script? Thanks (3 Replies)
Discussion started by: swatideswal
3 Replies

7. Filesystems, Disks and Memory

Creating filesystem of 2MB size

Hi all, I would like to ask on how to create 2MB partition on a Compact Flash card. It supposed to be of FAT12 type, and the CF capacity is 4GB. I try to do the partitioning and specify the size as 2MB but the partition editor automatically resize it to 8MB. I know that this is possible and the... (4 Replies)
Discussion started by: archayl
4 Replies

8. UNIX for Dummies Questions & Answers

string with blank spaces

I have a file that has dates like this: date FINAL_RESULT; 7 date FINAL_RESULT; 2 date FINAL_RESULT; 5 With this command: seira=`cut -f2 -d\; tes.txt` i take the date FINAL RESULTs and i store them on variable seira.then seira look like this: 6 3 8 I want to read seira and make a sum of all... (4 Replies)
Discussion started by: psalas
4 Replies

9. Solaris

How to correctly allocate size while creating FileSystem

Hello - I am finding difficulty in creating and allocating correct size to File Systems on solarix x86 box. Please see below contents I followed on screen and in the end It shows that /app file system is created of size 135GB , I wanted it to be 30gb as mentioned during 'format' command in 'Enter... (7 Replies)
Discussion started by: panchpan
7 Replies

10. UNIX for Advanced & Expert Users

Creating data of size

Hello, I need to create 100kb,1000kb,10000kb etc files of no particular substance but I need them a exact size. I think they are .dat's I need but im unsure of the command I issue to create them Any help appreciated :) Mr Pink (4 Replies)
Discussion started by: Mr Pink
4 Replies
Login or Register to Ask a Question