一次运行多个输入文件:

时间:2014-07-16 14:53:45

标签: unix awk

我正在运行一个bash脚本来查找从Master.txt到File22_Jun14.txt的范围

sh FindRange.sh

#!/bin/sh
gawk 'BEGIN { 
        FS="," # Field separator
        c=1    # counter
        while ((getline line < ARGV[1]) > 0) { 
            if (line !~ "Start,End,Qty,Date") { # No need for header
                F[c]=line;                   # store line
                split(line,a,",")            # split line
                F2[c]=a[1] ; F3[c]=a[2]        # store the lines range parts
                c++
            }
        }
      } 
FILENAME==ARGV[2] { 
    # Work on second file
    for (i in F) { # For every line scan the first file 
        # if within a range, step out
        if ($1>=F2[i] && $1<=F3[i]) {found=i ; break} 
        # else check next
        else {found=0}
    }  
    # if the above found anything print the line from second file
    # with the relavant line from the first
    if (found>0) { 
        print $0 "," F[found] 
    } 
    # otherwise the not found message
    else { 
        print $0 ",NotFound,NotFound,NotFound,NotFound" 
    } 
}' Master.txt    File22_Jun14_0000.txt   >Op_File33_Jun14.txt

有多个输入文件如下(File22_Jun14_.txt),我将逐个手动更改输入文件名并运行上面的脚本, 是否有任何选项o一次性运行所有File22_Jun14 _ * .txt

File22_Jun14_0000.txt
File22_Jun14_0001.txt
File22_Jun14_0002.txt
File22_Jun14_0003.txt
File22_Jun14_0004.txt
File22_Jun14_0005.txt
File22_Jun14_0006.txt
File22_Jun14_0007.txt
File22_Jun14_0008.txt
File22_Jun14_0009.txt
File22_Jun14_0010.txt

尝试过,sh FindRange.sh Master.txt File22_Jun14_*.txt >Op_File33_Jun14.txt并没有按照预期的方式工作,任何建议......

编辑:检查位于Master.txt范围内的File22_Jun14_0000.txt中的第一个字段 - 如果是,则需要填充所有行中的所有行 File22_Jun14_0000.txt和相应的范围来自Master.txt否则NotFound

Master.txt

StartRange,EndRange,Qty,Date
11,110,100,26.05.14
6001,10000,4000,03.05.14
501,805,305,03.05.14

File22_Jun14_0000.txt

11,abc,22-JUN-12.08:06:03,22-JUN-12.08:06:03,19-Apr-16,1,INR,RO0412,RC03,L7,,31
12,abc,22-JUN-12.08:06:03,22-JUN-12.08:06:03,19-Apr-16,1,INR,RO0412,RC03,L7,,31
905,abc,22-JUN-12.08:06:03,22-JUN-12.08:06:03,19-Apr-16,1,INR,RO0412,RC03,L7,,31
906,abc,30-JUN-12.01:06:49,30-JUN-12.01:06:49,19-Apr-16,1,INR,RO0412,RC03,L7,,29
28,abc,30-JUN-12.01:06:49,30-JUN-12.01:06:49,19-Apr-16,1,INR,RO0412,RC03,L7,,29
100001,def,29-MAY-13.12:05:11,29-MAY-13.12:05:11,15-Feb-17,1350,INR,RO0213,CD,K1,,30
100002,def,29-MAY-13.12:05:11,29-MAY-13.12:05:11,15-Feb-17,1350,INR,RO0213,CD,K1,,30

期望的输出:

11,abc,22-JUN-12.08:06:03,22-JUN-12.08:06:03,19-Apr-16,1,INR,RO0412,RC03,L7,,31,11,110,100,26.05.14
12,abc,22-JUN-12.08:06:03,22-JUN-12.08:06:03,19-Apr-16,1,INR,RO0412,RC03,L7,,31,11,110,100,26.05.14
905,abc,22-JUN-12.08:06:03,22-JUN-12.08:06:03,19-Apr-16,1,INR,RO0412,RC03,L7,,31,NotFound,NotFound,NotFound,NotFound
906,abc,30-JUN-12.01:06:49,30-JUN-12.01:06:49,19-Apr-16,1,INR,RO0412,RC03,L7,,29,NotFound,NotFound,NotFound,NotFound
28,abc,30-JUN-12.01:06:49,30-JUN-12.01:06:49,19-Apr-16,1,INR,RO0412,RC03,L7,,29,11,110,100,26.05.14
506,def,29-MAY-13.12:05:11,29-MAY-13.12:05:11,15-Feb-17,1350,INR,RO0213,CD,K1,,30,501,805,305,03.05.14
100002,def,29-MAY-13.12:05:11,29-MAY-13.12:05:11,15-Feb-17,1350,INR,RO0213,CD,K1,,30,NotFound,NotFound,NotFound,NotFound

1 个答案:

答案 0 :(得分:0)

最好我可以告诉你的脚本应该是:

awk '
BEGIN{ FS="," }
NR==FNR { F[NR]=$0; F2[NR]=$1; F3[NR]=$2; next }
{
    do whatever you do today inside that FILENAME block
}
' Master.txt File22_Jun14_*.txt > Op_File33_Jun14.txt

但我们无法测试解决方案,因为您没有提供任何样本输入和预期输出。