blank line deletion in a file using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting blank line deletion in a file using perl
# 1  
Old 03-05-2009
blank line deletion in a file using perl

Dear All,


I am looking for an option in perl using which i could delete empty lines in a file.

Or

the alternative of sed '/^$/d' <filename> in perl. Sed is not working in my perl script Smilie

Pls help me out .

Thanks,
VG
# 2  
Old 03-05-2009
Code:
$ cat input
A|3k|sdgs|211

C|dd|
D||gfgsfg|ff

22|fdfdfsdfsdf
$ perl -ne 'print unless(/^\s*$/);' input
A|3k|sdgs|211
C|dd|
D||gfgsfg|ff
22|fdfdfsdfsdf

# 3  
Old 03-05-2009
Hi Rikxik,

thanks for the reply.

But it's not working for me. let me elaborate:

my script has to get the last line in a file to a perl variable as a string. Now, the file has last line as empty one. Hence i can't directly put it to my string variable as i have to get the last non empty line into the variable.

e.g.,

cat input.txt
Hi there
<blank line>

my variable has to get the last line as:

$myvar=`tail -1 input.txt`;

The problem is that last line is empty and my requirement is the string which is last line in a file and is non empty.



Regards,
Vinod.

Quote:
Originally Posted by rikxik
Code:
$ cat input
A|3k|sdgs|211

C|dd|
D||gfgsfg|ff

22|fdfdfsdfsdf
$ perl -ne 'print unless(/^\s*$/);' input
A|3k|sdgs|211
C|dd|
D||gfgsfg|ff
22|fdfdfsdfsdf

# 4  
Old 03-05-2009
Well its not expected to work since you initially asked for "I am looking for an option in perl using which i could delete empty lines in a file." and now "i have to get the last non empty line into the variable". Make up your mind before you ask. This should work:

Code:
perl -e 'while(<>) {$var=$_ unless(/^\s*$/)}; print $var;' input

# 5  
Old 03-05-2009
Thanks for your help rikxik.


Quote:
Originally Posted by rikxik
Well its not expected to work since you initially asked for "I am looking for an option in perl using which i could delete empty lines in a file." and now "i have to get the last non empty line into the variable". Make up your mind before you ask. This should work:

Code:
perl -e 'while(<>) {$var=$_ unless(/^\s*$/)}; print $var;' input

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

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: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

3. Shell Programming and Scripting

Insert blank line in a file

I have a file with data as below : Heading 1 ------------- Heading 1 data1 Heading 1 data2 Heading 1 data3 Heading 1 data4 Heading 2 ------------- Heading 2 data1 Heading 2 data2 Heading 2 data3 Heading 2 data4 Heading 3 ------------- Heading 3 data1 Heading 3 data2 Heading 3... (2 Replies)
Discussion started by: yoursdivu
2 Replies

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. UNIX for Dummies Questions & Answers

Line deletion help using perl -ne

Hi all, I know the one liner to delete all the lines in a file which matches a pattern i.e perl -i.old -ne 'print unless /pattern/' file Now i need the perl onliner to delete all the lines which doesnt match the pattern. Also what is the difference between perl -i and perl -i.old. Does... (1 Reply)
Discussion started by: lijju.mathew
1 Replies
Login or Register to Ask a Question