hi
in a script i hate string definitions like
str="***********************************"
who can help to build a 50 character long string of asterisks more elegantly?
any hint is welcome
thanks and regards
lazy
hi
in a script i hate string definitions like
str="***********************************"
who can help to build a 50 character long string of asterisks more elegantly?
any hint is welcome
thanks and regards
lazy
What's your system? What's your shell?
str=`perl -e 'print "*"x50'`
ruby -e 'puts "*"*50'
awk 'BEGIN { while (a++<=50) s=s "x"; print s }'
printf '%*s' "50" ' ' | tr ' ' "*"
python -c 'print "*"*50'
echo "*****" | sed 's/.*/&&&&&&&&&&/'
found a solution, thanks all
lazy
hi
in the awk solution: how can i replace 50 with a variable?
regards
lazy
What about your solution?
We appreciate people saying their issue is solved, we like even more when someone saying found a solution, show what they found...
Just a little fix about it , the above statement would print it 51 times instead of 50 times
$ 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:
awk 'BEGIN { while (a++<50) s=s "x"; print s }'
or
awk 'BEGIN { while (++a<=50) s=s "x"; print s }'
how can i replace the 50 by a variable?
lazy
$ count=10
$ awk -v c="$count" 'BEGIN { while (++a<=c) s=s "x"; print s }'
xxxxxxxxxx
$ count=20
$ awk -v c="$count" 'BEGIN { while (++a<=c) s=s "x"; print s }'
xxxxxxxxxxxxxxxxxxxx
hi
thanks all
i will use the following solutions
st=$(printf '%*s' "$l" ' ' | tr ' ' "*")
st=$(awk -v c="$l" 'BEGIN { while (++a<=c) s=s "x"; print s }')
thanks
lazy
echo | awk NF=51 OFS=\*
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.
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:
echo "this line will never end" | awk NF=4
---------- Post updated at 23:13 ---------- Previous update was at 22:35 ----------
This works too
echo | awk '$50=OFS="*"'
49 field separators, followed by a 50th field that contains an *
$
$
$ LINE=`yes "*" | head -50 | tr -d '\n'`
$
$ echo "$LINE"
**************************************************
$
$
tyler_durden
Note that this solution does not work on all versions of awk. It works for gawk. oawk prints an empty string.
That's a very slick solution. Great job.
Regards,
Alister
---------- Post updated at 10:02 AM ---------- Previous update was at 09:53 AM ----------
It should work with any AWK that intends to be POSIX compliant. I'm not familiar with oawk, but if it endeavours to follow POSIX, it's a bug. Testing confirms that gawk, mawk, and nawk are compliant.
Looking back, this behavior was documented at least 15 years ago in SUSv2, if not earlier. So, it's nothing new and not a case of an active implementation being in the process of catching up: awk
Regards,
Alister
Hi.
Here is a longer, more general solution, changeable from the command line:
#!/usr/bin/env bash
# @(#) s1 Demonstrate creation of simple data files.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C ./spit
pl " Results, default:"
./spit
pl " Results, 2 x 3, value 1.5:"
./spit -r 2 -c 3 -v 1.5
pl " Results, one row of character data:"
./spit -d "" --rows 1 --cols 20 -v '*'
exit 0
producing:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
bash GNU bash 3.2.39
./spit spit - ( local: RepRev 1.3, ./spit, 2012-02-17 )
-----
Results, default:
1
-----
Results, 2 x 3, value 1.5:
1.5 1.5 1.5
1.5 1.5 1.5
-----
Results, one row of character data:
********************
I have needed data files for testing often enough that this is useful for me. Perhaps it will be useful for others ... cheers, drl
Download spit.txt, rename as "spit", chmod +x spit, run a test script. As usual no warranty, use at your risk.
oawk is the old pre-POSIX original implementation of awk. nawk is new awk from the AT&T Porgrammers Workbench. nawk was reference implementation for the POSIX awk specification. Unix operating systems often ship with both versions.