Is Perl a replacement for the cut and paste shell commands?

I once saw a quick and dirty Perl implementation for linux cut and paste commands. It was something like perl 'print "$F1"' filename replacing the cut -f1 filename command

Can someone tell me how it was in Perl? In particular, I'm interested because this, unlike cut / paste, will work the same in Windows environments.

+3
source share
4 answers

cut

perl -alpe'$_=$F[0]'
perl -alpe'$_="@F[1..3]"'

To create a custom input separator,

perl -F: -alpe'$_=$F[0]'

To change the output delimiter,

perl -F: -alpe'$"=":";$_="@F[1..3]"'

In grepwhile you are on it,

perl -alne'print$F[0]if/blah/'

paste

Not so easy.

#!/usr/bin/perl
for (@ARGV ? @ARGV : qw(-)) {
    if ($_ eq '-') {push @files, *STDIN}
    else {open $files[@files], '<', $_}
}
while (grep defined, (@lines = map scalar <$_>, @files)) {
    chomp @lines;
    print join("\t", @lines), "\n";
}
+3
source

Perl Power Tools? unix, Perl. Perl, . PPT cut, paste.

perl, perlrun, .

+4

: , perl -ane (-e , -an @F = split (/\t/,$line) ). - :

perl -ane 'print "$F[0]\t$F[1]\n"' file

:

cut -f1,2 file

, , , , perl -ane .

+1

Perl- cut -c1-2

perl -alne 'print substr ($ _, 0,2)'

0
source

Source: https://habr.com/ru/post/1722706/


All Articles