Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print blank lines

Status
Not open for further replies.

Tdlearner

Technical User
Jun 17, 2004
13
US
Hi Guys,

I am trying to print blank lines on using perl command line.

cat blank.dat
Hello
### Blank line commented for visibility ####
Second Blank Line
### Blank line commented for visibility ####

Command 1 :

perl -npe 'if ($_ == m/^$/) {print "This is a blank line \n"}' blank.dat

Output :
This is a blank line
Hello
### Blank line commented for visibility ####
This is a blank line
Second Blank Line
### Blank line commented for visibility ####


Command 2 :

perl -npe 'if (/^$/) {print "This is a blank line \n"}' blank.dat

Output :
Hello
This is a blank line
### Blank line commented for visibility ####
Second Blank Line
This is a blank line


I don't understand the order of print statements using command 1 .

Command 2 prints everything in the file , plus the print statements .


Shouldn't both the above should print just print statements ?

If I write a small perl program using while loop , it just works fine. But commandline behavior is different .

I am sure I am missing some important subtle points being a beginner in perl


thanks,
 
Because you're explicitly print out information, you don't need or want the -p option to perl. Also don't use == for string equality test, use eq instead. But the easiest solution is to just use a regular expression.

Code:
perl -ne 'print "blank at line # $.\n" if /^$/' blank.dat

You might have to change your cmd prompt quoting style if you're on a windows system. And don't forget to remove the comment from your blank line. Made that mistake temporarily when using your test data.

- Miller
 
Code:
perl -pe 'if (/^\s*$/) {print "Line number $. is a blank line\n"}'  blank.dat

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks guys,

Miller, I tried perl -n and it does the job but

BUt why perl -p is also printing non-matching lines ?


The following works but it also prints non-matching lines

perl -pe 'if ($_ =~ /^$/) {$_ = "This is a blank line \n" }' blank.dat

Output :
Hello
This is a blank line
Second Blank Line
This is a blank line


While -n option only prints matching line , -p option prints everything.

Is this default behaviour, to my undestanding , -p and -n options are same except the print part.

thanks
 
Yes, this is just documented behavior of the perl command line. -n simply loops over ARGV, while -p loops but also prints each line.

Given you're explicitly printing output, you probably want -n like in my example.

Code:
>perl --help

Usage: perl.exe [switches] [--] [programfile] [arguments]
  -e program      one line of program (several -e's allowed, omit programfile)
  -n              assume "while (<>) { ... }" loop around program
  -p              assume loop like -n but print line also, like sed

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top