bash 使用tail -n读取行时如何使用

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

How to use while read line with tail -n

bashcsvwhile-looptail

提问by 3kstc

Problem:I have a CSV dump file - with excess of 250,000 lines. When I use while read- it takes a while (no pun intended). I would like to go back to the last 10,000 lines to do what I need to do instead of the 250,000 lines.

问题:我有一个 CSV 转储文件 - 超过 250,000 行。当我使用时while read- 需要一段时间(没有双关语)。我想回到最后 10,000 行来做我需要做的事情,而不是 250,000 行。

Code Snippet:My current code is this:

代码片段:我当前的代码是这样的:

IFS=","
while read line
do

    awk_var=`echo "$line" | awk -F" " '{print 
done </some_directory/directory/file_in_question.csv
}'` var_array=($awk_var) read -a var_array <<< "${awk_var}" echo "${var_array[1]}" done </some_directory/directory/file_in_question.csv

Question:How can I use tail -n10000with while read linewhen reading the file_in_question.csvwith a bash script?

问题:在阅读with bash 脚本时如何使用tail -n10000with ?while read linefile_in_question.csv

回答by John1024

Replace:

代替:

done < <(tail -n10000 /some_directory/directory/file_in_question.csv)

with:

和:

awk -F, '{print }' /some_directory/directory/file_in_question.csv

The <(...)construct is called process substitution. It creates a file-like object that bash can read from. Thus, this replaces reading from some_directory/directory/file_in_question.csvdirectly with reading from tail -n10000 /some_directory/directory/file_in_question.csv.

<(...)构造称为进程替换。它创建了一个类似文件的对象,bash 可以从中读取。因此,这将some_directory/directory/file_in_question.csv直接从读取替换为从读取tail -n10000 /some_directory/directory/file_in_question.csv

Using process substitution like this allows you to keep your whileloop in the main shell, not a subshell. Because of this, variables that you create in the whileloop will retain their value after the loop exits.

使用像这样的进程替换允许您将while循环保留在主 shell 中,而不是子 shell 中。因此,您在while循环中创建的变量将在循环退出后保留其值。

Speeding up the original code

加速原始代码

The code as shown prints the second column of a CSV file. If that is all that the code is supposed to do, then it can be replaced with:

所示代码打印 CSV 文件的第二列。如果这就是代码应该做的所有事情,那么它可以替换为:

IFS=","
tail /var/log/httpd/error_log | while read foo bar
do
    echo $foo
done

回答by hendry

Something like:

就像是:

while : 
do read l || { sleep 1 ; continue; }
   echo "==> $l"
done < /var/log/httpd/error_log

I recommend you do the splitting in bash with read, instead of calling awkinefficiently there. Obviously rewriting the whole thing as an awk script will be faster than shell, but awk is harder less common language.

我建议您使用 bash 进行拆分read,而不是在awk那里低效调用。显然,将整个内容重写为 awk 脚本会比 shell 更快,但 awk 是更难的通用语言。

回答by Phi

Or this one.

或者这个。

##代码##