PERL: How to print few new lines in sequence into a file?

Posted on 2007-08-07
Last Modified: 2010-03-05
PERL: How to print few new lines in sequence into a file?
Eg: printf FH "\n\n\n\n";
It will not print 4 newlines for me.
Question by:lenz8as
    LVL 17

    Expert Comment

    This will print 4 new lines:
    print FH "\n\n\n\n";
    LVL 17

    Accepted Solution

    [root@ip-208-109-106-75 ~]# cat test.p

    open (FH, ">temp");
    print FH "hi\n";
    print FH "\n\n\n\n";
    print FH "bye\n";
    close (FH);

    [root@ip-208-109-106-75 ~]# ./test.p
    [root@ip-208-109-106-75 ~]# cat temp

    LVL 39

    Expert Comment

    printf is used to do formatting before printing.  See here for details:

    Unless you need the formatting provided by printf, you should generally use print.


    Author Comment


    I copy exactly your code, but what i got is only signel new line in my temp file.
    Theorytically i believe your script should works and i should see 5 empty line between "hi" and "bye".

    So, is that anything else that may affect the behavior/functionality of this printf basic command?
    LVL 17

    Expert Comment

    You should be using print, not printf
    LVL 84

    Expert Comment

    Since there are no format conversion characters in those strings, the result of a printf should be identical to the result of a print (unless you have $\ set)
    But I agree that print is more efficient and preferable to use when you aren't doing any format conversions.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now