Reputation: 389
Here is what i am trying to do: I have an out of a following command:
result=`awk /^#.*/{print;getline;print} file1.txt
echo "$result"
Output is:
#first comment
first line
#second comment
second line
#third comment
third line.
if i have to put $result into while loop and capture two lines as one string variable and print it, how can i do it?
Example:
echo "$result" | while read m
do
echo "Value of m is: $m"
done
Output is:
Value of m is:#first comment
Value of m is:first line
Value of m is:#second comment
Value of m is:second line
Value of m is:#third comment
Value of m is:third line.
But the Expected output is:
Value of m is:
#first comment
first line
Value of m is:
#second comment
second line
Value of m is:
#third comment
third line.
Upvotes: 1
Views: 5078
Reputation: 36282
One way using awk
. In each odd line read next one and join them between a newline character.
awk '
FNR % 2 != 0 {
getline line;
result = $0 "\n" line;
print "Value:\n" result;
}
' infile
Assuming content of infile
is:
#first comment
first line
#second comment
second line
#third comment
third line.
Running previous awk
command output will be:
Value:
Value:
#first comment
first line
Value:
#second comment
second line
Value:
#third comment
third line.
Upvotes: 1
Reputation: 360365
while read -r first; read -r second
do
printf '%s\n' 'Value of m is:' "$first" "$second"
done
Or if you need the lines in the variable:
while read -r first; read -r second
do
m="$first"$'\n'"$second"
echo 'Value of m is:'
echo "$m"
done
Upvotes: 4