テキスト内の各行の文字数を計算し、行ごとに許容される最大文字数を示すしきい値からこの数字を引いて、最大文字数と文字数を入力したいと思います。ポイントに。たとえば、
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