提问人:Peter Mortensen 提问时间:10/6/2009 最后编辑:Meraj al MaksudPeter Mortensen 更新时间:10/23/2023 访问量:2685304
在 Bash 中循环遍历文件的内容
Looping through the content of a file in 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 是 (from 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
文件肽 .txt 包含:
RKEKNVQ
IPKKLLQK
QYFHQLEKMNVK
IPKKLLQK
GDLSTALEVAIDCYEK
QYFHQLEKMNVKIPENIYR
RKEKNVQ
VLAKHGKLQDAIN
ILGFMK
LEDVALQILL
答:
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
评论
一种方法是:
while read p; do
echo "$p"
done <peptides.txt
正如评论中所指出的,这有修剪前导空格、解释反斜杠序列以及如果缺少终止换行符而跳过最后一行的副作用。如果存在这些问题,您可以执行以下操作:
while IFS="" read -r p || [ -n "$p" ]
do
printf '%s\n' "$p"
done < peptides.txt
在特殊情况下,如果循环正文可能从标准输入中读取,则可以使用不同的文件描述符打开文件:
while read -u 10 p; do
...
done 10<peptides.txt
在这里,10 只是一个任意数字(与 0、1、2 不同)。
评论
while read p || [[ -n $p ]]; do ...
选项 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 已删除。忽略引用选项 2 的评论,因为它作为反模式被正确地删除了(如果您确实需要知道,请参阅编辑)
评论
done < $filename
done 4<$filename
$filename
$1
tail -n +2 myfile.txt | grep 'somepattern' | cut -f3
这并不比其他答案更好,但在没有空格的文件中完成工作的另一种方法(请参阅注释)。我发现我经常需要单行来挖掘文本文件中的列表,而无需使用单独的脚本文件的额外步骤。
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 脚本中,而不是将其全部压缩到一行中。
祝你好运!
评论
for
for l in $(echo '* b c'); do echo "[$l]"; done
*
for
#!/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
评论
使用 while 循环,如下所示:
while IFS= read -r line; do
echo "$line"
done <file
笔记:
如果设置不正确,将丢失缩进。
IFS
评论
Note #2
-u
-u
这是我的真实示例,如何循环另一个程序输出的行,检查子字符串,从变量中删除双引号,在循环外使用该变量。我想很多人迟早会问这些问题。
##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
在循环外声明变量,设置值并在循环外使用它需要<<<“$(...)”语法完成。应用程序需要在当前控制台的上下文中运行。命令两边的引号保留输出流的换行符。
子字符串的循环匹配然后读取 name=value pair,拆分 last = 字符的右侧部分,删除第一个引号,删除最后一个引号,我们有一个干净的值可以在其他地方使用。
评论
@Peter:这可能对你有用——
echo "Start!";for p in $(cat ./pep); do
echo $p
done
这将返回输出-
Start!
RKEKNVQ
IPKKLLQK
QYFHQLEKMNVK
IPKKLLQK
GDLSTALEVAIDCYEK
QYFHQLEKMNVKIPENIYR
RKEKNVQ
VLAKHGKLQDAIN
ILGFMK
LEDVALQILL
评论
假设你有这个文件:
$ 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 结尾。
如果希望文本文件逐行包含空行和终止行,则必须使用 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'
(如果这样做,则保留前导和尾随空格,但如果最后一行未以 CR 结尾,则仍会丢失最后一行)while IFS= read -r p; do printf "%s\n" "'$p'"; done </tmp/test.txt
2)使用带有will的进程替换可以一口气读取整个文件,并失去单个行的含义: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 下被认为是损坏的。如果你能指望尾随,你就不需要在循环中。\n
\n
|| [[ -n $line ]]
while
更多内容请见 BASH 常见问题解答
如果您不希望您的读取被换行符打断,请使用 -
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "$line"
done < "$1"
然后以文件名作为参数运行脚本。
评论
其他答案未涵盖的还有几件事:
从分隔文件中读取
# ':' 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
使用进程替换从另一个命令的输出中读取
while read -r line; do
# process the line
done < <(command ...)
这种方法比因为这里的 while 循环在当前 shell 中运行,而不是像后者那样在子 shell 中运行更好。请参阅相关文章 A variable modified within a while loop is not remembered.command ... | while read -r line; do ...
例如,从空分隔的输入中读取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)
相关阅读:BashFAQ/020 - 如何查找并安全地处理包含换行符、空格或两者兼而有之的文件名?
一次读取多个文件
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
-u
是一个 bash 扩展。对于 POSIX 兼容性,每个调用都类似于 .read -r X <&3
将整个文件读入数组(Bash 版本早至 4)
while read -r line; do
my_array+=("$line")
done < my_file
如果文件以不完整的行结尾(末尾缺少换行符),则:
while read -r line || [[ $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
相关文章:
评论
command < input_filename.txt
input_generating_command | command
command < <(input_generating_command)
这来得相当晚,但考虑到它可能会帮助某人,我正在添加答案。此外,这可能不是最好的方法。 命令可以与参数一起使用,从文件的开头读取 N 行,同样,命令可用于从底部读取。现在,要从文件中获取第 n 行,我们以 n 行为首,将数据从管道数据中仅尾部 1 行。head
-n
tail
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
评论
sed
head
tail
while read -r
nl -ba
read
我喜欢用 . 功能强大且命令行友好xargs
while
xargs
cat peptides.txt | xargs -I % sh -c "echo %"
使用 ,您还可以添加详细程度 with 和 验证xargs
-t
-p
评论
peptides.txt
$(rm -rf ~)
$(rm -rf ~)'$(rm -rf ~)'
这可能是最简单的答案,也许它并非在所有情况下都有效,但它对我来说效果很好:
while read line;do echo "$line";done<peptides.txt
如果需要将空格括在括号中:
while read line;do echo \"$line\";done<peptides.txt
啊,这与获得最多赞成票的答案几乎相同,但它都在一行上。
使用 xargs 的另一种方式
<file_name | xargs -I {} echo {}
echo 可以替换为其他命令或进一步通过管道传输。
评论
cat file_name | xargs -I {} echo {}
对于“猫肽.txt”中的 P 做 回声 “${p}” 做
评论
for 的
行
评论
IFS=$'\n'
的确切含义是什么$IFS
awk