RenMcCourtey has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a piece of bash code I'm using to list all running db instances on logon to server. It runs quite slowly, annoyingly delaying logon. I need to grep uncommented lines, get first column and alphabetically sort it, this is core of the command:

grep '^[A-Z]' /var/opt/oracle/oratab | awk -F: '{print $1}' | sort

I was thinking about using perl oneliner but I am not really good with that and I am not even sure if it will run faster. Anyone would be willing to show me how? Is it even a good idea?

/edit: I should show what I started with:

perl -lane 'print if /^[A-Z]/;' /var/opt/oracle/oratab

This is to grep but now I need to add next step (awk) and I'm not sure what's the right syntax.

Replies are listed 'Best First'.
Re: Parse a text file with oneliner
by haukex (Archbishop) on Aug 28, 2017 at 09:55 UTC

    Have a look at perlrun and the -F switch, it's similar to awk, except it splits into the array @F:

    perl -F: -lane 'print $F[0] if /^[A-Z]/' infile

    Now you could pipe that through sort, or with some trickery you can have Perl do that for you too (see the "Eskimo greeting" in perlsecret):

    perl -F: -lane 'push @x, $F[0] if /^[A-Z]/}{print for sort @x' infile

    This should produce the same output as your first grep | awk | sort command.

      OK, thanks a lot, no wonder I couldn't figure it out, that syntax is completely unreadable for me. I can confirm that it is blazing fast in comparison to the original solution, so I will use it for sure. Now I'll just try to figure out how it actually works. I did find -F in the meantime, what I don't understand is how it works together, what actually replaces pipes in the process. I have a lot to learn.

        The switches -lanF: basically add some code to the oneliner provided with -e, this is described in perlrun, but you can also use B::Deparse to turn the oneliner into a script to see it:

        $ perl -MO=Deparse -F: -lane 'push @x, $F[0] if /^[A-Z]/}{print for so +rt @x' BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = readline ARGV)) { chomp $_; our @F = split(/:/, $_, 0); push @x, $F[0] if /^[A-Z]/; } { print $_ foreach (sort @x); }

        Which shows you that the script is:

        1. Setting the input record separator $/ and the output record separator $\ to a newline "\n" (the -l switch), in this case this is mainly useful for not having to add a newline when printing output
        2. Reading the input line-by-line in a while loop (the -n switch), and for each line:
          1. Removing the input record separator (newline) with chomp (the -l switch)
          2. splitting the line on the character : into the array @F (the -aF: switches)
          3. pushing the first element of the array, $F[0], onto the array @x, if the input line $_ matches /^[A-Z]/ (the code from the -e switch)
        3. After that loop,
        4. Loop over the sorted elements of @x, printing each one. (also the -e switch, after the }{)

        Cleaning up the above code gives you this script:

        use warnings; use strict; my @out; while (<>) { chomp; my @fields = split /:/; push @out, $fields[0] if /^[A-Z]/; } print "$_\n" for sort @out;

        Update: Added references to the corresponding switches to the above list and expanded explanation of -l.

      +1 for the eskimo kiss reference - I've been doing perl 1-liners for decades and never knew that trick!

      Mike