Visit Our UNIX and Linux User Community


How to print a particular character n number of times in a line??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print a particular character n number of times in a line??
# 1  
Old 09-16-2013
How to print a particular character n number of times in a line??

hi,

Is it possible to print a particular character n number of times in a line?
for example.

i am print the following line using echo command..

Code:
echo "files successfully moved"

i want to count the number of characters that are been displayed. i am doin it using

Code:
echo "files successfully moved"
no=`echo "files successfully moved" | wc -c`

after this i want to put a "-" below just to underline the text printed. like

Code:
files successfully moved
-------------------------

so after counting the number of characters printed, i want to print that many number of "-" below the line. how can i do this? plz help.
# 2  
Old 09-16-2013
Code:
i=1
while [ $i -le $no ]
do
  echo -n "-"
  (( i++ ))
done
echo

or
Code:
echo "files successfully moved" | sed 's/./-/g'


Last edited by balajesuri; 09-16-2013 at 11:43 AM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 09-16-2013
This is way to do it using awk:
Code:
awk -v i=10 'BEGIN { OFS="-"; $i="-"; print }'

i=10 specifies the amount of characters, you can change it to i=$no
This User Gave Thanks to Subbeh For This Post:
# 4  
Old 09-16-2013
try also:
Code:
echo "files successfully moved"| awk '1; {gsub(".","-")}1'

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 09-16-2013
Another way using pure Bash magic:

Code:
out="files successfully moved"
dash="-"
echo -e "${out}\n${out//?/$dash}"
files successfully moved
-----------------------

This User Gave Thanks to in2nix4life For This Post:
# 6  
Old 09-16-2013
Longhand in OSX 10.7.5 default bash terminal:-
Code:
Last login: Mon Sep 16 15:43:08 on ttys000
AMIGA:barrywalker~> text="Files successfully removed..."
AMIGA:barrywalker~> lentext="${#text}"
AMIGA:barrywalker~> echo $text; printf "%0.s-" $( seq 1 1 $lentext ); echo ""
Files successfully removed...
-----------------------------
AMIGA:barrywalker~> _

This User Gave Thanks to wisecracker For This Post:
# 7  
Old 09-16-2013
thanks all of you for the reply.
Out of all the solutions, i will go for the last solution since that code is reducing the extra work of counting the number of characters been printed. i.e.

Code:
echo "files successfully moved"| awk '1; {gsub(".","-")}1'

the above code displays the characters printed by echo command as well as puts the "-" character below it.

can u explain me the above command?? specially
Code:
awk '1; {gsub(".","-")}1'


Previous Thread | Next Thread
Test Your Knowledge in Computers #676
Difficulty: Medium
The first version of the 802.11 protocol was released in 1997, and provided up to 12 MB/s link speeds.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

Print String N times the number before it

Hey All, I want want to print a string N times the number N before it. Like i have "20 hello". so i want to print hello hello hello . . . . . 20 times.. Please help me.. I am not able o figure out.. how to do the same? (8 Replies)
Discussion started by: jaituteja
8 Replies

3. Shell Programming and Scripting

awk getline 8 times and if $3 = 8 when subtracted from 1st line,print

I have kind of a strange one here. I have a file of consecutive /24 ip blocks. If there are 8 consecutive ip blocks which represent a /20 then I need to print the first line. I played around and did not get the results I need, especially when considering that the highest $3 will be is 255 and then... (6 Replies)
Discussion started by: numele
6 Replies

4. Programming

Print line by character in C

Hello, I am reading the K & R C book, and trying to understand more about putchar()/getchar() by practice. Similar to the exercise 1-12, I want try to parse each line by character. Although there are many site about the exercise, but I could find similar case of mine, which is similar to fold... (11 Replies)
Discussion started by: yifangt
11 Replies

5. Shell Programming and Scripting

Tabbing a line a variable number of times in shell

Hi, I had another question. I was wondering if there was a way to tab a line a variable number of times in tcsh. To go into details, I want to tab a line by how deep a file is in its path. So here is an example code: set filea=/blah1/blah2/blah3 set fileb=/blah1/blah2/blah3/blah4 set... (4 Replies)
Discussion started by: chu816
4 Replies

6. Shell Programming and Scripting

Print a word specific number of times

Hi All, I wanted to know if there is a shell command to print a word n number of times The Input File is : Cat 4 Bat 3 Zall 1 Kite 2 Output File required is : Cat Cat Cat Cat Bat Bat Bat Zall Kite (4 Replies)
Discussion started by: sam_2921
4 Replies

7. Shell Programming and Scripting

Print selection of line based on line number

Hi Unix gurus Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number. I am trying to use the following code. but it is not working. cat... (3 Replies)
Discussion started by: mohanm
3 Replies

8. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

9. UNIX for Dummies Questions & Answers

Counting the number of times a character appears

I am looking for a bash command that counts the number of times a character appears in a file. For example "I am a newbie, trying to learn shell script". Then the command counts the number of e and gives them as 4. Also I want one that counts the number of times a character in a string is replaced.... (2 Replies)
Discussion started by: #moveon
2 Replies

10. Shell Programming and Scripting

AWK print a character many times

How can I print a '-' on the same line within awk, say 50 times, without actually typing '-' 50 times? Cheers (3 Replies)
Discussion started by: dbrundrett
3 Replies

Featured Tech Videos