我有一个文件,格式不规范,我想在每100个字符后面加上一个换行符,并删除其中的任何其他换行符,以便使该文件看起来具有一致的宽度和可读性。
此代码片段可帮助读取所有行。
此代码片段可帮助读取所有行。
while read LINE
do
len=${#LINE}
echo "Line length is : $len"
done < $file
但是如何对字符执行相同的操作呢?
想法是创建类似这样的内容:(只是一个示例,可能存在语法错误,尚未实现)
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
我正在学习 bash
,请温柔点!
n1
中的1
是什么意思?它是否包括空格和换行符? - NoobEditor