I read the help read
page, but still don\'t quite make sense. Don\'t know which option to use.
How can I read N lines at a time using Bash?
Simplest method - pretty self-explanatory. It is similar to the method provided by @Fmstrat, except the second read
statement is before the do
.
while read first_line; read second_line
do
echo "$first_line" "$second_line"
done
You can use this by piping multiline input to it:
seq 1 10 | while read first_line; read second_line
do
echo "$first_line" "$second_line"
done
output:
1 2
3 4
5 6
7 8
9 10
Here's an alternative way of doing it:
//This will open the file and users can start adding variables.
cat > file
//After finished ctrl + D will close it
cat file|while read line;
do
//do some stuff here
done
This is harder than it looks. The problem is how to keep the file handle.
The solution is to create another, new file handle which works like stdin
(file handle 0) but is independent and then read from that as you need.
#!/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<&-
While the selected answer works, there is really no need for the separate file handle. Just using the read command on the original handle will function fine.
Here are two examples, one with a string, one with a file:
# 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"
Running the above as a script would output (the same output for both loops):
ONE: 1 TWO: 2
ONE: 3 TWO: 4
ONE: 1 TWO: 2
ONE: 3 TWO: 4
That is much more simple! :)
cat input-file.txt | xargs -L 10 ./do_something.sh
or
cat input-file.txt | xargs -L 10 echo
Awk is a funny way for this case:
~$ cat test.txt
tom
tom@gmail.com
jack
jack@gmail.com
marry
marry@gmail.com
gogo
gogo@gmail.com
~$ cat test.txt | awk 'BEGIN{c=1}{a=c;if(a==2){print b" "$0;c=1} if(a==1){b=$0;c=2}}'
tom tom@gmail.com
jack jack@gmail.com
marry marry@gmail.com
gogo gogo@gmail.com