Skip to content
Advertisement

How to merge two rows in a same row from a text file in linux shell script

From Nagios I downloaded its html file by using wget command and then I converted that htmlfile to Textfile by using following code:

html2text -width 180 file.html >a.txt

then I cut the first 10 lines becoz I dont want that text and I got below textfile output

awk 'NR > 10 { print }'a.txt > b.txt

I have to merge two rows into single row not for all lines only for particular output from b.txt file. Note: the text file contains N number of lines

Here the b.txt File Output:

                      DISK OK - free space:          CRITICAL
01-08-2018 07:05:05   Service Required     Critical  CPU:loadaverage 6.0%                    

01-08-2018 07:10:25   Service Alert        Critical  memoryUsage

                       DISK OK - free space: 
02-08-2018 01:05:2018  Service Alert       Warning   memoryUsage

                                                      CRITICAl:outstanding alert attention 
02-08-2018 02:05:2018  Service Alert        Critical  required 

Expected output:

01-08-2018 07:05:05   DISK OK - free space:Service Required  Critical    CRITICALservice requiredCPU:loadaverage 6.0%

01-08-2018 07:10:25   Service Alert                          Critical    memoryUsage

02-08-201801:05:2018  DISK OK - free space:Service Alert     Warning     memoryUsage

02-08-2018 02:05:2018 Service Alert                         Critical     CRITICAL:outstanding alert attention required

Thanks in advance

Advertisement

Answer

You can use awk to merge lines together:

awk '
  /^ +/{                     # For lines starting with spaces,
    gsub(/^ +/," ");         # Replace these multiple spaces with only one
    a=a $0;                  # Store the line into the variable a
    next                     # Continue with the next line
  }
  {                          # For lines not starting with spaces
    $3=$3 a;                 # Append the variable a to the third element
    a=""                     # Clear variable a
  }
  1                          # Print the current line
' b.txt
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement