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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21968938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:39:53  来源:igfitidea点击:

Read user given file character by character in bash

bashshell

提问by NoobEditor

I have a file which is kind of unformatted, I want to place a new-line after every 100th character and remove any other new lines in it so that file may look with consistent width and readable

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

This code snippet helps read all the lines

此代码片段有助于阅读所有行

 while read LINE
        do
                len=${#LINE}
                echo "Line length is : $len"
        done < $file

but how do i do same for characters

但我如何对角色做同样的事情

Idea is to have something like this : (just an example, it may have syntax errors, not implemented yet)

想法是有这样的东西:(只是一个例子,它可能有语法错误,尚未实现

 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

I am learning bash, so please go easy!!

我正在学习bash,所以请放轻松!!

回答by devnull

I want to place a new-line after every 100th character and remove any other new lines in it so that file may look with consistent width and readable

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

Unless you have a good reason to write a script, go ahead but you don't need one.

除非您有充分的理由编写脚本,否则请继续,但您不需要脚本。

Remove the newline from the input and fold it. Saying:

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

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

should achieve the desired result.

应该能达到预期的效果。

回答by chepner

bashadds a -nflag to the standard readcommand to specify a number of characters to read, rather than a full line:

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

while read -n1 c; do
    echo "$c"
done < $file

回答by Paused until further notice.

You can call the function below in any of the following ways:

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

line_length=100
wrap $line_length <<< "$string"
wrap $line_length < file_name
wrap $line_length < <(command)
command | wrap $line_length

The function reads the input line by line (more efficiently than by character) which essentially eliminates the existing newlines (which are replaced by spaces). The remainder of the previous line is prefixed to the current one and the result is split at the desired line length. The remainder after the split is kept for the next iteration. If the output buffer is full, it is output and cleared otherwise it's kept for the next iteration so more can be added. Once the input has been consumed, there may be additional text in the remainder. The function is called recursively until that is also consumed and output.

该函数逐行读取输入(比按字符更有效),这基本上消除了现有的换行符(由空格替换)。前一行的其余部分作为当前行的前缀,结果按所需的行长度拆分。拆分后的剩余部分保留用于下一次迭代。如果输出缓冲区已满,则将其输出并清除,否则将保留用于下一次迭代,以便可以添加更多内容。一旦输入被消耗,剩余部分可能会有额外的文本。该函数被递归调用,直到它也被消耗和输出。

wrap () { 
    local remainder rest part out_buffer line len=
    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
}

回答by NoobEditor

#!/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

Are you looking for something like this?

你在寻找这样的东西吗?