We have moved to www.dataGenX.net, Keep Learning with us.

Thursday, March 22, 2012

Perl 1 liners : tips & tricks : Part -2


Here we continues the Perl one liner's magic.......
you can find Perl 1 liners : tips & tricks : Part -1 here.

11. Invert the letter case.

perl -ple 'y/A-Za-z/a-zA-Z/' file_name

This one-liner uses the y operator (also known as troperator). Operators y and tr do string transliteration.Given y/SEARCH/REPLACE/, the operator transliterates all occurrences of the characters found in SEARCH list with the corresponding (position-wise) characters in REPLACE list. Here all caps letters are replaced with small letters and vice-versa.

12. Search and replace in a file.

perl -i -pe 's/1/One/' file_name

Here 1 is replaced with One in the file for 1st occurence only. This is equivalent to the sed command on UNIX systems.

13. Substitute (find and replace) "Dialog" with "Proquest"on lines that matches "IBM".

perl -pe '/IBM/ && s/Dialog/Proquest/' file_name

This one-liner is equivalent to:

while (defined($line = <>)) {
    if ($line =~ /IBM/) {
        $line =~ s/Dialog/Proquest/;
    }
}

14. Print lines that are 80 chars or longer.

perl -ne 'print if length >= 80' file_name

This one-liner prints all lines that are 80 chars or longer.


15. Print only line selected line or print all lines expect particular line.

perl -ne '$. == 13 && print && exit' file_name

The $. special variable stands for "current line number".This 1 liner prints 13 line of file.

perl -ne '$. != 27 && print' file_name

Prints all the lines expect 27.

16. Print all lines between two regexes (including lines that match regex).

perl -ne 'print if /regex1/../regex2/' file_name

Here it prints all the lines between the 2 regex-es.

17. Print all lines that contain a only number.

perl -ne 'print if /^\d+$/' file_name

18. Print all lines that contain only characters.

perl -ne 'print if /^[[:alpha:]]+$/ file_name

19. Print all lines that repeat.

perl -ne 'print if ++$a{$_} == 2' file_name

This one-liner keeps track of the lines it has seen so far and it also keeps the count of how many times it has seen the line before. If it sees the line the 2nd time, it prints it out because ++$a{$_} == 2 is true. If it sees the line more than 2 times, it just does nothing because the count for this line has gone beyond 2 and the result of the print check is false.

20. Print all the lines if strings in it looks like an email address.

perl -ne 'print if/.+@.+\..+/' file_name

Prints all the lines containing email address.


Enjoy the Simplicity.........

Tuesday, March 20, 2012

read command in Unix/Linux

Reads one line from standard input.

The read command reads one line from standard input and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators.

The Variable Name parameter specifies the name of a shell variable that takes the value of one field from the line of input. The first shell variable specified by the Variable Name parameter is assigned the value of the first field, the second shell variable specified by the Variable Name parameter is assigned the value of the second field, and so on, until the last field is reached.

If the line of standard input has more fields than there are corresponding shell variables specified by the Variable Name parameter, the last shell variable specified is given the value of all the remaining fields. If there are fewer fields than shell variables, the remaining shell variables are set to empty strings.

    Note: If you omit the Variable Name parameter, the variable REPLY is used as the default variable name.

-p     Reads input from the output of a process run by the Korn Shell using |& (pipe, ampersand).
 
Note: An end-of-file character with the -p flag causes cleanup for this process so that another can be spawned.
-r     Specifies that the read command treat a \ (backslash) character as part of the input line, not as   a      control character.
-s     Saves the input as a command in the Korn Shell history file.
-u [ n ]     Reads input from the one-digit file descriptor number, n. The file descriptor can be opened with the ksh exec built-in command. The default value of the n is 0, which refers to the keyboard. A value of 2 refers to standard error.


To save a copy of the input line as a command in the history file, type:
read -s line < input_file
If input_file contains "echo hello world," then "echo hello world" will be saved as a command in the history file.

Perl 1 liners : tips & tricks : Part -1


Typically there are cases, where one would not like to create a source file to check small programs. Such things can be tested by running them using "-e" option of the Perl. There are many other command line switches available and some of them have been listed below.

List of Perl's Command Line Switches  Switch Description


-e This option can be used to enter a Perl program directly in the command line. 
-p This option loops around your program and print it. 
-n This option too loops around your program. 
-l Automatically chomps the input line (basically gets rid of newline at the end). 
-a Autosplit mode with -n or -p option. 
-i Edit <> files in place. 
-d Runs the program in debug mode. 
-v Prints version and sub-version of the Perl binary. 
-w Enables the useful warnings. 
-W Enable all warnings. 
-X Disable all warnings. 

Below are some handy one-liners which should be useful to all Perl programmers.

1. Remove all blank lines from a file

perl -i -ne 'print unless /^$/' file_name

The -n flag causes Perl to assume a similar program.

while (<>) {
    print unless $_ =~ /^$/;
}

2. Print the number of empty lines in a file.

perl -lne '$a++ if /^$/; END {print $a}' file_name

The -l argument makes sure a newline gets added afterprinting out this number. The above program is interpreted as below.

while (<>) {
    $a++ if $_ =~ /^$/;
}

END { print $a; print "\n"; }

3. Find the total number of fields (words) in each line.

perl -alne 'print scalar @F' file_name

# or

perl -alne 'print ~~@F' file_name

The -a argument turns on field auto-splitting.This one-liner forces to evaluate the @F in scalar context, which inPerl means "the number of elements in @F." Therefore thisone-liner prints out the number of elements on each line.

4. Find the total number of fields (words) on all lines.

perl -alne '$t += @F; END { print $t }'

Here we just keep adding the number of fields on each line to the variable $t, and at the end we print it out. The result is number of words on all lines.

5. Generate and print the alphabet or numbers.

perl -le 'print a..z'
    #prints abcdefghijklmnopqrstuvwxyz
perl -le 'print 1..20'
    # prints 1234567891011121314151617181920

It actually becomes difficult to interpret the output because there is not field separator in the output. If one wishes to include whitespaces between the output numbers (or alphabets) then it may be assigned to an array variable and then printed, as given below.

perl -le '@F=A..Z; print "@F"'
    #prints A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
perl -le '@F=1..15; print "@F"'
    # prints 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

If one wants the alphabetical series as AA, AB and so on, the following trick can be used

perl -le '@F=A..AZ; print "@F"'
    # prints A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    # AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ

It is also possible to change the field separator in the output by using the $, (the Output Field Separator built-in variable).

perl -le '$, = ","; print a..z'
    # prints a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z

6. Generate Random numbers.

perl -le '$,= "\n"; print map { ("890".."999")[rand 26] } 1..4'
    # prints 890
    #        907
    #        900
    #        914

In each iteration the code chooses a random number from the numbers between 890-999. When map is done iterating, it returns the generated list of numbers and prints it out.

7. Create an array of even or odd numbers.

perl -le '@even = grep {$_ % 2 == 0} 1..100; print "@even"'

The above code generates an array of odd numbers from 1 to99 (as 1, 3, 5, 7, 9, 11, ..., 99). It uses the grepfunction that evaluates the given code $_ % 2 == 1 for eachelement in the given list 1..100 and returns only theelements that had the code evaluate to true.

8. Find the length of the string.

perl -le 'print length "one-liners are great"'
     # prints 20

9. Find the number of elements in an array.

perl -le '@array = ("a".."z"); print ~~@array'
    # prints 26

10. Convert all text to uppercase, lowercase.

perl -nle 'print uc' file_name
    # prints file in uppercase.

perl -nle 'print lc' file_name
    # prints file in uppercase.

We will continue this session in next part. till then...

Enjoy the Simplicity.........