循环浏览Bash中的文件内容?

如何用Bash迭代文本文件的每一行?

用这个脚本:

echo "Start!" for p in (peptides.txt) do echo "${p}" done 

我在屏幕上得到这个输出:

 Start! ./runPep.sh: line 3: syntax error near unexpected token `(' ./runPep.sh: line 3: `for p in (peptides.txt)' 

(后来我想用$ p做一些更复杂的事情,而不仅仅是输出到屏幕上。)


环境变量SHELL是(来自env):

 SHELL=/bin/bash 

/bin/bash --version输出:

 GNU bash, version 3.1.17(1)-release (x86_64-suse-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc. 

cat /proc/version输出:

 Linux version 2.6.18.2-34-default (geeko@buildhost) (gcc version 4.1.2 20061115 (prerelease) (SUSE Linux)) #1 SMP Mon Nov 27 11:46:27 UTC 2006 

文件peptides.txt包含:

 RKEKNVQ IPKKLLQK QYFHQLEKMNVK IPKKLLQK GDLSTALEVAIDCYEK QYFHQLEKMNVKIPENIYR RKEKNVQ VLAKHGKLQDAIN ILGFMK LEDVALQILL 

一种方法是:

 while read p; do echo $p done <peptides.txt 

例外的是,如果循环体可以从标准输入读取 ,则可以使用不同的文件描述符来打开文件:

 while read -u 10 p; do ... done 10<peptides.txt 

这里,10只是一个任意数字(与0,1,2不同)。

 cat peptides.txt | while read line do # do something with $line here done 

选项1a: While循环:一次一行:输入重定向

 #!/bin/bash filename='peptides.txt' echo Start while read p; do echo $p done < $filename 

选项1b: while循环:一次一行:
打开文件,从文件描述符(在这种情况下是文件描述符#4)中读取。

 #!/bin/bash filename='peptides.txt' exec 4<$filename echo Start while read -u4 p ; do echo $p done 

选项2: For循环:将文件读入单个变量并解析。
这个语法将根据记号之间的空格解析“行”。 这仍然有效,因为给定的输入文件行是单个工作标记。 如果每行有多个标记,那么这个方法就不能工作。 另外,将整个文件读入单个变量对于大文件来说并不是一个好策略。

 #!/bin/bash filename='peptides.txt' filelines=`cat $filename` echo Start for line in $filelines ; do echo $line done 

这不比其他答案好,但是还有一种方法可以在没有空格的文件中完成工作(请参阅注释)。 我发现我经常需要单行程序来查看文本文件中的列表,而无需使用单独的脚本文件。

 for word in $(cat peptides.txt); do echo $word; done 

这种格式允许我把它放在一个命令行中。 将“echo $ word”部分更改为任何你想要的,你可以发出多个以分号分隔的命令。 以下示例将该文件的内容作为参数写入您可能写入的其他两个脚本中。

 for word in $(cat peptides.txt); do cmd_a.sh $word; cmd_b.py $word; done 

或者,如果你打算使用这个像流编辑器(学习sed),你可以转储输出到另一个文件如下。

 for word in $(cat peptides.txt); do cmd_a.sh $word; cmd_b.py $word; done > outfile.txt 

我已经使用了这些,因为我已经使用了每行一个字创建它们的文本文件。 (见注释)如果你有空间,你不想分裂你的话/线,它会有点丑陋,但同样的命令仍然工作如下:

 OLDIFS=$IFS; IFS=$'\n'; for line in $(cat peptides.txt); do cmd_a.sh $line; cmd_b.py $line; done > outfile.txt; IFS=$OLDIFS 

这只是告诉shell只在新行上拆分,而不是空格,然后将环境返回到之前的状态。 在这一点上,你可能要考虑把它们全部放到shell脚本中,而不是把它们全部压缩成一行。

祝你好运!

使用while循环,如下所示:

 while IFS= read -r line; do echo "$line" done <file 

笔记:

  1. 如果您没有正确设置IFS ,则将失去缩进。

  2. 你应该几乎总是使用阅读-r选项。

  3. 不要用for读行

其他答案没有涵盖的其他一些东西:

从分隔文件中读取

 # ':' is the delimiter here, and there are three fields on each line in the file # IFS set below is restricted to the context of `read`, it doesn't affect any other code while IFS=: read -r field1 field2 field3; do # process the fields # if the line has less than three fields, the missing fields will be set to an empty string # if the line has more than three fields, `field3` will get all the values, including the third field plus the delimiter(s) done < input.txt 

从零分隔的输入读取,例如find ... -print0

 while read -r -d '' line; do # logic # use a second 'read ... <<< "$line"' if we need to tokenize the line done < <(find /path/to/dir -print0) 

一次读取多个文件

 while read -u 3 -r line1 && read -u 4 -r line2; do # process the lines # note that the loop will end when we reach EOF on either of the files, because of the `&&` done 3< input1.txt 4< input2.txt 

将整个文件读入数组(Bash版本早于4)

 while IFS= read -r line; do my_array+=("$line") done < my_file 

将整个文件读入数组(Bash版本4x及更高版本)

 readarray -t my_array < my_file 

要么

 mapfile -t my_array < my_file 

接着

 for line in "${my_array[@]}"; do # process the lines done 
  • readread readarray命令的更多内容

  • 更多关于IFS

  • BashFAQ / 001 – 如何逐行(和/或逐场)读取文件(数据流,变量)?

如果您不希望您的阅读被换行符打破,请使用 –

 #!/bin/bash while IFS='' read -r line || [[ -n "$line" ]]; do echo "$line" done < "$1" 

然后以文件名作为参数运行脚本。

假设你有这个文件:

 $ cat /tmp/test.txt Line 1 Line 2 has leading space Line 3 followed by blank line Line 5 (follows a blank line) and has trailing space Line 6 has no ending CR 

有四个元素会改变许多Bash解决方案读取的文件输出的含义:

  1. 空行4;
  2. 两条线上的空格或空格
  3. 保持个别行的意思(即每一行是记录);
  4. 第6行没有终止一个CR。

如果你想要一行一行的文本文件(包括没有CR的空白行和终止行),则必须使用while循环,并且必须对最后一行进行备用测试。

以下是可能更改文件的方法(与cat返回值相比):

1)失去最后一行和前后空格:

 $ while read -rp; do printf "%s\n" "'$p'"; done </tmp/test.txt 'Line 1' 'Line 2 has leading space' 'Line 3 followed by blank line' '' 'Line 5 (follows a blank line) and has trailing space' 

(if while IFS= read -rp; do printf "%s\n" "'$p'"; done </tmp/test.txt而不是保留前导和尾随空格,但仍然丢失最后一行if它不是以CR终止的)

2)使用cat过程替换将读取整个文件在一个吞咽和失去个别行的意思:

 $ for p in "$(cat /tmp/test.txt)"; do printf "%s\n" "'$p'"; done 'Line 1 Line 2 has leading space Line 3 followed by blank line Line 5 (follows a blank line) and has trailing space Line 6 has no ending CR' 

(如果你从$(cat /tmp/test.txt)删除了,你可以$(cat /tmp/test.txt)阅读文件,而不是一个$(cat /tmp/test.txt)的文件,


逐行读取文件并保留所有间距的最稳健和最简单的方法是:

 $ while IFS= read -r line || [[ -n $line ]]; do printf "'%s'\n" "$line"; done </tmp/test.txt 'Line 1' ' Line 2 has leading space' 'Line 3 followed by blank line' '' 'Line 5 (follows a blank line) and has trailing space ' 'Line 6 has no ending CR' 

如果您想剥离领先和交易空间,请删除IFS=部分:

 $ while read -r line || [[ -n $line ]]; do printf "'%s'\n" "$line"; done </tmp/test.txt 'Line 1' 'Line 2 has leading space' 'Line 3 followed by blank line' '' 'Line 5 (follows a blank line) and has trailing space' 'Line 6 has no ending CR' 

(一个文本文件没有终止\n ,虽然相当普遍,但在POSIX下被认为是坏的,如果你可以指望\n你不需要在while循环中使用|| [[ -n $line ]]

更多在BASH常见问题

 #!/bin/bash # # Change the file name from "test" to desired input file # (The comments in bash are prefixed with #'s) for x in $(cat test.txt) do echo $x done 

这里是我真实的例子,如何循环另一个程序输出的行,检查子字符串,从变量中删除双引号,在循环之外使用该变量。 我想很多人迟早会问这些问题。

 ##Parse FPS from first video stream, drop quotes from fps variable ## streams.stream.0.codec_type="video" ## streams.stream.0.r_frame_rate="24000/1001" ## streams.stream.0.avg_frame_rate="24000/1001" FPS=unknown while read -r line; do if [[ $FPS == "unknown" ]] && [[ $line == *".codec_type=\"video\""* ]]; then echo ParseFPS $line FPS=parse fi if [[ $FPS == "parse" ]] && [[ $line == *".r_frame_rate="* ]]; then echo ParseFPS $line FPS=${line##*=} FPS="${FPS%\"}" FPS="${FPS#\"}" fi done <<< "$(ffprobe -v quiet -print_format flat -show_format -show_streams -i "$input")" if [ "$FPS" == "unknown" ] || [ "$FPS" == "parse" ]; then echo ParseFPS Unknown frame rate fi echo Found $FPS 

在循环之外声明变量,设置值并在循环之外使用它需要完成<<<“$(…)”语法。 应用程序需要在当前控制台的上下文中运行。 命令周围的引用保持输出流的换行符。

子字符串的循环匹配然后读取名称=值对,拆分最后一个字符的右侧部分,删除第一个报价,删除最后一个报价,我们有一个干净的值用于其他地方。

@彼得:这可以为你工作 –

 echo "Start!";for p in $(cat ./pep); do echo $p done 

这将返回输出 –

 Start! RKEKNVQ IPKKLLQK QYFHQLEKMNVK IPKKLLQK GDLSTALEVAIDCYEK QYFHQLEKMNVKIPENIYR RKEKNVQ VLAKHGKLQDAIN ILGFMK LEDVALQILL