How to delete newline with perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete newline with perl
# 1  
Old 10-15-2010
Debian How to delete newline with perl

input:
Code:
donkey

monkey


dance


drink

output should be:
Code:
donkey
monkey
dance
drink

# 2  
Old 10-15-2010
try chomp

---------- Post updated at 09:49 AM ---------- Previous update was at 09:42 AM ----------

Code:
use strict;
use warnings;

open(FILE, "<", "a") or die;

my $data;
while ( $data = <FILE> ) {
    chomp($data);
    print $data, "\n" if ( $data =~ /./ );
}

close(FILE) or warn;


Last edited by matrixmadhan; 10-15-2010 at 01:49 AM..
# 3  
Old 10-15-2010
Any regular expression to solve this?
# 4  
Old 10-15-2010
Code:
$ 
$ 
$ cat -n f24
     1    donkey
     2    
     3    monkey
     4    
     5    
     6    dance
     7    
     8    
     9    drink
$ 
$ 
$ perl -lne 'print if /./' f24
donkey
monkey
dance
drink
$ 
$ 

# 5  
Old 10-15-2010
Please explain this line:
Code:
 $ perl -lne 'print if /./' f24

'print if / ./'
# 6  
Old 10-15-2010
Code:
perl -i -pe 's/^\s+$//g' file


Last edited by k_manimuthu; 10-15-2010 at 01:58 AM.. Reason: update
This User Gave Thanks to k_manimuthu For This Post:
# 7  
Old 10-15-2010
Quote:
Originally Posted by cola
Please explain this line:
Code:
 $ perl -lne 'print if /./' f24

'print if / ./'
. represents any character except a newline ( \n )
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : Delete all files except few

I have a directory like below. Need help with Perl to delete all files under test1 except the one passed as parameters.The parameters will always be the directories under test1 in the format below. EX: dir1/abc.txt,dir2/test.xml,dir3/dfb.txt,dir4/text.xml test1 ... (5 Replies)
Discussion started by: gaurav99
5 Replies

2. Shell Programming and Scripting

awk delete newline after other replacements

Dear All, could you please help me to remove \n characters after all other replacements have been done as in the code below: { #remove punctuation and starting whitespaces gsub("]"," "); $1=$1; } { #print lines containing 'whatever' if ($1=="whatever") {print} #print... (3 Replies)
Discussion started by: shivacoder
3 Replies

3. Shell Programming and Scripting

Delete line - Perl one liner

Hi all, I need a Perl one liner which prints a newline into a .txt file, only where the line starts with "/mediawiki-1.19.0/". It should add the newline to the line before. My problem is, when I try to realize this (with my little knowledge :rolleyes: ) i come to the point where the // are... (4 Replies)
Discussion started by: Mr.Smith
4 Replies

4. Shell Programming and Scripting

Delete lines in an array Using perl

im having an array @check which contains text ..i want to open the array and i have to delete lines starting from a word called "check1" till "check2" for eg:- check1 Use descriptive titles when posting. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt".... (0 Replies)
Discussion started by: rajkrishna89
0 Replies

5. Shell Programming and Scripting

Delete files in directory using perl

Hello All I am implementing my task in Perl and i found an issue. What i want to do is to remove files from the directory which were made 20 days back using Perl script (9 Replies)
Discussion started by: parthmittal2007
9 Replies

6. Shell Programming and Scripting

delete a file using perl

Hi, How to delete a file (if exists) using perl script. I have used following script.. if ( -e $newfile) { open (FILE, ">$newfile") || die "Cannot Open File\n"; print FILE; close(FILE); } But it gives me error "Use of uninitialized value in print". please help. Thanks in... (1 Reply)
Discussion started by: arup1980
1 Replies

7. Shell Programming and Scripting

delete last row in PERL

How to delete last row in the file in PERL. file1 has a.output b.output c.output d.output e.output expected output is a.output b.output c.output d.output (1 Reply)
Discussion started by: adaleru
1 Replies

8. UNIX for Dummies Questions & Answers

Delete the line started with nondigit or newline character

i want to delete the line which is not started with numeric in vim. vim temp.txt Volume in drive D is DATA Volume Serial Number is 8C52-2055 Directory of D:\data\notes 02/16/2010 03:09 PM <DIR> . 02/16/2010 03:09 PM <DIR> .. 09/11/1999 03:03 AM ... (5 Replies)
Discussion started by: Manabhanjan
5 Replies

9. UNIX for Dummies Questions & Answers

delete newline character between html tags

Hi, I have learned some of the Unix commands a way back and not sure of how to code them when needed in certain way, especially sed command. Here is my situation. I have an xml file with several tags. most of the tags start on the same line and end on the same line. However, data for some tags... (8 Replies)
Discussion started by: girish312
8 Replies

10. Shell Programming and Scripting

Perl delete an element from array

Probably I am not seeing it or I am not using the "delete" correctly I had the following codes but it does not work for me #!/bin/perl -w ... @sysFile1 = (a_b, a_c, a_d); @sysFile2 = (a_c, a_e, b_f); foreach $line1 (@sysFile1){ trim(\$line1); (my $tmp1, my $tmp2) = split/_/,... (6 Replies)
Discussion started by: ahtat99
6 Replies
Login or Register to Ask a Question