通过在shell脚本读取使用命令行读取输入文件跳过最后一行 [英] Reading input files by line using read command in shell scripting skips last line

查看:2830
本文介绍了通过在shell脚本读取使用命令行读取输入文件跳过最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用读命令来读取输入文件由行shell脚本行。一个例子code如下面所述一个产生一个错误的结果,如果一个新的线未在输入文件中的最后一行,blah.txt的末端插入。

I usually use the read command to read an input file to the shell script line by line. An example code such as the one below yields a wrong result if a new line isn't inserted at the end of the last line in the input file, blah.txt.

#!/bin/sh

while read line
do
echo $line
done <blah.txt

因此​​,如果输入文件中读取类似 -

So if the input file reads something like -

One 
Two
Three
Four

和我不打四回后,该脚本无法读取的最后一行,并打印

and I do not hit return after Four, the script fails to read the last line, and prints

One
Two
Three

现在,如果我离开一个额外的空白行四后一样,

Now if I leave an extra blank line after Four, like,

One 
Two
Three
Four
//blank line

在输出打印的所有线路,包括四。然而,这是不是当我使用命令读取一行的情况;所有线路包括印刷没有我不得不在最后添加一个额外的空白行的最后搞定。

the output prints all the lines, including Four. However, this is not the case when I read a line using the cat command; all lines including the last get printed without me having to add an extra blank line at the end.

任何人有为什么会这样的想法?我创建的脚本将主要由其他人来运行,所以它是没有必要的,他们打算在每个输入文件的末尾添加一个额外的空行。

Anyone has ideas on why this happens? The scripts I create will mostly be run by others, so it isn't necessary they're going to add an extra blank line at the end of every input file.

我一直在试图算出这个望穿秋水;我倒是AP preciate,如果您有任何解决方案(当然,在命令是其中之一,但我想知道背后的原因读不工作以及)。

I've been trying to figure this out for ages; I'd appreciate it if you have any solutions(of course, the cat command is one, but I'd like to know the reason behind read not working as well).

推荐答案

读取直到它找到一个换行符或文件的结尾,并返回一个非零退出$如果遇到结束文件c $ C。因此,它很可能为它同时读取一行并返回一个非零的退出code。

read reads until it finds a newline character or the end of file, and returns a non-zero exit code if it encounters an end-of-file. So it's quite possible for it to both read a line and return a non-zero exit code.

因此​​,以下code是不是安全的,如果输入的可能不是由一个换行符终止:

Consequently, the following code is not safe if the input might not be terminated by a newline:

while read LINE; do
  # do something with LINE
done

由于体内的,而不会在最后一行执行。

because the body of the while won't be executed on the last line.

从技术上说,不与新行终止的文件不是文本文件,文本工具可能在这样的一个文件奇方式失败。但是,我总是不愿意依傍这一解释。

Technically speaking, a file not terminated with a newline is not a text file, and text tools may fail in odd ways on such a file. However, I'm always reluctant to fall back on that explanation.

要解决这个问题的方法之一是测试什么读非空( -n

One way to solve the problem is to test if what was read is non-empty (-n):

while read -r LINE || [[ -n $LINE ]]; do
  # do something with LINE
done

其他的解决方案,包括使用映射文件将文件读入一个数组,通过一些实用工具,是保证正确地终止的最后一行通过管道将文件(的grep。,例如,如果你不希望处理空行),或者像 AWK (这通常是我的preference)。

Other solutions include using mapfile to read the file into an array, piping the file through some utility which is guaranteed to terminate the last line properly (grep ., for example, if you don't want to deal with blank lines), or doing the iterative processing with a tool like awk (which is usually my preference).

注意 -r 几乎可以肯定是需要在内建;它会导致不reinter preT \\ -sequences输入。

Note that -r is almost certainly needed in the read builtin; it causes read to not reinterpret \-sequences in the input.

这篇关于通过在shell脚本读取使用命令行读取输入文件跳过最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆