Command line perl replacing sed,awk

While updating svn of framework3 in Backtrack, it failed as it was locked. So I can resolve by removing all the lock files in the folders. I tried to pipe the list of files to rm but failed.

So thought of running command rm <filename>

Now I just ran sh lockfilesremove.txt, which fixed my problem

Here the point is perl is a wonderful tool that can replace sed, awk.
These command does process text in the file like rows, break the row based on delimeter. Which is equivalent to perl script

while(<>){    # $_ holds the row where     @fields = split(/'<delimeter>'/) #script to process these rows and columns}

we can keep the block in the perl inline command using switches –p, -e, -n, -i

-e Allows to program directly in perl without a file

#perl –e ‘print “hello”‘

-n Assume a loop around the program
-p Assume a loop around the program and allows printing
Equivalent to

while (<>) { 
 # your program goes here 
} continue { 
 print or die "-p failed: $!\n"; 
}

Eg: Double space the lines except blank line

Perl cmd line Equivalent Perl script
perl -pe '$_ .= "\n" unless /^$/'
while (<>) { 
 if ($_ !~ /^$/) { 
 $_ .= "\n" 
 } 
} continue { 
 print or die "-p failed: $!\n"; 
}

Eg: Count total number of lines

perl -ne “$.– if /^$/; END{print $.}” input.txt

The above line reduces $. (linecount) if it is blankline.

So I go with

perl –ne '##script for each row of file' filename
Advertisements

About wikihead

A Seeker. Information Security Professional, Pursuing Life with Ayurveda.
This entry was posted in Articles, Notes and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s