Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中逐个字符读取用户给定的文件_Bash_Shell - Fatal编程技术网

在bash中逐个字符读取用户给定的文件

在bash中逐个字符读取用户给定的文件,bash,shell,Bash,Shell,我有一个未格式化的文件,我想在每100个字符后放置一行新行,并删除其中的任何其他新行,以便文件看起来具有一致的宽度和可读性 此代码段有助于读取所有行 while read LINE do len=${#LINE} echo "Line length is : $len" done < $file 读取行时 做 len=${LINE} echo“线路长度为:$len” 完成

我有一个未格式化的文件,我想在每100个字符后放置一行新行,并删除其中的任何其他新行,以便文件看起来具有一致的宽度和可读性

此代码段有助于读取所有行

 while read LINE
        do
                len=${#LINE}
                echo "Line length is : $len"
        done < $file
读取行时
做
len=${LINE}
echo“线路长度为:$len”
完成<$file
但是我如何对角色做同样的事情呢

想法是这样的:(只是一个例子,它可能有语法错误,还没有实现)

读取时读取字符
做
chcount++#递增字符计数
if[“$chcount”-eq“100”&&“$ch”!=“\n”]#如果第100个字符且不是新行
然后
echo-e“\n”#echo新行
elif[“$ch”=”\n“]#如果字符不是第100个而是新行
然后
ch=”“$将其替换为空格
fi
完成<$file
我正在学习bash,所以请放松

我想在每100个字符后放置一行新行,并删除任何 在其中添加其他新行,使文件看起来具有一致的宽度和宽度 可读的

除非你有很好的理由去写剧本,否则尽管去吧,但你并不需要

从输入中删除换行符并将其折叠。说:

tr -d '\n' < inputfile | fold -w 100
tr-d'\n'

应达到所需的结果。

bash
在标准的
read
命令中添加一个
-n
标志,以指定要读取的字符数,而不是整行:

while read -n1 c; do
    echo "$c"
done < $file
读取时-n1c;做
回音“$c”
完成<$file

您可以通过以下任一方式调用下面的函数:

line_length=100
wrap $line_length <<< "$string"
wrap $line_length < file_name
wrap $line_length < <(command)
command | wrap $line_length
行长度=100
包装$line_length
#/bin/bash
w=~/testFile.txt
chcount=0
读r字时;做
len=${word}

因为((i=0;i@NoobEditor我会推迟,因为现在时间不够。也就是说,重新发明轮子听起来很少有说服力。让我知道你什么时候能做到……耐心是我的美德……同时,如果我上网,我会删除这个问题!:)@NoobEditor我会给你一个提示:
read
会读一行输入(不包含换行符).
n
我明白了,
n1
中的
1
是什么?还有,它读的是
whites spaces
new line
吗?
-n
是一个强制参数;
-n2
一次读两个字符。仔细看你的答案……现在我意识到我根本不懂
bash脚本
line_length=100
wrap $line_length <<< "$string"
wrap $line_length < file_name
wrap $line_length < <(command)
command | wrap $line_length
wrap () { 
    local remainder rest part out_buffer line len=$1
    while IFS= read -r line
    do
        line="$remainder$line "
        (( part = $len - ${#out_buffer} ))
        out_buffer+=${line::$part}
        remainder=${line:$part}
        if (( ${#out_buffer} >= $len ))
        then
            printf '%s\n' "$out_buffer"
            out_buffer=
        fi
    done
    rest=$remainder
    while [[ $rest ]]
    do
        wrap $len <<< "$rest"
    done
    if [[ $out_buffer ]]
    then
        printf '%s\n' "$out_buffer"
        out_buffer=
    fi
}
#!/bin/bash
w=~/testFile.txt
chcount=0
while read -r word ; do
        len=${#word}
        for (( i = 0 ; i <= $len - 1 ; ++i )) ; do
                let chcount+=1
                if [ $chcount -eq 100 ] ; then
                        printf "\n${word:$i:1}"
                        let chcount=0
                else
                        printf "${word:$i:1}"
                fi
        done
done < $w