Completando a contagem de caracteres em cada linha com pontos [duplicados]

0

Gostaria de saber como contar os caracteres em todas as linhas de um texto e, em seguida, subtrair esse número de um limite representa o número máximo de caracteres permitidos por linha, depois disso eu gostaria de preencher as lacunas entre o número máximo de caracteres e a contagem de caracteres com pontos. Por exemplo:

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.

O número máximo de caracteres em todas as linhas é / 31 / na linha # 11. Eu gostaria que a contagem de caracteres em cada linha fosse de / 31 / preenchendo os espaços vazios com pontos, assim:

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., ................

Como posso fazer isso em bash ?

    
por Isaac 13.10.2018 / 02:30

3 respostas

2

Para tarefas de processamento de texto, sugiro usar algo como Awk ou Perl no lugar de bash , por exemplo,

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., ...............
    
por 13.10.2018 / 03:09
0

Experimente este código:

#!/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

saída:

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.,...............
    
por 13.10.2018 / 02:43
0

Outra maneira é preencher todas as linhas com pontos no final e cortar os primeiros 31 caracóis.

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

Tags