User Tools

Site Tools


perl

This is an old revision of the document!


Table of Contents

Perl

Oneliners

To change only the fifth line, you can add a test checking $., the input line number, then only perform the operation when the test passes:

0@@

To add lines before a certain line, you can add a line (or lines!) before Perl prints $_:

1@@

You can even add a line to the beginning of a file, since the current line prints at the end of the loop:

2@@

To insert a line after one already in the file, use the -n switch. It's just like -p except that it doesn't print $_ at the end of the loop, so you have to do that yourself. In this case, print $_ first, then print the line that you want to add.

3@@

To delete lines, only print the ones that you want.

4@@

… or …

5@@

Using %ENV to pass parameters to a perl one-liner

Used here to clean up a comma-separated list of parameters passed into a shell.<br />

6@@

Add a line after another one in a file using Perl

Use Perl to filter a script (or a load of scripts) and add an extra line when certain pattern match condition exists.<br /> Uses -n switch with print because using -p switch evaluates your condition first and then prints the line. This could be used if you want to print a line before the matched line!

7@@

perl -a

Turns on autosplit mode. Use -F<delim> to specify how to split the elements<br /> Breaks down the input into elements of an automatically assigned array called @F.

8@@

perl -l

When trimming whitespace from your input, the \n is removed also. Using -l adds it back in at the end of processing.<br /> See examples above.

BEGIN and END

Allows you to run code before or after the loop over the lines.<br /> Example, sum the values in the second column of a CSV file…<br /> Replace the 'n' with a 'p' to see the numbers being summed.

9@@

.. operater

Cut chunks out of a file from /start range marker/ .. /end range marker/

10@@

A one-liner web server!

11@@
  • First we accept a socket and fork the server. Then we overload the new socket as a code ref. This code ref takes one argument, another code ref, which is used as a callback.
  • The callback is called once for every line read on the socket. The line is put into $_ and the socket itself is passed in to the callback.
  • Our callback is scanning the line in $_ for an HTTP GET request. If one is found it parses the file name into $1. Then we use $1 to create an new IO::All file object… with a twist. If the file is executable(“-x”), then we create a piped command as our IO::All object. This somewhat approximates CGI support.
  • Whatever the resulting object is, we direct the contents back at our socket which is in $_0.

From: commandlinefu.com

What Perl modules are installed?

As found by typing “perldoc q installed”

12@@

Prepend a line at the beginning of a file

Surprisingly tricky thing to do as a one-liner. Seemingly cannot be done (cross-platform) in ksh in one line.

13@@

Implement a socket client/server in Perl

Scan for emails with attachments and save attachments to files

Install a Perl module from CPAN

14@@
15@@
16@@

Mass update of files using perl inline script

17@@

Perl emulation of dos2unix command

18@@

and then back again with unix2dos

19@@
20@@

Exclude the first few lines of a file

21@@

Delete the last line of a file

22@@
23@@
24@@
25@@

but more efficiently…

26@@
27@@
28@@

Find palindromes in a file

29@@

Reverse all the characters in a file

30@@

Reverse all the characters in a paragraph but keeping the paragraphs in order

31@@

Trim all heading and trailing spaces and compress any intermediate spaces to 1

32@@

Nice one to reformat a document so that all lines are between 50 and 70 characters long. Only breaks on whitespace

33@@

Substitute text in subject placing modified text in result

Variable “subject” remains unchanged

34@@
35@@

Capitalise all words in a file (ensuring all others letters in the words are lower case)

ref: Matz Kindahl

36@@

Translate into Zafir language!

37@@

Read in (include) a configuration file in Perl

Although there are several ways to “include” files into the current program, this method is the simplest.<br /> The problem with using require or include is that the scope is different so any my variables won't be visible<br /> The trick here is the use of the “our” hash.

38@@

Send an email from perl without needing any external modules

but it only works on Unix :(

39@@

Using the function is straightforward. Simply pass it the data in the correct order.

40@@

What "day of the year" (DOY) number was it yesterday?

41@@

What "day of the year" (DOY) number is it today?

42@@

Sort a list

Numerically

43@@

Alphabetically

44@@

Alphabetically (case-insensitive)

45@@

Schwartzian transform

46@@

broken down into (semi)understandable pieces looks like this…

47@@

Use 'map' to apply transformations

Push the list in one side (right) and get it back on the other (left) with some transformation applied.<br /> Inside the code block, you refer to the current element with the traditional $_ variable.

48@@

Use it with join to create clever stuff…

49@@

Difference in hours between two dates

50@@

or, without using any external modules…

51@@

Slurp an external file into a variable

The idea is to read an SQL file into a variable to prepare and execute it

52@@

Search for a (list of) keyword(s) in a file

Could write this in several loops but here is a very neat way. Spotted on Stackoverflow.com

53@@

basically it builds up a regular expression and searches for it.<br /> See reference link for more details.<br /><br /> Can also be done very neatly with grep in shell<br />

54@@

where keywords.txt is a file containing the list of words to search for.

Assign and substitute a value to a variable in one statement

Keep forgetting where the parentheses go so have to write it down…<br /> Have to pre-define the variable $new_var otherwise you will get:

55@@
56@@

Match a regular expression across multiple lines

I always forget this regex and it's sooo useful!

57@@

where backup.ini looks like this:

58@@

Using /s and /m modifiers in a regular expression

Tom Christiansen in his Perl Cookbook demonstrates the difference between these 2 confusing modifiers

59@@

Match regular expression and assign to a variable in a single step

60@@

or split directory and filenames (on Windows or Unix)

61@@

or split a line into bits and assign the bits to variables

62@@

Perl ternary operator

Putting examples here as I keep forgetting the syntax/semantics!<br /> Theoretically it should be:

63@@

which to me means:

64@@

but it's not like that, it's like this:

65@@

and this

66@@

Extract a value from a comma separated list of values in Perl

Suppose you need the 10th column …but only from the lines ending in 'detail'

67@@

or

68@@

and print out the 10th element of @fields

Typical filter program (without needing to slurp)

Keep one of these here to save me looking it up all the time!

69@@
perl.1544130327.txt.gz · Last modified: 2018/12/06 21:05 by 91.177.234.129

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki