Search code examples
perlawkpasswordsshadow

get password from /etc/shadow — awk vs Perl


I am wondering how to achieve this functionality in Perl

cat /etc/shadow | awk -F ":" '/root/{print $2}'

It would be nice to have your opinion what is better for this kind of task: Perl or awk.


Solution

  • Perl has the switch -F to define a delimiter and turn on autosplitting into the array @F. Thus:

    perl -F: -le 'print $F[1] if /root/' /etc/shadow     # Perl 5.20+
    

    and

    perl -anF: -le 'print $F[1] if /root/' /etc/shadow
    

    will do the same as

    awk -F: '/root/ {print $1}' /etc/shadow
    

    I don't see a real difference between both.