How can I fill out blank spaces in a text file with the word from the line above?

I have a large report file (about 20MB) that looks like this:

 586  700006207        8,622.09  896 
                       9,882.82  896 
 777   68607099          900.00  896 
 587  800006207        7,059.22  896 
                         959.02  896 
                         697.87  896 
   7  280667985             .00  899 

On 1st and 2nd columns there are blanks if the values are the same as the line above. I need help with a grep/sed/powershell one-liner to fill out the empty spaces, so that it looks like this:

 586  700006207        8,622.09  896 
 586  700006207        9,882.82  896 
 777   68607099          900.00  896 
 587  800006207        7,059.22  896 
 587  800006207          959.02  896 
 587  800006207          697.87  896 
   7  280667985             .00  899 

Thanks.

Answers


This might work for you:

sed ':a;$!N;/^\(\( [0-9]\+ *[0-9]\+\).*\n\)\( \{15\}\)/{s//\1\2/;ta};P;D' file

From the data you have provided the line always begins with a space. If this is not the case then:

sed ':a;$!N;/^\(\([0-9]\+ *[0-9]\+\).*\n\)\( \{14\}\)/{s//\1\2/;ta};P;D' file

Assuming there are not blank lines and the inter-column delimiters are spaces, the following works (tested in Ubuntu/bash shell, using GNU sed)...

sed -r "/^ {15}/{G; s/^ {15}(.*)\n(.{15}).*/\2\1/};h" "report"

Assuming you need to keep the spacing and the first 2 columns are the first 15 chars:

awk '
    NF==2 {print fill substr($0, 16); next}
    {print; fill = substr($0, 1, 15)}
'

Need Your Help

How should i save my app data in my android app?

java android stream fileinputstream fileoutputstream

I am making my first android app. I want to save an array of strings in one activity and then in second activity i want to show those strings as a list. I am using android platform 2.2 with API 8 S...

Capture SSH Response to variable

c# c#-4.0 ssh ssh.net

I am currently trying to "talk" to a router using SSH commands via C#.