Fms*_*rat 35
虽然所选答案有效,但实际上不需要单独的文件句柄.只需在原始句柄上使用read命令就可以正常运行.
这是两个例子,一个带字符串,一个带文件:
# Create a dummy file
echo -e "1\n2\n3\n4" > testfile.txt
# Loop through and read two lines at a time
while read -r ONE; do
read -r TWO
echo "ONE: $ONE TWO: $TWO"
done < testfile.txt
# Create a dummy variable
STR=$(echo -e "1\n2\n3\n4")
# Loop through and read two lines at a time
while read -r ONE; do
read -r TWO
echo "ONE: $ONE TWO: $TWO"
done <<< "$STR"
Run Code Online (Sandbox Code Playgroud)
将以上脚本作为脚本输出(两个循环的输出相同):
ONE: 1 TWO: 2
ONE: 3 TWO: 4
ONE: 1 TWO: 2
ONE: 3 TWO: 4
Run Code Online (Sandbox Code Playgroud)
gni*_*urf 29
Bash≥4你可以这样使用mapfile:
while mapfile -t -n 10 ary && ((${#ary[@]})); do
printf '%s\n' "${ary[@]}"
printf -- '--- SNIP ---\n'
done < file
Run Code Online (Sandbox Code Playgroud)
那是一次读10行.
Aar*_*lla 23
这比它看起来更难.问题是如何保持文件句柄.
解决方案是创建另一个新文件句柄,其工作方式与stdin(文件句柄0)相同,但是是独立的,然后根据需要从中读取.
#!/bin/bash
# Create dummy input
for i in $(seq 1 10) ; do echo $i >> input-file.txt ; done
# Create new file handle 5
exec 5< input-file.txt
# Now you can use "<&5" to read from this file
while read line1 <&5 ; do
read line2 <&5
read line3 <&5
read line4 <&5
echo "Four lines: $line1 $line2 $line3 $line4"
done
# Close file handle 5
exec 5<&-
Run Code Online (Sandbox Code Playgroud)
Mik*_*der 17
最简单的方法 - 非常不言自明.它类似于@Fmstrat提供的方法,除了第二个read语句在之前do.
while read first_line; read second_line
do
echo "$first_line" "$second_line"
done
Run Code Online (Sandbox Code Playgroud)
您可以通过管道多行输入来使用它:
seq 1 10 | while read first_line; read second_line
do
echo "$first_line" "$second_line"
done
Run Code Online (Sandbox Code Playgroud)
输出:
1 2
3 4
5 6
7 8
9 10
Run Code Online (Sandbox Code Playgroud)
nav*_*ram 14
那要简单得多!:)
cat input-file.txt | xargs -L 10 ./do_something.sh
Run Code Online (Sandbox Code Playgroud)
或者
cat input-file.txt | xargs -L 10 echo
Run Code Online (Sandbox Code Playgroud)
我不认为有一种方法可以在bash中本地执行它,但是可以创建一个方便的功能:
#
# Reads N lines from input, keeping further lines in the input.
#
# Arguments:
# $1: number N of lines to read.
#
# Return code:
# 0 if at least one line was read.
# 1 if input is empty.
#
function readlines () {
local N="$1"
local line
local rc="1"
# Read at most N lines
for i in $(seq 1 $N)
do
# Try reading a single line
read line
if [ $? -eq 0 ]
then
# Output line
echo $line
rc="0"
else
break
fi
done
# Return 1 if no lines where read
return $rc
}
Run Code Online (Sandbox Code Playgroud)
通过这样做,可以轻松地在数据的N行块上循环
while chunk=$(readlines 10)
do
echo "$chunk" | ... # Whatever processing
done
Run Code Online (Sandbox Code Playgroud)
在这个循环中,$ chunk在每次迭代时将包含10个输入行,除了最后一行,它将包含最后一行输入,可能小于10但总是大于0.