a perl command line as substitute for “cut”

These 2 command lines are equivalent, but the 2nd one is the recommend one (it’s only 2 lines, and they should not be broken):

$ perl -ne '@F = split(":"); print "$F[0],\"$F[4]\"\n"' /etc/passwd
$ perl -naF: -e 'print "$F[0],\"$F[4]\"\n"' /etc/passwd

I had used “-a” and “-F” before, but I did not remember (and I wasn’t able to understand “perl –help” properly), that I would still need to give “-n” (or “-p”).

If you want to print the account name together with only the full name from the GCOS field, this is how to achieve it:

$ perl -naF: -e '@GCOS = split(",",$F[4]); print "$F[0],\"$GCOS[0]\"\n"' /etc/passwd

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.