题 循环遍历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=/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
942
2017-10-05 17:52
起源
答案:
一种方法是:
while read p; do
echo $p
done <peptides.txt
特殊情况下,如果 循环体可以从标准输入读取,您可以使用不同的文件描述符打开文件:
while read -u 10 p; do
...
done 10<peptides.txt
这里,10只是一个任意数字(不同于0,1,2)。
1520
2017-10-05 18:00
cat peptides.txt | while read line
do
# do something with $line here
done
293
2017-10-05 17:54
选项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
107
2017-10-05 18:18
这并不比其他答案好,但是在没有空格的文件中完成工作的另一种方法(参见注释)。我发现我经常需要单行来挖掘文本文件中的列表,而无需使用单独的脚本文件。
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脚本中,而不是将其全部压缩到一行中。
祝你好运!
59
2017-10-04 13:30
使用while循环,如下所示:
while IFS= read -r line; do
echo "$line"
done <file
笔记:
如果你没有设置 IFS
没错,你会失去缩进。
您应该几乎总是将-r选项与read一起使用。
不要读行 for
36
2018-06-09 15:09
还有一些其他答案没有涉及的事情:
从分隔文件中读取
# ':' 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 ...)
这种方法比 command ... | while read -r line; do ...
因为while循环在当前shell中运行而不是在后者的情况下运行子shell。查看相关文章 在while循环内修改的变量不会被记住。
例如,从空分隔输入读取 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
基于 @ chepner的 回答 这里:
-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
相关文章:
29
2018-01-14 03:30
如果您不希望读取被换行符破坏,请使用 -
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "$line"
done < "$1"
然后以文件名作为参数运行脚本。
8
2018-03-08 16:10
假设你有这个文件:
$ 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'
(如果你这样做 while 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'
(没有终止的文本文件 \n
虽然相当常见,但在POSIX下被认为是破碎的。如果你可以依靠尾随 \n
你不需要 || [[ -n $line ]]
在里面 while
循环。)
更多的是 BASH FAQ
8
2018-02-03 19:15
#!/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
4
2017-11-14 14:23
这是我的真实例子如何循环另一个程序输出的行,检查子串,从变量中删除双引号,在循环外使用该变量。我想很多人迟早会问这些问题。
##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
在循环之外声明变量,设置值并在循环之外使用它 完成<<<“$(...)” 句法。应用程序需要在当前控制台的上下文中运行。命令周围的引号保持输出流的换行符。
然后读取子串的循环匹配 名称=值 对,分裂最后的右侧部分 = 字符,丢弃第一个报价,删除最后一个报价,我们有一个干净的值,以便在别处使用。
2
2018-06-30 08:15