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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-26-2011, 01:14 PM
|
#1
|
|
LQ Newbie
Registered: Feb 2011
Posts: 4
Rep:
|
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.
|
|
|
|
04-26-2011, 02:13 PM
|
#2
|
|
Member
Registered: Sep 2006
Posts: 95
Rep:
|
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;
|
|
|
|
04-27-2011, 10:55 AM
|
#3
|
|
LQ Newbie
Registered: Feb 2011
Posts: 4
Original Poster
Rep:
|
Thanks adam999. This just worked fine.
Thanks for your help !!
K
|
|
|
|
04-27-2011, 11:38 AM
|
#4
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,321
|
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?
|
|
|
|
04-27-2011, 03:21 PM
|
#5
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,269
|
Quote:
Originally Posted by grail
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.
|
|
|
|
04-27-2011, 07:22 PM
|
#6
|
|
Senior Member
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,232
Rep: 
|
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
|
|
|
|
04-27-2011, 08:34 PM
|
#7
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,321
|
Or
Code:
awk 'ORS=RT"\n"' file
|
|
|
|
04-27-2011, 08:46 PM
|
#8
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
|
|
|
1 members found this post helpful.
|
04-27-2011, 10:01 PM
|
#9
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,321
|
Now that is sweet 
|
|
|
|
04-28-2011, 08:02 AM
|
#10
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,269
|
I knew you guys were good...
--- rod.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|