Showing posts with label One-liner. Show all posts
Showing posts with label One-liner. Show all posts

Tuesday, January 1, 2013

Perl one-liners: File Extension Frequency

Perl Squirrel was curious about what types of files were on the Linux system.

Using a series of Linux commands to isolate just the extensions of each file, this output is piped to a Perl one-liner that stores in a hash, the file extension as the key and the occurrence count as the value.
The output is sorted by key (or file extension, alphabetical order).

This is all done as a one-liner.

Description and One-Liner Code:
## For all files, ## get basename of each file (meaning drop the directory portion of the string) ## except files starting with dot (meaning don't look at hidden files) ## except files containing comma (no strange files) ## awk uses period as field separator ( -F\. ) ## for lines that have more than 1 field (NF > 1 meaning there is at least one period in the file) ## print the last field $NF (meaning the file extension) ## Display unique extension and number of occurances find . -type f -print | xargs -I {} basename {} | grep -v "^\." | grep -v ',' | awk -F\. 'NF > 1 {print $NF}' | perl -e 'while(<>){chomp;$f{$_}++;}printf("%-40s %12i\n",$_,$f{$_}) foreach sort keys %f;'

Bonus:   Frequency counter for numeric values

To get frequency values for numeric values, you'll want to use a slightly different kind of sort when outputting the contents of the hash keys and values so that the keys display in numerical order not alphabetical order.

This is just an example to show the numeric sort.
In a long directory listing (ls -l), the 7th field contains the day of the month.
Lets get a frequency distribution of the day of the month for each item in the directory.

One-Liner Code:
ls -l | awk '$7 >= 1 {print $7}' | perl -e 'while(<>){chomp;$f{$_}++;}printf("%-40s %12i\n",$_,$f{$_}) foreach sort {$a<=>$b} keys %f;'

Happy New Year!!

Sunday, November 22, 2009

Perl One-liners - An easy way to begin using Perl

A Perl one-liner is a small perl script run from the command-line prompt or a shell script as one command. You can pack a lot of power in a small command. By embedding Perl into a shell script you can enhance the capabilities of your shell script a great deal.

Here are some examples:

Fixed Length Data File - Show records where a column range matches a given string.

General Command:


perl -ne 'print if substr($_, StartCol, Stringlen) =~ "searchstring";'  infile.txt > outfile.txt


Example: Find records where position 488-489 is equal to "CA". Keep in mind that for startcol, the first character of the record begins a position 0.


perl -ne 'print if substr($_, 487, 2) =~ "CA";' infile.txt > outfile.txt

Pipe-Delimited Data File - Show records where a column 2 value equals 2

123|2|Oak
456|4|Cedar
789|2|Willow

perl -ne 'split(/\|/); @line=@_; print if $line[1] == 2;' in.txt > out.txt

Korn shell - Embed Perl for loop inside Korn shell for loop

Example: Loop to count from 1 to 99 (by Odd numbers)
Note: everything between $( ... )  is the perl one-liner

for i in $(perl -e 'for ($i=1; $i<100; $i+=2) {print "$i\n";}')
do
  echo $i
done

Frequency Counter - Pipe Standard Input into this one-liner File of votes with a name on each line - in.txt

cat in.txt | perl -e '%freq;while (<>) {chomp;$freq{$_}++;} printf("%-40s %10i\n", $_, $freq{$_}) foreach sort keys %freq;'

Output:
Al               3
Fred           5
Mary         6