dex10
dex10

Reputation: 109

Fill blank lines in a text file with each line of another file

So basically I have a text file with a lot of blank lines. Let's call it time.txt and a part of this file looks like this:

       1          5   20    
       2          5   12    
       1          6   3    
       2          6   4

       1          10   30    
       2          10   21    
       1          11   27    
       2          12   8
       1          11   34    
       2          12   20

       1          10   30    
       2          10   21  

Now, I have another file, called location.txt, which contains the same number of lines as the number of blank lines in time.txt. It looks like this:

   110      -7      5.000              66
   110      -7      5.000              99
   110      -7      5.000              60

And what I want is actually simple: I just want to fill the blank lines in time.txt with each line in location.txt, giving the intended results:

   110      -7      5.000              66
       1          5   20    
       2          5   12    
       1          6   3    
       2          6   4
   110      -7      5.000              99
       1          10   30    
       2          10   21    
       1          11   27    
       2          12   8
       1          11   34    
       2          12   20
   110      -7      5.000              60
       1          10   30    
       2          10   21  

My way to solve this is by reading line by line location.txt, store each line in a variable within the loop, and then using awk to detect empty lines inside time.txt and replace it with stored loop variable. My code looks like this:

time="time.txt"
location="location.txt"
while read -r lines_locs; do
    awk '!NF{$0=$lines_locs}1' $time
done < "$location"

but this only prints out time.txt in my screen with no replacements made. Plus, I had too many lines printed compared to the expected number of lines. I'm sure that I'm missing something and I would be glad if someone can point this out.

Upvotes: 3

Views: 332

Answers (3)

James Brown
James Brown

Reputation: 37454

One in awk using getline:

$ awk -v file="$location" 'NF==0{if((getline < file)<=0)$0=""}1' "$time"

Explained:

$ awk -v file="$location" '    # location file as parameter
NF==0 {                        # NF==0 considers bare space records empty
    if((getline < file)<=0)    # when empty read from another file. if failure
        $0=""                  # reset the record. see comments for discussion
}1' "$time"                    # output

Output:

   110      -7      5.000              66
       1          5   20    
       2          5   12    
       1          6   3    
       2          6   4
   110      -7      5.000              99
       1          10   30    
       2          10   21    
       1          11   27    
       2          12   8
       1          11   34    
       2          12   20
   110      -7      5.000              60
       1          10   30    
       2          10   21  

If file location runs out of records, script prints empty records. See comments for related discussion.

Upvotes: 5

Ed Morton
Ed Morton

Reputation: 204258

Assuming location.txt isn't so massive that it can't fit in memory:

$ awk 'NR==FNR{loc[NR]=$0; next} {print (NF ? $0 : loc[++c])}' location.txt time.txt
   110      -7      5.000              66
       1          5   20
       2          5   12
       1          6   3
       2          6   4
   110      -7      5.000              99
       1          10   30
       2          10   21
       1          11   27
       2          12   8
       1          11   34
       2          12   20
   110      -7      5.000              60
       1          10   30
       2          10   21

Upvotes: 1

Sundeep
Sundeep

Reputation: 23677

If you are okay with GNU sed, you can use

sed -e '/^$/{R '"$location" -e 'd}' "$time"
  • /^$/ matches empty line from $time file
  • R command allows you to read one line at a time from given file, $location in this case
  • d command then deletes the empty line
  • if empty line matches but no more lines to read, then the empty line gets deleted

Use sed -i if you wish to modify $time file in-place

Upvotes: 3

Related Questions