Jepessen
Jepessen

Reputation: 12415

Create a string of same size of another one

In a bash script, I want to write something like:

=====================================
= Doing something in THIS_DIRECTORY =
=====================================

Actually in my script I use:

echo "====================================="
echo "= Doing something in $VARIABLE ="
echo "====================================="

This works only if $VARIABLE value has the right length, otherwise there will be a misalignment, like appears below:

=====================================
= Doing something in THIS_IS_ANOTHER_DIRECTORY =
=====================================

How can I change length of upper and lower strings dynamically in order to maintain the same length of middle row?

Upvotes: 2

Views: 170

Answers (6)

potong
potong

Reputation: 58391

This might work for you (GNU sed):

sed 'h;s/./=/g;H;G' <<<"= Doing something in $VARIABLE ="

EDIT:

Copy the original string in the pattern space (PS) to the hold space (HS), replace every character in the PS with an =, then append the PS to the HS and lastly append the HS to the PS and print out the PS.

Upvotes: 3

John B
John B

Reputation: 3646

You could use printf with seq to expand to the desired length.

var="= Doing something in $VARIABLE ="
equals=$(printf "%0.s=" $(seq ${#var}))
printf "$equals\n$var\n$equals\n"

Upvotes: 1

Fredrik Pihl
Fredrik Pihl

Reputation: 45652

Another way that allows you to add coloring

#!/bin/bash

function pretty_p {
    # Pretty-print a header
    echo -e "\e[00;32m"
    for i in in $(seq 1 ${#1}); do printf "%s" "=";done;echo
    echo $1
    for i in in $(seq 1 ${#1}); do printf "%s" "=";done
    echo -e "\e[00m"
}


VARIABLE="foobar"
X="= Doing something in $VARIABLE ="

pretty_p "$X"

Output (black-n-white unfortunately instead of cool grenish):

==============================
= Doing something in foobar =
==============================

Upvotes: 1

Fazlin
Fazlin

Reputation: 2337

One way is to calculate the length of $VARIABLE and then echo equal-to(=) based on that.

var=$(echo "= Doing something in ${variable} =")
equal=$(printf "%-"${#var}"s" "=")
echo "${equal// /=}"; echo ${var}; echo "${equal// /=}"

var will contain the middle line text. ${#var} will return the length of the middle line.

Upvotes: 2

NoDataFound
NoDataFound

Reputation: 11959

You can do that:

writenchar() {
  local length=$1
  local char=$2

  for (( i = 0; i < $length; ++i )); do
    echo -n "$char"
  done
}

You would have to compute the length of middle string:

declare middleString="= Doing something in ${VARIABLE} ="
declare border=$(writenlchar "${#middleString}" '=')

And echo it:

echo "${border}"
echo "${middleString}"
echo "${border}"

Since it is a loop, I store the result in a variable because it will cost less. If you don't want that, then the code would look like this:

writenlchar "${#middleString}" '='
echo ""
echo "${middleString}"
writenlchar "${#middleString}" '='
echo ""

The empty echo are here to dump a newline.

Tell me if it works (I did not test).

Upvotes: 1

Cyrus
Cyrus

Reputation: 88583

VARIABLE="foobar"
X="= Doing something in $VARIABLE ="
echo "${X//?/=}"
echo "$X"
echo "${X//?/=}"

Output:

=============================
= Doing something in foobar =
=============================

Upvotes: 6

Related Questions