In a file, replace blank line by the last line not blank above


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting In a file, replace blank line by the last line not blank above
# 1  
Old 02-24-2017
In a file, replace blank line by the last line not blank above

Dear All,

In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read.

For example, consider the extract below:
Code:
123
234

543


111
234


198

I would like this extract to be replaced by:
Code:
123
234
234
543
543
543
111
234
234
234
198
198

I wrote a simple script to do the job that reads myfile.txt and writes the result in out.txt:
Code:
MEMVAL=""
LINENUM=0
while read LINE
do
  LINENUM=$(($LINENUM+1));

  # Test if the line is blank
  CURLINE=`echo $LINE | grep -E '^[:space]*$'`
  TEST="$?"
  if [ $TEST == 0 ]; then 
    if [ $LINENUM == 1 ]; then # The first line cannot be blank!!
      exit 1
    else
      # Replace current blank line by memorised info in the output data file
      echo "$MEMVAL" >> out.txt
    fi
  else
    MEMVAL="$LINE"

    # At first init output data file
    if [ $LINENUM == 1 ]; then 
      echo "$LINE" > out.txt
    fi
  fi
done < myfile.txt

This script does the job, but it takes ages.
Also I guess there are far more efficient ways to do this by using either sed of awk (maybe csvfix in my case).

Would anybody have a suggestion?
Cheers, Bagvian


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-24-2017 at 03:56 PM.. Reason: Added CODE tags.
# 2  
Old 02-24-2017
How about
Code:
awk '/^ *$/ {$0 = PRV} {PRV = $0} 1' file
123
234
234
543
543
543
111
234
234
234
198

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-24-2017
Quote:
Originally Posted by bagvian
Dear All,
In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read.
For example, consider the extract below:
Code:
123
234

543


111
234


198

I would like this extract to be replaced by:
Code:
123
234
234
543
543
543
111
234
234
234
198
198

I wrote a simple script to do the job that reads myfile.txt and Would anybody have a suggestion?
Cheers, Bagvian
Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!
Hello bagvian,

Could you please let us know if this is a typo or it is real, your all Output sample looks fine apart from last digit 198 which comes into a space which is before it? All other digits are filling spaces which are coming after them, so could you please confirm on this?
Also if this is not a typo then kindly do let us know what all are conditions where you need this kind of output please.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 02-24-2017
Code:
perl -pe '/^\s+?$/ and $_=$p; $p=$_' myfile.txt > out.txt

This User Gave Thanks to Aia For This Post:
# 5  
Old 02-25-2017
Certainly the *last* blank line is missing in the given input file.
Another solution
Code:
awk 'NF {s=$0} {print s}' file

Code:
sed '/[^ ]/h; g' file

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 02-26-2017
Quote:
Originally Posted by RavinderSingh13
Hello bagvian,

Could you please let us know if this is a typo or it is real, your all Output sample looks fine apart from last digit 198 which comes into a space which is before it? All other digits are filling spaces which are coming after them, so could you please confirm on this?
Also if this is not a typo then kindly do let us know what all are conditions where you need this kind of output please.

Thanks,
R. Singh
Hi R. Singh,

I acknowledge the fact that this point was not clear in my question. Indeed, it is not a typo in my current example (I considered having a blank like after the final: "198").
Cheers,
Bagvian

---------- Post updated at 05:53 PM ---------- Previous update was at 05:29 PM ----------

Dear All,

I have only one word following you answers: "smashing!"
Many thanks to all of you. I tried all the suggested solutions and they all perfectly solve the problem.
(I am not even mentioning the time your methods take compared to my original script. While on a more complex example my script would take a minute, your solutions take a fraction of a second!)

May I make it more complicated?
As I mentioned in the original post, the example I gave comes from a multi-column CSV file using ";" as a separator. The example I gave is the extraction of a given column.

To process this file my idea was to extract columns one by one, apply your suggested solutions to fill up the blank lines and glue the newly generated columns with, for instance: a "paste".

Again, starting from your suggestions, is there a more efficient way to to it?

As an illustration of a CSV file, the idea would be to start for example from:

Code:
123;AZE;
212;;
;;
;ZER;
345;;
;;
;;

and after processing obtain:

Code:
123;AZE;
212;AZE;
212;AZE;
212;ZER;
345;ZER;
345;ZER;
345;ZER;

Any suggestion would be welcome.
Cheers,
Bagvian



Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-26-2017 at 01:08 PM.. Reason: Added CODE tags.
# 7  
Old 02-26-2017
Any chance you came up with an idea of your own based on what you learned in this thread?

EDIT:
Howsoever, try
Code:
awk '$1 {PRV1 = $1}  $2 {PRV2 = $2} {print PRV1, PRV2, _}' FS=";" OFS=";" file
123;AZE;
212;AZE;
212;AZE;
212;ZER;
345;ZER;
345;ZER;
345;ZER;


Last edited by RudiC; 02-26-2017 at 01:34 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Deleting the last non blank line from a file

PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17... (6 Replies)
Discussion started by: swathi reddy1
6 Replies

2. Shell Programming and Scripting

Need help to replace a pattern with a blank line

Need help to replace the line beginning with tcp_sendspace with a blank line. # cat if en0: flags=1e080863,480<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),CHAIN> inet 10.27.53.21 netmask 0xffffff00 broadcast 10.207.52.255 inet... (11 Replies)
Discussion started by: sags007_99
11 Replies

3. Shell Programming and Scripting

replace blank line number

hlow all i need help how can i replace blank number with awk input.txt 300::|355264313178490 301::|358814003239510 302::|358316038113400 303::|357954002633660 304::|354072040694090 305::|356956015214190 306::|352943020525180 307::|359574033836610 308::|381810990023580 so will be like... (4 Replies)
Discussion started by: zvtral
4 Replies

4. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

5. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

6. Shell Programming and Scripting

how to replace a line in file with blank line

Hi I nned cmd to which will help me to replace a line in file with blank line e.g. file1 a b c d e after running cmd I shud get file1 b c d e (5 Replies)
Discussion started by: tarunn.dubeyy
5 Replies

7. Shell Programming and Scripting

how to get the blank line number of a file?

I want to get the blank line number of a file. example: 9000|9000|WW|1|1|SL|472|472|LC|2272|1072|MTY|niceDay 9000|9000|WW|1|1|SL|470|470|MC|1270|1172|MPVT|nice 9000|9000|WW|1|1|SL|472|472|LC|1072|1672|MBD|Sonice 9000|9000|WW|1|1|SL|473|473|LF|1173|1173|MTY|nice666 I want to get... (5 Replies)
Discussion started by: robbiezr
5 Replies

8. Shell Programming and Scripting

Remove last blank line of file

I have a number of files (arranged in directories) which have last line blank, I am trying to synchronize my code with other env and due to this blank lines, all files error out as different although only difference is that of balnk line at end of file. Is there a way I can recursively... (2 Replies)
Discussion started by: ruchimca
2 Replies

9. Shell Programming and Scripting

ignoring blank line in a file

i have a file called Cleaner1.log . This files have some blank lines also.My requirement is that it should ignore the blank lines and give me the lines that contain some data. I m using this logic in a script: below the contents of file : Maximum Time Taken for Processing(Failed) RR... (4 Replies)
Discussion started by: ali560045
4 Replies

10. Shell Programming and Scripting

how to delete a first blank line from the file

I have a file which has the first blank line: sundev22$cat /t1/bin/startallocs /t1/bin/startallocsys 123 sundev22$ Is there a command to remove this first blank line? Thanks for help -A (4 Replies)
Discussion started by: aoussenko
4 Replies
Login or Register to Ask a Question