Efficiently Repeat Text


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Efficiently Repeat Text
# 1  
Old 04-22-2010
Efficiently Repeat Text

Hi,

Often when I use echo statements in scripts I echo a line of #'s above and below. For example:

Code:
echo #####
echo hello world
echo #####

However, I generally have a series of about 75 #'s. For example:

Code:
echo #(x 75)
echo hello world
echo #(X 75)

While this helps to delineate sections in my terminal output, my code is filled with lines of #'s. Is there a way to tell the echo command to repeat a # 75 times, so I don't have to type them individually?
# 2  
Old 04-22-2010
Hi.

There is a few good answers here: https://www.unix.com/shell-programmin...er-printf.html

Modifying post #8 from vgersh99 slightly to fit your needs:
Code:
awk 'BEGIN{$75=OFS="#";print}'

# 3  
Old 04-23-2010
What I do in many of my scripts is create a function at the beginning called separator and then whenever I need to separate things, I just call on it.

Code:
$cat example
separator () #Function that draws separator
    {
        printf "${LB}==============================================================================================================${N}\n"
    }
separator 
echo "This is an example"
separator 

$./example
==============================================================================================================
This is an example
==============================================================================================================

Edit: The {LB} and {N} are there because in the script I copied this out of, I was using ANSI colour codes to colour the separator.
# 4  
Old 04-23-2010
Quote:
Originally Posted by thegeek
When you can write function, you can do lot of optimization such as

Code:
separator()
{
for (( i=0 ; i<$1 ; i++ )) ;
do
        echo -n "#"
done
}

separator 10;

Sorry to thread jack. I'm pretty familiar with bash functions, and I use them regularly in my scripting, but I was wondering if you could just give me an idiots guide of the loop you wrote there. I understand what it's doing, but I don't really know the C style i++...etc notation.

I know I could get the same results with:

Code:
sep ()
{
    i=0;
    while [[ $i -lt $1 ]]; do
        echo -n "#";
        i=$(($i + 1));
    done
}
[~]$ sep 10
##########

But I'd like to understand your way (and know if there is an advantage of yours over mine).
# 5  
Old 04-23-2010
Combining the other methods into a slightly shorter idea:

Code:
separator()
{
awk -v num=$1 'BEGIN{$num=OFS="#";print}'
}

separator 10;

# 6  
Old 04-23-2010
Quote:
Originally Posted by DeCoTwc
Sorry to thread jack. I'm pretty familiar with bash functions, and I use them regularly in my scripting, but I was wondering if you could just give me an idiots guide of the loop you wrote there. I understand what it's doing, but I don't really know the C style i++...etc notation.
Code:
#!/bin/bash
# The first expression is evaluated once, before the loop is entered.
# Usually used for assignments.  You could put more than one, separated
# by commas, but that's almost never done.

# The second is a condition.  Whenever it's true, it continues looping.
# If you put 0, the inner loop would never be executed at all.  If you put 1, it'd loop forever.

# The third expression is evaluated after every loop.  Usually used to
# increase a value but you could put any expression there.

# the do/done section is just like a while.
for ((  N=0 ; N<10 ; N++ ))
do
        echo "ASDF"
done

These structures work in bash and newer versions of ksh, but don't work in minimal or old implementations of sh.

---------- Post updated at 02:16 PM ---------- Previous update was at 02:14 PM ----------

Quote:
Originally Posted by treesloth
Combining the other methods into a slightly shorter idea:

Code:
separator()
{
awk -v num=$1 'BEGIN{$num=OFS="#";print}'
}

separator 10;

Forking off an external process to do it for you is not an efficient way to print ten hatch marks.

Personally, I'd just do:

Code:
SEPARATOR="##############"

then just "echo $SEPERATOR" whenever I need to.
# 7  
Old 04-23-2010
Quote:
Originally Posted by Corona688
Forking off an external process to do it for you is not an efficient way to print ten hatch marks.
No, it isn't, but recall that that's not the point of doing it that way.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Purging 2000+ directories efficiently

Hi I have a requirement wherein i need to purge some directories. I have more than 2000 directories where i need to keep data for 10 days and delete the rest. What i am looking for is an efficient way to achieve this. There are four mount points from where i need to delete the files. ... (3 Replies)
Discussion started by: Apoorvbarwa
3 Replies

2. Shell Programming and Scripting

Getting remote variables more efficiently

Hello all, I have a script that has to get variables remotely. Rather than having the script login to the remote server 3 separate times, is there a faster way to get each variable? ##Server comes from input or list## CHKINSTALL=`ssh server "swlist | grep -i program" | grep -v... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

3. Shell Programming and Scripting

How to parse a string efficiently

I am new to the boards and to shell programming and have a requirement to name new files received with a unique sequence number. I need to look at a particular file pattern that exists and then to increment a sequence by 1 and write the new file. Example of file names and sequence # ... (4 Replies)
Discussion started by: sandiego_coder
4 Replies

4. Shell Programming and Scripting

efficiently split a 2GB text file into two

Can an expert kindly write an efficient Linux ksh script that will split a large 2 GB text file into two? Here is a couple of sample record from that text file: "field1","field2","field3",11,22,33,44 "TG","field2b","field3b",1,2,3,4 The above rows are delimited by commas. This script is to... (2 Replies)
Discussion started by: ihot
2 Replies

5. UNIX Desktop Questions & Answers

how to search files efficiently using patterns

hi friens, :) if i need to find files with extension .c++,.C++,.cpp,.Cpp,.CPp,.cPP,.CpP,.cpP,.c,.C wat is the pattern for finding them :confused: (2 Replies)
Discussion started by: arunsubbhian
2 Replies

6. Shell Programming and Scripting

Using xapply efficiently?

Hi all, Were currently using xapply to run multiple ssh instances that then calls a script that returns the PID of a webserver process. Currently we have like 30 xapply statements in a script call checkit which checks various webserver processes on various unix/linux boxes. My question... (0 Replies)
Discussion started by: bdsffl
0 Replies

7. Programming

Writing fast and efficiently - how ?

I have a lot of processes all of which need to write quite a lot of data to the filesystem ( to a single file). This is managed today in the following way : all the processes write the data to a shared memory block, which is manged by a process that empties it to a file, thus allowing more... (7 Replies)
Discussion started by: Seeker
7 Replies

8. Filesystems, Disks and Memory

Writing fast and efficiently - how ?

I have a lot of processes all of which need to write quite a lot of data to the filesystem ( to a single file). This is managed today in the following way : all the processes write the data to a shared memory block, which is manged by a process that empties it to a file, thus allowing more... (1 Reply)
Discussion started by: Seeker
1 Replies

9. IP Networking

how to use PING command efficiently

Do anyone telle me please how to use PING command to verify connection (TCP/IP) between serveurs. thanks (1 Reply)
Discussion started by: hoang
1 Replies
Login or Register to Ask a Question