build a string of asterisks elegantly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting build a string of asterisks elegantly
# 8  
Old 02-16-2012
Quote:
Originally Posted by fpmurphy
Code:
awk 'BEGIN { while (a++<=50) s=s "x"; print s }'

Just a little fix about it , the above statement would print it 51 times instead of 50 times

Code:
$ awk 'BEGIN { while (a++<=50) s=s "x"; print s }' | fold -w 10
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
x


... so instead, you may want to use:

Code:
awk 'BEGIN { while (a++<50) s=s "x"; print s }'

or
Code:
awk 'BEGIN { while (++a<=50) s=s "x"; print s }'

# 9  
Old 02-16-2012
how can i replace the 50 by a variable?
lazy
# 10  
Old 02-16-2012
Code:
$ count=10
$ awk -v c="$count" 'BEGIN { while (++a<=c) s=s "x"; print s }'
xxxxxxxxxx

Code:
$ count=20
$ awk -v c="$count" 'BEGIN { while (++a<=c) s=s "x"; print s }'
xxxxxxxxxxxxxxxxxxxx

# 11  
Old 02-16-2012
hi

thanks all

i will use the following solutions

Code:
st=$(printf '%*s' "$l" ' ' | tr ' ' "*")

st=$(awk -v c="$l" 'BEGIN { while (++a<=c) s=s "x"; print s }')

thanks
lazy

Last edited by methyl; 02-16-2012 at 05:47 PM.. Reason: please use code tags
# 12  
Old 02-16-2012
Code:
echo | awk NF=51 OFS=\*

Code:
c=50
echo | awk NF=$c+1 OFS=\*

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 13  
Old 02-16-2012
Quote:
Originally Posted by Scrutinizer
Code:
echo | awk NF=51 OFS=\*

Code:
c=50
echo | awk NF=$c+1 OFS=\*

Intriguing. How does that work? I wasn't aware the number of fields was something you could set.
# 14  
Old 02-16-2012
Quote:
assigning to a nonexistent field (for example, $(NF+2)=5) shall increase the value of NF; create any intervening fields with the uninitialized value; and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS. Each field variable shall have a string value or an uninitialized value when created
awk: Variables and Special Variables
It is not strictly assigning to a field but it is increasing NF

You can also use NF to cut of fields:
Code:
echo "this line will never end" | awk NF=4

---------- Post updated at 23:13 ---------- Previous update was at 22:35 ----------

This works too
Code:
echo | awk '$50=OFS="*"'

49 field separators, followed by a 50th field that contains an *

Last edited by Scrutinizer; 02-16-2012 at 05:53 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing multiple asterisks in vi

i need to replace all occurrences of "period asterisk" as it is shown in this: blah blah .*:.*:.* blah blah with: :: so that the end result looks like this: blah blah :: blah blah I tried different variations of the following but it didint work: %s_ .*:.*:.* _ :: _g (2 Replies)
Discussion started by: SkySmart
2 Replies

2. UNIX for Advanced & Expert Users

Show Asterisks when changing Password

Note: **Showing Asterisks when using SUDO is not what I am looking for. That method is well documented** Short Description: We have a requirement where users want to see that they are typing a password when logging into a RedHat box or when they are changing their password -- instead of... (1 Reply)
Discussion started by: caperjm
1 Replies

3. Shell Programming and Scripting

Eliminate or ignore asterisks in data when parsing

I have data file that has this in it: data.txt ......... ......... PPJ97**2017PPJ97**2017-03-21-13.35.15.887208********************START ERROR LOGGING****************** PPJ97**2017-03-21-13.35.15.887208** PROMPT APPLICATION ERROR ** PPJ97**2017-03-21-13.35.15.887208** IN TIMESTAMP |... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. UNIX for Dummies Questions & Answers

Adding SDK Build on Kernel Source Build

Hi, So I downloaded this kernel source and was able to build it successfully. But I want to add this SDK source code inside, can anyone help me how to do this? Note that the SDK source can be built by itself. I added the SDK in the main Makefile: init-y := init/ #added SDK... (0 Replies)
Discussion started by: h0ujun
0 Replies

5. Shell Programming and Scripting

Create new file when three asterisks are encountered.

Hi All, I have a text file which is currently formatted like this: TEXT1 *** TEXT2 *** TEXT3 *** I want text before *** to go into separate files. For example, 1.dat TEXT1 (5 Replies)
Discussion started by: shoaibjameel123
5 Replies

6. Shell Programming and Scripting

need to replace asterisks

I need to replace occurrences of twelve asterisks "************" with the string " 0000000.00" . Note that there are two spaces in front of the first zero. How can I do this using awk or sed? (3 Replies)
Discussion started by: mustang_9333
3 Replies

7. Shell Programming and Scripting

how to build a pipe delimited string

#! /bin/csh set delimiter = | foreach i (*) set str_deli="$i$delimiter" question: how to retain the value of str_deli so i can build a pipe delimited string? end (1 Reply)
Discussion started by: jdsignature88
1 Replies

8. Shell Programming and Scripting

Assistence With Using Asterisks in GREP Expressions

I am attempting to find all complete words which contain an asterisk at the beginning and the end - for instance, "*Hello?*" or "*you*". From what I've read, I would have thought that the following expression would do that just fine: \<\*.*\*\> \< denoting the beginning of a word. \*... (12 Replies)
Discussion started by: MagusScythe
12 Replies

9. Shell Programming and Scripting

How to build a string in shell script

Hi all, I had a typical problem. I am using a parameter PK="PK1 PK2 PK3" i need to build the string a.PK1=b.PK1 and a.PK2=b.PK2 and a.PK3=b.PK3 Please help (8 Replies)
Discussion started by: nkosaraju
8 Replies

10. Shell Programming and Scripting

Double asterisks

When I go$ echo *I get a directory listing. When I go$ echo * *I get a directory listing, followed by a second identical directory listing. When I go$ echo **I only get one directory listing. What happens to the second asterisk in this case? Why doesn't it expand? I haven't been able to sleep... (2 Replies)
Discussion started by: na5m
2 Replies
Login or Register to Ask a Question