help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shifting a block of text


From: Kerin Millar
Subject: Re: Shifting a block of text
Date: Wed, 8 Dec 2021 09:13:32 +0000

On Wed, 8 Dec 2021 09:03:56 +0000
Kerin Millar <kfm@plushkava.net> wrote:

> On Wed, 8 Dec 2021 08:37:52 +0100 (CET)
> fatiparty--- via <help-bash@gnu.org> wrote:
> 
> > 
> > Trying to adapt the following function to use all arguments rather than 
> > just the first one.
> > The code should introduce nc spaces ahead of the start of each argument 
> > passed. 
> > Each shifted argument must be on  new line.  The first argument ins 
> > numeric, defining
> > the amount of shift.
> > 
> > shifted-block ()
> > {
> >  if (( $# == 1 )); then
> >    nc=0 ; arg="$1"
> >  elif (( $# >= 2 )); then
> >    nc="$1" ; arg="$2"
> >  else
> >    nc=0 ; arg="$1"
> >  fi
> > 
> >  nw=$(( nc + ${#arg} ))
> >  printf "%${nw}s\n" "$arg"
> > }
> 
> shifted-block() {
>       local n=$1 spaces
>       shift
>       if (( $# )); then
>               printf -v spaces "%${n}s"
>               printf '%s\n' "${@/#/$spaces}"
>       fi
> }

On second thoughts, you'd be better off injecting the spaces into the format 
string, as Chris did. It's slightly faster.

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]