我有一个未格式化的文件,我想在每100个字符后放置一个换行符并删除其中的任何其他新行,以便文件看起来具有一致的宽度和可读性
此代码段有助于阅读所有行
while read LINE
do
len=${#LINE}
echo "Line length is : $len"
done < $file
Run Code Online (Sandbox Code Playgroud)
但我如何为角色做同样的事情
想法是这样的:( 只是一个例子,它可能有语法错误,尚未实现)
while read ch #read character
do
chcount++ # increment character count
if [ "$chcount" -eq "100" && "$ch"!="\n" ] #if 100th character and is not a new line
then
echo -e "\n" #echo new line
elif [ "$ch"=="\n" ] #if character is not 100th but new line
then
ch=" " $replace it with space
fi
done < $file
Run Code Online (Sandbox Code Playgroud)
我正在学习bash
,所以请放轻松!
我想在每100个字符后放置一个换行符并删除其中的任何其他新行,以便文件看起来具有一致的宽度和可读性
除非你有充分的理由编写脚本,否则请继续,但不需要.
从输入中删除换行并折叠它.他说:
tr -d '\n' < inputfile | fold -w 100
Run Code Online (Sandbox Code Playgroud)
应该达到预期的效果.
bash
-n
向标准read
命令添加一个标志以指定要读取的字符数,而不是整行:
while read -n1 c; do
echo "$c"
done < $file
Run Code Online (Sandbox Code Playgroud)