각 줄의 문자 수를 점으로 완성하세요. [중복]

각 줄의 문자 수를 점으로 완성하세요. [중복]

텍스트의 각 줄에 대한 문자 수를 계산한 다음 줄당 허용되는 최대 문자 수를 나타내는 임계값에서 이 숫자를 뺀 다음 최대 문자 수와 문자 수를 채우고 싶습니다. 점으로. 예를 들어:

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.

11번째 줄의 모든 줄에 대한 최대 문자 수는 /31/입니다. 다음과 같이 공백을 점으로 채워 각 줄의 문자 수를 /31/로 설정하고 싶습니다.

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

어떻게 해야 합니까 bash?

답변1

bash텍스트 처리 작업의 경우 예를 들어 Awk 또는 Perl과 같은 것을 사용하는 것이 좋습니다 .

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

답변2

다음 코드를 사용해 보세요:

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

산출:

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

답변3

또 다른 방법은 각 줄의 끝에 점을 추가하고 처음 31자를 자르는 것입니다.

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

관련 정보