0

I would like to know how to count the characters in every line in a text, then subtracting this number from a threshold represents the maximum number of characters allowed per line, after then I would like to fill the gaps between the maximum number of characters and the count of characters with dots. For example:

Unix was 
originally meant 
to be a 
co
nvenient p
latform for progra
mmers developing software to
 be run on it and on other 
systems, rather than for non-
programmers.
[7][8] The system grew larger
 as the operating system star
ted spreading in a
cademic circ
les, as users add
ed their own tools to th
e system and shared them wi
th colleagues.

The maximum number of characters over all the lines is /31/ in line#11. I would like the count of characters in every line to be /31/ by filling the empty spaces with dots, like this:

Unix was , .....................
originally meant , .............
to be a , ......................
co, ............................
nvenient p, ....................
latform for progra, ............
mmers developing software to, ..
be run on it and on other , ....
systems, rather than for non-,..
programmers., ..................
[7][8] The system grew larger,..
as the operating system star, ..
ted spreading in a, ............
cademic circ, ..................
les, as users add, .............
ed their own tools to th, ......
e system and shared them wi, ...
th colleagues., ................

How can I do that in bash?

0

3 Answers 3

2

For text processing tasks, I'd suggest using something like Awk or Perl in place of bash e.g.

perl -lnE '
  push @a, $_; $max = length $_ > $max ? length $_ : $max 
  }{ 
  foreach $x (@a) {say $x, ", ", "."x($max - length $x)}
' file
Unix was , ....................
originally meant , ............
to be a , .....................
co, ...........................
nvenient p, ...................
latform for progra, ...........
mmers developing software to, .
 be run on it and on other , ..
systems, rather than for non-, 
programmers., .................
[7][8] The system grew larger, 
 as the operating system star, 
ted spreading in a, ...........
cademic circ, .................
les, as users add, ............
ed their own tools to th, .....
e system and shared them wi, ..
th colleagues., ...............
0

Try this code:

#!/bin/bash
 # This loop is to count the number of bytes per line, then it will find the max number of bytes over all the lines
max=$(cat datafile| while IFS=" " read line; do echo "${line}" | wc -c; done | sort -k 1.1n | tail -n1)

cat datafile| while IFS=" " read line; do 
# Count of bytes in every line
n=$(echo "${line}" | wc -c)     

# bytes difference in every line
diff=$(echo $((${max}-${n})))  

# complete the number of bytes per line to the max number of bytes over all the lines.
dash=$(printf %"${diff}"s | tr " " ".")

# print results
echo ${line},${dash}
done

output:

Unix was,.....................
originally meant,.............
to be a,......................
co,...........................
nvenient p,...................
latform for progra,...........
mmers developing software to,.
be run on it and on other,....
systems, rather than for non-,
programmers.,.................
[7][8] The system grew larger,
as the operating system star,.
ted spreading in a,...........
cademic circ,.................
les, as users add,............
ed their own tools to th,.....
e system and shared them wi,..
th colleagues.,...............
0

Another way is filing every lines with dots at the end and cut off the first 31characers.

sed "s/$/,$(printf '%.1s' .{1..31})/" infile | cut -c-31