如何使用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—版本输出:

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 line;do echo "$line";done<peptides.txt

如果需要用括号括起空格:

while read line;do echo \"$line\";done<peptides.txt

啊,这和投票最多的答案差不多,但都在一行。

其他回答

@彼得:这可能对你有用-

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

这将返回输出-

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

使用while循环,如下所示:

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

笔记:

如果不正确设置IFS,将丢失缩进。您几乎应该始终在read中使用-r选项。不读取带有for的行

假设您有此文件:

$ 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解决方案读取的文件输出的含义:

空白行4;两行上的前导或尾随空格;保持各行的含义(即,每行都是一条记录);线路6未以CR终止。

如果您希望文本文件一行一行地包含空白行和没有CR的终止行,则必须使用while循环,并且必须对最后一行进行替换测试。

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

1) 丢失最后一行以及前导空格和尾随空格:

$ while read -r p; 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'

(如果在IFS=read-r p;do printf“%s\n”“'$p'”;done</tmp/test.txt时执行,则保留前导空格和尾随空格,但如果最后一行未以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)中删除“”,您将逐字逐句地阅读文件,而不是一饮而尽。也可能不是预期的内容…)


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

$ 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'

(一个没有终止符的文本文件,虽然在POSIX下很常见,但被认为是已损坏的。如果您可以指望结尾符,则在while循环中不需要||[[-n$line]]。)

更多关于BASH常见问题解答

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

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

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

选项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