如何使用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

当前回答

我喜欢使用xargs而不是while。xargs功能强大,命令行友好

cat peptides.txt | xargs-I%sh-c“echo%”

使用xargs,还可以使用-t添加详细信息,使用-p添加验证

其他回答

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

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

这将返回输出-

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

这来得很晚,但考虑到这可能会对某人有所帮助,我补充了答案。同样,这可能不是最好的方法。head命令可以与-n参数一起使用,从文件开头读取n行,同样,tail命令可以用于从底部读取。现在,要从文件中提取第n行,我们需要头n行,通过管道将数据传输到管道数据中仅1行的尾部。

   TOTAL_LINES=`wc -l $USER_FILE | cut -d " " -f1 `
   echo $TOTAL_LINES       # To validate total lines in the file

   for (( i=1 ; i <= $TOTAL_LINES; i++ ))
   do
      LINE=`head -n$i $USER_FILE | tail -n1`
      echo $LINE
   done
cat peptides.txt | while read line 
do
   # do something with $line here
done

以及单衬里变体:

cat peptides.txt | while read line; do something_with_$line_here; done

如果没有换行符,这些选项将跳过文件的最后一行。

您可以通过以下方式避免此问题:

cat peptides.txt | while read line || [[ -n $line ]];
do
   # do something with $line here
done

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

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

空白行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常见问题解答