Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 以一定长度从unix批量读取文件_Shell_Unix - Fatal编程技术网

Shell 以一定长度从unix批量读取文件

Shell 以一定长度从unix批量读取文件,shell,unix,Shell,Unix,我有一个名为p.txt的文件。 它包含以下值: 201601 201602 201603 201604 201605 201606 201607 201608 201609 201610 我想一批一批地读这些记录。即 一个变量将有以下三个值 201601 201602 201603 在第一次迭代中。在第二次迭代中,它将有接下来的三行 201604 201605 201606 若数字并没有被3完全除掉,那个么迭代将被除掉+1 在unix中如何实现这一点 到目前为止,我所尝试的: PERD=

我有一个名为p.txt的文件。 它包含以下值:

201601
201602
201603
201604
201605
201606
201607
201608
201609
201610
我想一批一批地读这些记录。即 一个变量将有以下三个值

201601
201602
201603 
在第一次迭代中。在第二次迭代中,它将有接下来的三行

201604
201605
201606
若数字并没有被3完全除掉,那个么迭代将被除掉+1

在unix中如何实现这一点

到目前为止,我所尝试的:

PERD=`cat P.txt`

for perd in `cat PERD_ID.txt`; 
do
    bteq << EOF
    .logon ${conn_string};

    /* Database*/
    DATABASE $ET;

    /* Update NULL LOCL_SEGMNT3_IDNs*/
    INSERT INTO T 
    SELECT  *
    FROM A
    WHERE PERIOD IN ($PERD); 

    .if errorcode != 0 then .exit 5.1;

    .LOGOFF
    .EXIT
EOF

done

试着从这个简单的代码开始。根据您的意愿进行修改:

cat p.txt | while read a; 
do
   read b;
   read c;
   echo $a $b $c;
done

变量a、b、c有3个值。

试着从这个简单的代码开始。根据您的意愿进行修改:

cat p.txt | while read a; 
do
   read b;
   read c;
   echo $a $b $c;
done

变量a、b、c有3个值。

bash
没有任何直接的工具/例程一次读取
n行。因此,
xargs
与使用选项(
-L
)读取3行以及使用while loop over
read
命令的组合,类似于:

# 'count' a local counter variable, incremented in while-loop
# 'file' is sample input file 

$  count=1; xargs -L 3 <file | while read line; do printf "Iteration-->%d\n%s\n" "$(( count++ ))" "${line// /$'\n'}"; done

您可以优化我的解决方案,将每3行输出存储到变量或数组。

bash
没有任何直接的工具/例程一次读取
n行。因此,
xargs
与使用选项(
-L
)读取3行以及使用while loop over
read
命令的组合,类似于:

# 'count' a local counter variable, incremented in while-loop
# 'file' is sample input file 

$  count=1; xargs -L 3 <file | while read line; do printf "Iteration-->%d\n%s\n" "$(( count++ ))" "${line// /$'\n'}"; done

您可以优化我的解决方案,将每3行输出存储到一个变量或数组。

仅为了记录,mapfile/readarray bash方法可能适合

示例:

mapfile -n3 varA<file;echo ${varA[@]} # Get the first three lines from file and store them in a array called varA.
mapfile -s3 -n3 varB<file;echo ${varB[@]} # Skip 3 - get next three lines and store it in another array varB
mapfile -s6 -n3 varC<file;echo ${varC[@]} # Skip 6 - get next three lines, and store it in another array varC

mapfile-n3vara仅就记录而言,mapfile/readarray bash方法是合适的

示例:

mapfile -n3 varA<file;echo ${varA[@]} # Get the first three lines from file and store them in a array called varA.
mapfile -s3 -n3 varB<file;echo ${varB[@]} # Skip 3 - get next three lines and store it in another array varB
mapfile -s6 -n3 varC<file;echo ${varC[@]} # Skip 6 - get next three lines, and store it in another array varC

mapfile-n3 varAit完全可以被3整除时工作正常。但是,如果不是这样,它会给我(201601,,),这是失败的。您希望以3人一组的方式处理这些行。如果最后一组中只有2个或1个项目,则系统将按照您希望的方式进行处理。您可以使用例如“if”语句。当它完全可以被3整除时,它工作正常。但是,如果不是这样,它会给我(201601,,),这是失败的。您希望以3人一组的方式处理这些行。如果最后一组中只有2个或1个项目,则系统将按照您希望的方式进行处理。您可以使用例如“if”语句。
mapfile -O1 var<file