Home > Mobile >  How to check if a value contains characters in bash
How to check if a value contains characters in bash

Time:11-06

I have values such as

B146XYZ, G638XYZ, G488xBC

I have to write a bash script where when it sees comma it has to remove the comma and add 7 spaces to it and also if it sees comma and a space or just space(no punctuations) it has to add 7 spaces to make all of them fixed length.

if [[  $row = *’,’* ]]
then
first= “${ row%%,*}echo${first }

I tried but can’t understand how to add conditions for the remaining criteria specially struggling with single value conditions such as G488xBC

CodePudding user response:

What about just:

sed -E 's/[, ] /       /g' file

Or something like this will print a padded table, so long as no field is longer than 13 characters:

awk -F '[,[:space:]] ' \
'{
    for (i=1; i<NF; i  ) {
        printf("%-14s", $i)
    }

    print $NF
}'

Or the same thing in pure bash:

while IFS=$', \t' read -ra vals; do
    last=$((${#vals[@]} - 1))

    for ((i=0; i<last; i  )); do
        printf "%-14s" "${vals[i]}"
    done

    printf '%s\n' "${vals[last]}"
done

CodePudding user response:

newrow="${row//,/       }"
  • Related