Linux
使用 awk 創建的文件中的範例名稱存在問題
我有一些數據如下:
dir1 |___dir2 |___dir3 |____files |____TGH4_1.tar.gz |____TGH4_2.tar.gz |____IOP5_1.tar.gz |____IOP5_2.tar.gz |____RGH2_btre_1.tar.gz |____RGH2_btre_2.tar.gz |____QWE6_btre_1.tar.gz |____QWE6_btre_2.tar.gz
在文件夾中
dir3
,我創建了一個腳本test.awk
,其程式碼如下:BEGIN { FS="[/_]"; OFS="\t" print "sample", "Second", "Third" } NR%2 { second = $0; next } { print $2, second, $0 }
使用
test.awk
我創建了一個文件:printf '%s\n' $PWD/files/* | awk -f test.awk > test.txt
我得到的輸出
test.txt
如下:sample Second Third dir1 /dir1/dir2/dir3/H0032/files/TGH4_1.tar.gz /dir1/dir2/dir3/H0032/files/TGH4_2.tar.gz dir1 /dir1/dir2/dir3/H0032/files/IOP5_1.tar.gz /dir1/dir2/dir3/H0032/files/IOP5_2.tar.gz dir1 /dir1/dir2/dir3/H0032/files/RGH2_btre_1.tar.gz /dir1/dir2/dir3/H0032/files/RGH2_btre_2.tar.gz dir1 /dir1/dir2/dir3/H0032/files/QWE6_btre_1.tar.gz /dir1/dir2/dir3/H0032/files/QWE6_btre_2.tar.gz
輸出應如下所示:
sample Second Third TGH4 /dir1/dir2/dir3/H0032/files/TGH4_1.tar.gz /dir1/dir2/dir3/H0032/files/TGH4_2.tar.gz IOP5 /dir1/dir2/dir3/H0032/files/IOP5_1.tar.gz /dir1/dir2/dir3/H0032/files/IOP5_2.tar.gz RGH2_btre /dir1/dir2/dir3/H0032/files/RGH2_btre_1.tar.gz /dir1/dir2/dir3/H0032/files/RGH2_btre_2.tar.gz QWE6_btre /dir1/dir2/dir3/H0032/files/QWE6_btre_1.tar.gz /dir1/dir2/dir3/H0032/files/QWE6_btre_2.tar.gz
改變:
BEGIN { FS="[/_]" ... } ... { print $2, second, $0 }
至:
BEGIN { FS="/" ... } ... { sample = $NF sub(/_[^_]*$/,"",sample) print sample, second, $0 }
您的程式碼正在列印
dir1
,因為這是/dir1/dir2/dir3/H0032/files/TGH4_2.tar.gz
給定 FS 設置的第二個欄位,並且您的程式碼顯示print $2...
.