LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2011, 07:44 PM   #1
jimtony
Member
 
Registered: Jan 2011
Location: Shanghai,China
Distribution: fedora
Posts: 73
Blog Entries: 1

Rep: Reputation: 2
PERL:How to print the lines of a opening file without '\n' in the end?


Hi all,

I found that while reading a line from a file,perl will add a '\n' in the end when printing this line.
For example:
#!/usr/bin/perl

use strict;
use warnings;
open FILE,"anaconda.cfg" or warn $!;
while(<FILE>){
print "$_.....................";
}


the output is as follows:
# Kickstart file automatically generated by anaconda.
.....................%packages
.....................@admin-tools
.....................@base
.....................@core
.....................

But I don't want this output,I want the ".............."in the end of each lines,as follows:
# Kickstart file automatically generated by anaconda......................%packages.....................@admin-tools.....................@base.....................@core.....................

I think the perl will add '\n' in each line from the file,so how to delete the '\n' character?

Last edited by jimtony; 11-21-2011 at 08:20 PM.
 
Old 11-21-2011, 08:19 PM   #2
jimtony
Member
 
Registered: Jan 2011
Location: Shanghai,China
Distribution: fedora
Posts: 73

Original Poster
Blog Entries: 1

Rep: Reputation: 2
I solved this problem,use chomp function to delete the '\n'.
So the codes should be:
#!/usr/bin/perl

use strict;
use warnings;
my $line="sdfsdf";
open FILE,"anaconda.cfg" or warn $!;
while(($line=<FILE>)){
chomp($line);
print "$line.....................";
}

The output is:
# Kickstart file automatically generated by anaconda......................%packages.....................@admin-tools.....................@base.....................@core.....................

Last edited by jimtony; 11-21-2011 at 08:20 PM.
 
Old 11-21-2011, 08:22 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Perl doesn't add a newline, the newline comes from the file. Use chomp to strip it.
Code:
#!/usr/bin/perl

 use strict;
 use warnings;
 open FILE,"anaconda.cfg" or warn $!;
 while(<FILE>){
   chomp;
   print "$_.....................";
 }
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add comma to end of lines in text file Johng Programming 9 08-21-2010 04:15 AM
print the file: between the lines viveksnv Programming 3 02-26-2008 03:06 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
How to Delete nnn lines from the top of a file with out opening it? rhelpm Linux - Newbie 6 12-07-2006 02:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration