bash Shell 脚本:如何在 Windows 上读取不以换行符结尾的文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9408103/
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 01:38:21  来源:igfitidea点击:

Shell Script: how to read a text file that does not end with a newline on Windows

bashshell

提问by user1010399

The following program reads a file and it intends to store the all values (each line) into a variable but doesn't store the last line. Why?

下面的程序读取一个文件,它打算将所有值(每一行)存储到一个变量中,但不存储最后一行。为什么?

file.txt:

文件.txt:

   1
   2
   .
   .
   .
   n

Code:

代码

 FileName=file.txt

if test -f $FileName         # Check if the file exists
    then
        while read -r line
        do    
            fileNamesListStr="$fileNamesListStr $line"

        done < $FileName
    fi

  echo "$fileNamesListStr"  // 1 2 3 ..... n-1 (but it should print up to n.)

回答by glenn Hymanman

Instead of reading line-by-line, why not read the whole file at once?

与其逐行读取,为什么不一次读取整个文件呢?

[ -f $FileName ] && fileNameListStr=$( tr '\n' ' ' < $FileName )

回答by Ade YU

One probable cause is that there misses a newline after the last line n.

一个可能的原因是在最后一行之后缺少换行符n

Use the following command to check it:

使用以下命令进行检查:

tail -1 file.txt

And the following fixes:

以及以下修复:

echo >> file.txt

If you really need to keep the last line withoutnewline, I reorganized the whileloop here.

如果你真的需要保留没有换行符的最后一行,我在while这里重新组织了循环。

#!/bin/bash
FileName=0
if test -f $FileName ; then
    while [ 1 ] ; do    
        read -r line
        if [ -z $line ] ; then
            break
        fi
        fileNamesListStr="$fileNamesListStr $line"
    done < $FileName
fi
echo "$fileNamesListStr"

回答by jordanm

The issue is that when the file does not end in a newline, read returns non-zero and the loop does not proceed. The read command will still read the data, but it will not process the loop. This means that you need to do further processing outside of the loop. You also probably want an array instead of a space separated string.

问题是当文件不以换行符结尾时, read 返回非零并且循环不会继续。读取命令仍会读取数据,但不会处理循环。这意味着您需要在循环之外做进一步的处理。您可能还需要一个数组而不是空格分隔的字符串。

FileName=file.txt

文件名=文件.txt

if test -f $FileName         # Check if the file exists
    then
        while read -r line
        do    
            fileNamesListArr+=("$line")

        done < $FileName

        [[ -n $line ]] && fileNamesListArr+=("$line")
fi

echo "${fileNameListArr[@]}"

See the "My text files are broken! They lack their final newlines!" section of this article: http://mywiki.wooledge.org/BashFAQ/001

请参阅“我的文本文件已损坏!他们缺少最后的换行符!” 本文部分:http: //mywiki.wooledge.org/BashFAQ/001

回答by ArunGJ

As a workaround, before reading from the text file a newline can be appended to the file.

作为一种解决方法,在从文本文件读取之前,可以将换行符附加到文件中。

echo "\n" >> $file_path

This will ensure that all the lines that was previously in the file will be read. Now the file can be read line by line.

这将确保之前在文件中的所有行都将被读取。现在可以逐行读取文件。