Adding variable number of space in between two string

Hi,

I am looking for the way to add variable number of spaces between two string. e.g input line is
a ,bb
abc ,bcb
pqr ,bfg

My output should be something like this

a                                   ,bb
abc                    ,bcb
pqr                 ,bfg

This text are basically inside a file. So I tried like this

while read line
do
        IFS=','
        set $line
        printf '%s' "$1" >> out.txt
        ## Here I have to include space to arrange line properly
        printf '%s,' "$pading" >> out.txt 
        printf '%s\n' "$2" >> out.txt
 
done < $fname

Thanks

---------- Post updated at 04:18 AM ---------- Previous update was at 04:13 AM ----------

Well I am not able to show the require output properly . The only thing which I want is second column should be start at same point.Like
bg
bc

Not like
bg
"some spaces" bc

This may help:

IFS="${IFS},"
while read f1 f2; do
        printf '%-20s%s\n' "$f1" "$f2"
done

which generates:

a                   bb
abc                 bcb
pqr                 bfg

Thanks. But it is not working

I checked the script:

$ sh diehard.sh < diehard.txt
a                   bb
abc                 bcb
pqr                 bfg

$ cat diehard.sh
IFS="${IFS},"
while read f1 f2; do
        printf '%-20s%s\n' "$f1" "$f2"
done

$ cat diehard.txt
a ,bb
abc ,bcb
pqr ,bfg

Can you post your version of the script and what it is (or is not) producing?

1 Like

Hello,

Could you please try following, it may help you.

awk -F, 'FNR==NR{l=length($1) > l?length($1):l;next} {$1=sprintf("%-"l"s",$1)} 1' OFS=, Input_file Input_file

Output will be as follows.

a                                    ,bb
abc                                  ,bcb
pqr                                  ,bfg

Thanks,
R. Singh

Well the field is separated by , so I can have pq abc , pq. In that case your script won't work.
By the way thanks for your help.

Hello,

Following is working even space is there in first field, let's say we have following input file.

Input_file:

cat check_space
a                                   ,bb
abc                    ,bcb
pqr ab              ,bfg

Output will be as follows.

awk -F, 'FNR==NR{l=length($1) > l?length($1):l;next} {$1=sprintf("%-"l"s",$1)} 1' OFS=, Input_file Input_file
 
a                                     ,bb
abc                                   ,bcb
pqr ab                                ,bfg

Thanks,
R. Singh

1 Like

Yes it working. Thanks.

Can we have any kind of separator like | or : or ; ?

With respect R. Singh, your code has an interesting side effect if the width of the line of data is not non-decreasing, as in:

a   ,bb
abc                        ,bcb
pqr ab    ,bfg

Which results in:

a   ,bb
abc                        ,bcb
pqr ab                     ,bfg

which may or may not be desirable. For a constant width, perhaps:

awk -F ' *,' '{printf "%-20sn,%s\n", $1, $2; }' inputfile
a                   ,bb
abc                 ,bcb
pqr ab              ,bfg

Depends what the end result is supposed to be....

Yes, offcourse we can have different seprators in code, also I have changed my solution a bit in previous posts in which I am reading file 2 times. Following is an example for separator ; .

Input_file:

cat check_space
a                                   ;bb
abc                    ;bcb
pqr ab                                ;bfg

Output will be as follows.

awk -F";" 'FNR==NR{l=length($1) > l?length($1):l;next} {$1=sprintf("%-"l"s",$1)} 1' OFS=";"  Input_file Input_file
 
a                                     ;bb
abc                                   ;bcb
pqr ab                                ;bfg

Thanks,
R. Singh

---------- Post updated at 07:29 AM ---------- Previous update was at 06:56 AM ----------

Hi derekludwig,

I have fixed the same and mentioned in POST#10. Also you can't hardcode value of "%-20s as it is dependent on 1st field.
so we can better take length of first field and do the operation accordingly.

Thanks,
R. Singh