LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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, 02: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, 03:13 PM   #2
adam999
Member
 
Registered: Sep 2006
Posts: 95

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, 11: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, 12:38 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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, 04:21 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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, 08:22 PM   #6
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,240

Rep: Reputation: 196Reputation: 196
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, 09:34 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

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

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

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

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


Reply

Tags
perl


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
[SOLVED] adding line from file1 into a line of another file based on maching IDs rossk Programming 6 01-06-2011 01:06 AM
Perl - Adding new line on the beginig of a file shaife720 Programming 4 08-12-2009 07: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 07:35 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 07: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 09:13 AM


All times are GMT -5. The time now is 08:34 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration