Search code examples
awksedgawk

How to merge two lines in the exact place as if it is pulled up/down using awk or sed


A sample output of my file is given here

! S R Number  :            Class :       ! 
                635035               3     
! Name        :                          ! 
                ADAM BASHA               
+========================================+ 
! Code  ! Description     !       Amount ! 
+========================================+ 
! 2222  ! Tech.Exam Allow !       340.00 ! 
! 3104  ! D A             !    19,524.43 ! 
! 3107  ! H R A           !     3,984.40 ! 
! 4113  ! Transport Allow !       460.00 ! 
!                         :              ! 
  Net Amount Payable  :        24,308.83
! S R Number  :            Class :       ! 
                551820               2     
! Name        :                          ! 
                TOM SMITH              
+========================================+ 
! Code  ! Description     !       Amount ! 
+========================================+ 
! 3104  ! D A             !    19,404.41 ! 
! 3107  ! H R A           !     3,960.60 ! 
! 4113  ! Transport Allow !       460.00 ! 
!                         :              ! 
  Net Amount Payable  :        23,825.01   
! S R Number  :            Class :       ! 
                550044               3     
! Name        :                          ! 
                ROBERT LOUIS           

I want every two lines similar to this format given below to be merged

 ! S R Number  :            Class :       ! 
                635035               3

I want the two lines to be merged as follows

! S R Number  : 635035     Class :   3   !

And the entire result should be as follows :

! S R Number  : 635035     Class :   3   ! 
! Name        : ADAM BASHA               ! 
+========================================+ 
! Code  ! Description     !       Amount ! 
+========================================+ 
! 2222  ! Tech.Exam Allow !       340.00 ! 
! 3104  ! D A             !    19,524.43 ! 
! 3107  ! H R A           !     3,984.40 ! 
! 4113  ! Transport Allow !       460.00 ! 
! Net Amount Payable  :        24,308.83 !
! S R Number  : 551820     Class :   2   ! 
! Name        : TOM SMITH                !
+========================================+ 
! Code  ! Description     !       Amount ! 
+========================================+ 
! 3104  ! D A             !    19,404.41 ! 
! 3107  ! H R A           !     3,960.60 ! 
! 4113  ! Transport Allow !       460.00 ! 
! Net Amount Payable  :        23,825.01 !
! S R Number  : 550044     Class :   3   ! 
! Name        : ROBERT LOUIS             !

The file is too big, so I have posted a sample of it. This output file seems to be splitting some lines into two lines, like the lines containing S R Number, Name and Net Amount Payable. Please provide a solution to this.

The answers already given work fine, only thing is they process all the lines instead of only the lines which are split. Thanks to all.

I have edited my question using a practical example.


Solution

  • $ cat tst.awk
    /^[^[:space:]]/ {
        if (NR>1) { print buf }
        buf = $0
        next
    }
    {
        if (/:/) { sub(/:/," ",buf) }
        while ( match($0,/[^[:space:]]+/) ) {
            buf = substr(buf,1,RSTART-1) substr($0,RSTART,RLENGTH) substr(buf,RSTART+RLENGTH)
            $0 = sprintf("%*s",RSTART+RLENGTH-1,"") substr($0,RSTART+RLENGTH)
        }
    }
    END { print buf }
    

    .

    $ awk -f tst.awk file
    ! S R Number  : 635035     Class :   3   !
    ! Name        : ADAM BASHA               !
    +========================================+
    ! Code  ! Description     !       Amount !
    +========================================+
    ! 2222  ! Tech.Exam Allow !       340.00 !
    ! 3104  ! D A             !    19,524.43 !
    ! 3107  ! H R A           !     3,984.40 !
    ! 4113  ! Transport Allow !       460.00 !
    ! Net Amount Payable  :        24,308.83 !
    ! S R Number  : 551820     Class :   2   !
    ! Name        : TOM SMITH                !
    +========================================+
    ! Code  ! Description     !       Amount !
    +========================================+
    ! 3104  ! D A             !    19,404.41 !
    ! 3107  ! H R A           !     3,960.60 !
    ! 4113  ! Transport Allow !       460.00 !
    ! Net Amount Payable  :        23,825.01 !
    ! S R Number  : 550044     Class :   3   !
    ! Name        : ROBERT LOUIS             !