LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-26-2011, 01:14 PM   #1
KManepalli
LQ Newbie
 
Registered: Feb 2011
Posts: 4

Rep: Reputation: 0
adding new line after each line in perl


Hi

I need to add extra line after end of each line in perl.
I have file with data like below.
12345
67890
abcde

I need the data to be:
12345

67890

abcde

I am using the below code to achieve this.Here i am opening the file, looping through all the records and adding "\n" at the end of each record. But I am not getting what i wanted. Any suggestions??

***********************
my $thisFILE="$Filepath";

open (my $FILE,"<", $thisFILE) or die "Argh!";
while (my $rec=<$FILE>){
print "Entered into while loop\n";
$rec=$rec."\n";
}
**********************

Thanks
K.
 
Old 04-26-2011, 02:13 PM   #2
adam999
Member
 
Registered: Sep 2006
Posts: 105

Rep: Reputation: 18
my $thisFILE="$Filepath";

open (my $FILE,"<", $thisFILE) or die "Argh!";
my $newrec;
while (my $rec=<$FILE>){
print "Entered into while loop\n";
$newrec .= $rec."\n";
}

print $newrec;
 
Old 04-27-2011, 10:55 AM   #3
KManepalli
LQ Newbie
 
Registered: Feb 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks adam999. This just worked fine.
Thanks for your help !!

K
 
Old 04-27-2011, 11:38 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
This seems like a lot of hard work for something trivial ... are you just looking to improve your Perl skills? Or maybe this is a small part of something bigger?
 
Old 04-27-2011, 03:21 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by grail View Post
This seems like a lot of hard work for something trivial
That's what I first thought, too. However my initial reaction to use either sed or tr both failed me. My best was this:
Code:
perl -i -e 'while(<>){print $_."\n";}' /home/my/file.ext
What is the real solution?
--- rod.
 
Old 04-27-2011, 07:22 PM   #6
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
Assuming you have file.ext including a few text lines to insert empty line after each line is enough to run the following command:

sed -i 's/$/\n/' file.ext
 
Old 04-27-2011, 08:34 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Or
Code:
awk 'ORS=RT"\n"' file
 
Old 04-27-2011, 08:46 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or even
Code:
sed G file
 
1 members found this post helpful.
Old 04-27-2011, 10:01 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Now that is sweet
 
Old 04-28-2011, 08:02 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I knew you guys were good...
--- rod.
 
  


Reply

Tags
perl



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
[SOLVED] adding line from file1 into a line of another file based on maching IDs rossk Programming 6 01-06-2011 12:06 AM
Perl - Adding new line on the beginig of a file shaife720 Programming 4 08-12-2009 06:35 AM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:58 AM.

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