V
V
Vladimir2019-11-03 21:40:50
linux
Vladimir, 2019-11-03 21:40:50

How to generate a fixed string from a number in bash?

you need to output sequentially a string like "A0000".
The question is how to iteratively get a number and make the desired string out of it.
for example: generate from 1 string like "0001".
now I'm doing it with 5 nested loops.... but it doesn't seem like a very pretty way.

#!/bin/bash

allThreads=("B" "C" "D" "F" "G" "H" "J" "K" "L" "M" "N" "P" "Q" "R" "S" "T" "V" "W" "X" "Y" "Z")

echo "INSERT INTO public.audience (audience_name, \"limit\") VALUES " 

for ((l = 0 ; l < 10 ; l++)); do
    for ((g = 0 ; g < 10 ; g++)); do
        for ((k = 0 ; k < 10 ; k++)); do
            for ((j = 0 ; j < 10 ; j++)); do
                  for ((i = 0 ; i < 10 ; i++)); do

        audience=${allThreads[$l]}
                num=${g}${k}${j}${i}
             audience="${audience}${num}"

    echo "('${audience}'::varchar(5), `shuf -i1-40 -n1`), "

         done
       done
    done
  done
done

echo " ('Z1111'::varchar(5), `shuf -i1-40 -n1`);  "

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Fedoseev, 2019-11-03
@idskill

man printf

S
Saboteur, 2019-11-04
@saboteur_kiev

#!/bin/bash
for letter in {A..Z}; do
  for number in $(seq -w 0 100); do
    echo "('${letter}${number}'::varchar(5), `shuf -i1-40 -n1`), "
  done
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question