Search code examples
bashif-statementunixawkecho

How to echo each two lines in one line


I have one txt file with below content:

20210910 ABC  ZZZ            EEE  Rcvd     Staging QV QV P
20210813_20210816_20210818
20210910 XYZ  YYY            EEE  Rcvd     Staging QV QV R
20210813_20210816

There are four rows. How to echo those in two rows. I am not getting how to write if statement in the below code. If the logic is correct please advice :

cat file.txt | while read n
do
    if [ row number odd ]
    then
        column1=`echo $n | awk 'NF' | awk '{print $1}'`
        column2=`echo $n | awk 'NF'| awk '{print $2}'`
        ...till column9
    else
        column10=`echo $n | awk 'NF'| awk '{print $1}'`

        [Printing all columns : 

echo " $column1 " >> ${tmpfn}

echo " $column2 " >> ${tmpfn}

        ...till column10]

    fi
done 

Output:

20210910 ABC  ZZZ            EEE  Rcvd     Staging QV QV P 20210813_20210816_20210818
20210910 XYZ  YYY            EEE  Rcvd     Staging QV QV R 20210813_20210816

Solution

  • No need for an if statement. Just call read twice each time through the loop.

    while read -r line1 && read -r line2
    do
        printf "%s %s" "$line1" "$line2"
    done < file.txt > "${tmpfn}"