-1

Need while loop that can get each two lines and store in variable.

while read data; do
   echo $data
done

so I need to do something for each block of text which is two lines each.

joanis
  • 10,635
  • 14
  • 30
  • 40
robw
  • 1
  • 2
  • 2
    Can you put your expected input, and expected output? (the title sounds different to your details). – Mr R Mar 14 '21 at 13:24
  • The output is blocks of two lines separated by blank lines. I don't get this site formatting but basically Each block has two lines separated by blank lines. The input and output should be the same but "read line" only grabs the first line of each block of text – robw Mar 14 '21 at 15:46
  • 1
    it should be easy to edit now and put some sample input, and expected output. Just replace the lines `PUT YOUR SAMPLE INPUT HERE` and `PUT YOUR EXPECTED OUTPUT HERE` with your examples (actually you might not see my edit yet .. put three back-tick characters on a line, then your code snippet, then on a new line three more. – Mr R Mar 14 '21 at 20:39
  • Okay, the file reads – robw Mar 14 '21 at 22:16
  • Okay, the file reads ``` some text here some text here some text here some text here `` I am trying to read both lines of each block of txt but. ``` while read line; do ``` Only grabs the top half of the text. I need to read both of those lines for parsing, the parsing part I already have working fine. The blank line in between should be the sperator. – robw Mar 14 '21 at 22:22
  • To me, the above post did not show how I wanted it. I give up – robw Mar 14 '21 at 22:23
  • If the answer I've added doesn't help - please explain what you are after. – Mr R Mar 14 '21 at 22:36

3 Answers3

0

For this input -



some text here
some text here a
some text here 2
some text here 2a


This will merge two lines use while read line.. It's NOT how I'd do it but it does what you said you wanted ...

last=""
while read line; do
  if [ "$last" != "" ]; then
     echo "$last$line"
     last=""
  else
     last=$line
  fi
done
if [ "$last" != "" ]; then
   echo "$last"
fi

This great article (How to merge every two lines into one from the command line?) shows lots of different ways of merging 2 lines ..

Mr R
  • 754
  • 7
  • 19
0

You can read two lines in the while condition:

while read -r first && read -r second
do
   echo "${first} ${second}"
done
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • @DavidC.Rankin I'm answering the question as written. Consider this a double vote to close if OP didn't include information about empty lines in the question. – l0b0 Mar 15 '21 at 02:16
  • No worries, that's why I was just asking. I think we are all shooting at a moving target... – David C. Rankin Mar 15 '21 at 02:20
0

It would help to know what you want to do to the two lines, but you can collect each block of 2 surrounded by empty lines easy enough with awk, e.g.

awk '
    NF==0 { n=0; next } 
    n<2   { arr[++n]=$0 } 
    n==2  { printf "do to: %s & %s\n",arr[1],arr[2]; n=0 }
' file

or as a 1-liner:

awk 'NF==0{n=0;next} n<2{arr[++n]=$0} n==2{printf "do to: %s & %s\n",arr[1],arr[2]; n=0}' file

Where you have 3-rules, the first checks if the line is empty with NF==0, and if so, sets the index n to zero and skips to the next record (line). The second check is n<2 and adds the current line to the array arr. The final rule where n==2 just does whatever you need to the lines contained in arr[1] ane arr[2] and then resets the index n to zero.

Example Input File

Shamelessly borrowed from the other answer and modified (thank you), you could have:

$ cat file

some text here
some text here a

some text here 2
some text here 2a

Example Use/Output

Where each 2-lines separated by whitespace are collected and then output with "do to: " prefixed and the lines joined by " & ", for example purposes only:

$ awk 'NF==0{n=0;next} n<2{arr[++n]=$0} n==2{printf "do to: %s & %s\n",arr[1],arr[2]; n=0}' file
do to: some text here & some text here a
do to: some text here 2 & some text here 2a

Depending on what you need to do to the lines, awk may provide a very efficient solution. (as may sed)

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85