想要打印文件名以及位于目录(pwd)中的文件,所有都是* .gz扩展名文件。
例如,Master.csv.gz
位于/cygdrive/e/Test/Master.csv.gz
和
输入文件位于不同的子目录中,如/cygdrive/f/Jan/Input_Jan.csv.gz
& /cygdrive/f/Feb/Input_Feb.csv.gz
等等..
下面的命令工作正常,没有文件名目录:
awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$2] = $0; next} ($2 in a) {print $0}' <(gzip -dc /cygdrive/e/Test/Master.csv.gz) <(gzip -dc /cygdrive/f/Jan/Input_Jan.csv.gz)
awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$2] = $0; next} ($2 in a) {print $0}' <(gzip -dc /cygdrive/e/Test/Master.csv.gz) <(gzip -dc /cygdrive/f/Feb/Input_Feb.csv.gz)
输出:
Name,Age,Location
abc,20,xxx
def,40,yyy
尝试过以下命令打印文件名目录并完成
awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$2] = $0; next} ($2 in a) {print $0,FILENAME}' <(gzip -dc /cygdrive/e/Test/Master.csv.gz) <(gzip -dc /cygdrive/f/Jan/Input_Jan.csv.gz)
awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$2] = $0; next} ($2 in a) {print $0,FILENAME}' <(gzip -dc /cygdrive/e/Test/Master.csv.gz) <(gzip -dc /cygdrive/f/Feb/Input_Feb.csv.gz)
awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$2] = $0; next} ($2 in a) {print $0,$file}' <(gzip -dc /cygdrive/e/Test/Master.csv.gz) <(gzip -dc /cygdrive/f/Jan/Input_Jan.csv.gz)
awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$2] = $0; next} ($2 in a) {print $0,$file}' <(gzip -dc /cygdrive/e/Test/Master.csv.gz) <(gzip -dc /cygdrive/f/Feb/Input_Feb.csv.gz)
期望的输出:
Name,Age,Location, FileName1,FileName2
abc,20,xxx,/cygdrive/e/Test/Master.csv.gz,/cygdrive/f/Jan/Input_Jan.csv.gz
def,40,yyy,/cygdrive/e/Test/Master.csv.gz,/cygdrive/f/Feb/Input_Feb.csv.gz
寻找你的建议..
答案 0 :(得分:0)
问题是awk没有看到你的输入文件。它会看到进程替换文件。所以你无法从那里得到你想要的名字。
这样的事情应该起作用(以重复自己为代价)。
awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$2] = $0; next} ($2 in a) {print $0,$file}' file=/cygdrive/e/Test/Master.csv.gz <(gzip -dc /cygdrive/e/Test/Master.csv.gz) file=/cygdrive/f/Jan/Input_Jan.csv.gz <(gzip -dc /cygdrive/f/Jan/Input_Jan.csv.gz)
您可以通过仅将文件名传递给awk来玩游戏,然后使用管道命令中的getline
来执行此操作,但我不认为这是值得的。< / p>