shell script to print '*' triangle


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to print '*' triangle
# 8  
Old 01-09-2008
Code:
# awk 'NR<6{a=sprintf("%0"NR"s","*");gsub("0","*",a); print a}' /etc/services
*
**
***
****
*****

# 9  
Old 01-09-2008
Quote:
Originally Posted by Raamc
Hi Klashxx,

What OFS in Print statement does?
I am new to AWK so i didnt have no idea what OFS means?

Thanks,
Raamc
Code:
>awk 'BEGIN{    
               while ( i < 5 )
               {
               i++
               $i=OFS="*"
               print
               $i=""
               }
               }'

Hi , you don“t need a input file , this awk has only a begin part (execuded once before any input is read).

Then we can change dinamically the number of fields ($i), and assign value to it.

Ex:

Code:
> awk 'BEGIN{$5="x";print}'
    x

As you see , with this litle trick we order awk to print the 5th field with value "x".
OFS is the Output Field Splitting ,by default its value is a blank space, we didn't change it so the output of the previous statement is four blanks (because in five fields we have 4 field separators).

Next step:

Code:
> awk 'BEGIN{$5=OFS="x";print}'
xxxxx

Here we changed the OFS to "x" so the awk prints four OFS "xxxx" and the 5th field "x".

Finally we have to set the $i variable to null cause ,if we don't do that , the variable $i will have value "*" at i postion , like in this example:

Code:
> awk 'BEGIN{ while ( i < 5 )
               {
               i++
               OFS="*"
               $i="o"
               print
               }
             }'
o
o*o
o*o*o
o*o*o*o
o*o*o*o*o


Regards

Last edited by Klashxx; 01-09-2008 at 12:47 PM..
# 10  
Old 01-09-2008
Code:
$ echo "*
> **
> ***
> ****
> *****"
*
**
***
****
*****
$

Smilie
# 11  
Old 01-09-2008
sometimes is funny how we make things complicated Smilie
# 12  
Old 01-09-2008
what if number of * doesn't stop at 5? Smilie
# 13  
Old 01-10-2008
Quote:
Originally Posted by ghostdog74
what if number of * doesn't stop at 5? Smilie
But, it does. I counted. What if we were supposed to display pentagon?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print statement in shell script

Hi All, when I executed the query in normal unix mode, the output is expected. if I executed the query using shell script, I am not getting the result. not sure whether I need to use dbms_output.put_line or echo. dbms_output.put_line("COUNTRY_CODE"||" "||"SUB_TYPE"||" ... (3 Replies)
Discussion started by: arun888
3 Replies

2. Shell Programming and Scripting

Print 1 to 10 with space in shell script

Hi, I want to print 1 to 10 or upto any number sequentially with space in a single line. Like, 1 2 3 4 5 6 7 ...... In shell script only.. Can anyone plz help me. Thanks: (14 Replies)
Discussion started by: arup1980
14 Replies

3. Fedora

Shell Script - awk, begin, for and print

pointsb=`awk -v a2="$a2" -v b2="$b2" -v c2="$c2" -v yb="$yb" -v yc="$yc" \ 'BEGIN { for (y=yc; y<=yb; y++) { x = a2*y*y+b2*y+c2; print x, y }; }'` I am learning shell script. I was reading a script and got confused in this line. I understood that awk is allowing to assign the variable. But... (10 Replies)
Discussion started by: agriz
10 Replies

4. Shell Programming and Scripting

Help me with shell script command to print

I have a file with similar line like below /u13/orabkup/u13/orabkup/abcd/arc82.123 Now I need to have only u13/orabkup/abcd/arc82.123 rather than /u13/orabkup/u13/orabkup/abcd/arc82.123 Kindly help me to print /u13/orabkup/ only once and the file name abcd/arc82.123.. Thanks & Regards... (1 Reply)
Discussion started by: anitha11889
1 Replies

5. Ubuntu

How to creat a half-triangle in bash(Ubuntu)?

how to create a half-triangle like this and that will accept an input from the user, Dimension of the triangle will be based on the users input. example: enter the dimension: 5 ***** -**** --*** ---** ----* thankyou (3 Replies)
Discussion started by: carljoses
3 Replies

6. Shell Programming and Scripting

How to print backslash in shell script using awk?

I found that echo "aaa" | awk '{print ",\\";}' works, and it will give "\". but ddd=`echo "aaa" | awk '{print ",\\";}'`; echo $ddd will not work. Could anyone tell me why? thank you. (8 Replies)
Discussion started by: wxuyec
8 Replies

7. Shell Programming and Scripting

need help in writing shell script to generate pascal's triangle

Hi :) I am learning shell scripting, I need help, I would like to have a script that produces pascal's triangle as shown in the picture http://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Pascal%27s_triangle_5.svg/250px-Pascal%27s_triangle_5.svg.png plz, help it's urgent thanks in... (1 Reply)
Discussion started by: angel1
1 Replies

8. Shell Programming and Scripting

shell script to print words having first and last character same.

Hi I want to write a shell script to print only those words from a file whose beginning and last character are same. Please help. Thanks, vini (5 Replies)
Discussion started by: vini kumar
5 Replies

9. UNIX for Dummies Questions & Answers

Linux Shell Question: how to print the shell script name ?

Suppose I have a script named "sc.sh" in the script how to print out its name "sc.sh"? (3 Replies)
Discussion started by: meili100
3 Replies
Login or Register to Ask a Question