Fork me on GitHub

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

shell、perl编程

shell部分
perl部分

Shell

shell创建空文件

> filename / >> filename
touch filename
echo "" > filename (替换) / echo "" >> filename (补充)

perl

#!/usr/bin/env perl

$infile = shift;  # 获取数组的第一个元素,若未提供则获取 @ARGV 的值
$outfile = shift;
$num_per = shift;

print "参数" , $infile , $outfile , $num_per;   # 打印语句
open IN ,"<:encoding(utf8)" , $infile or die $!;   # < 读取模式,以utf8解码
$cc = 0;  # 定义变量
$file_num = 1;
$line = "";
open OUT , ">:encoding(utf8)", $outfile."/$infile-$file_num" or die $!; # > 写模式

while( $line = <IN> ) # 遍历读取对象内容
{
        $cc++;
        if ($cc%$num_per==1 ) {
                if($cc > $num_per){
                        close OUT;
                        $file_num++;
                        open OUT , ">:encoding(utf8)", $outfile."/$infile-$file_num" or die $!;
                }
        }
        # if ($file_num > 100){
        #       last;     #  last跳出循环,next跳过当前次循环
        # }
        print OUT $line;     # 将读取内容写进新文件中
}

close OUT;  # 关闭文件对象
close IN;

shift文档详解

shift ARRAY
shift   Shifts the first value of the array off and returns it,
        shortening the array by 1 and moving everything down. If there
        are no elements in the array, returns the undefined value. If
        ARRAY is omitted, shifts the @_ array within the lexical scope
        of subroutines and formats, and the @ARGV array outside a
        subroutine and also within the lexical scopes established by the
        "eval STRING", "BEGIN {}", "INIT {}", "CHECK {}", "UNITCHECK {}"
        and "END {}" constructs.

        See also "unshift", "push", and "pop". "shift" and "unshift" do
        the same thing to the left end of an array that "pop" and "push"
        do to the right end.
posted @ 2022-01-13 00:15  365/24/60  阅读(64)  评论(0编辑  收藏  举报