有 Perl 替代剪切和粘贴 shell 命令吗?

发布于 2024-08-10 13:13:54 字数 180 浏览 7 评论 0原文

我曾经看到过一个用于剪切和粘贴 Linux 命令的快速但肮脏的 Perl 实现。这就像 perl '打印“$F1”'文件名 替换 cut -f1 filename 命令

有人能告诉我 Perl 的方式是怎样的吗?具体来说,我很感兴趣,因为与剪切/粘贴不同,它在 Windows 环境中也能起到同样的作用。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

为你鎻心 2024-08-17 13:13:54

您了解 Perl 电动工具吗?它们是您最喜欢的 UNIX 命令的实现,但是是用 Perl 完成的。如果您有 Perl,您就可以使用您最喜欢的命令,而无需在命令行上进行杂技操作。 PPT 具有剪切粘贴 功能。

尽管许多人向您指出了一些 perl 开关,但他们忘记链接到 perlrun< /a> 这解释了所有这些。

Do you know about the Perl Power Tools? They are implementations of your favorite unix commands, but done in Perl. If you have Perl, you can have your favorite commands without acrobatics on the command line. PPT has both cut and paste.

Although many people pointed you to some perl switches, they forget to link to perlrun which explains them all.

躲猫猫 2024-08-17 13:13:54

cut

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

要提供自定义输入分隔符,

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

要更改输出分隔符,

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

要在使用时使用 grep

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

进行粘贴

不太容易。

#!/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";
}

cut

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

To give a custom input separator,

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

To change the output separator,

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

To grep while you're at it,

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

paste

Not quite as 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";
}
情感失落者 2024-08-17 13:13:54

对于剪切:我认为您正在寻找 perl -ane 解决方案(-e 执行以下代码,-an 应用 @F = split (/\t/,$line) 到文件中的每一行)。
因此,类似于:

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

与:

cut -f1,2 file

对于粘贴,我不确定如何执行此操作,但我认为 perl -ane 可以将多个文件作为输入。

For cut: I think you are looking for the perl -ane solution (the -e executes the following code, the -an applies @F = split (/\t/,$line) to every line in the file).
So something like:

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

which is identical to:

cut -f1,2 file

For paste, I'm not sure how you can do this, but I think the perl -ane can take multiple files as input.

梦忆晨望 2024-08-17 13:13:54

Perl 相当于 cut -c1-2

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

Perl equivalent of cut -c1-2

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文