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:
I would like this extract to be replaced by:
I wrote a simple script to do the job that reads myfile.txt and writes the result in out.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:
Please use CODE tags as required by forum rules!
Last edited by RudiC; 02-24-2017 at 03:56 PM..
Reason: Added CODE tags.
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:
I would like this extract to be replaced by:
I wrote a simple script to do the job that reads myfile.txt and Would anybody have a suggestion?
Cheers, Bagvian
Moderator's Comments:
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:
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:
and after processing obtain:
Any suggestion would be welcome.
Cheers,
Bagvian
Moderator's Comments:
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.
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)
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)
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)
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)
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)
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)
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)
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)