![用点完成每行中的字符数](https://linux22.com/image/140566/%E7%94%A8%E7%82%B9%E5%AE%8C%E6%88%90%E6%AF%8F%E8%A1%8C%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E6%95%B0.png)
我想知道如何计算文本中每一行的字符数,然后从阈值中减去这个数字表示每行允许的最大字符数,然后我想填补最大字符数和带点的字符数。例如:
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
对于文本处理任务,我建议使用 Awk 或 Perl 之类的东西来代替bash
例如
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