從文件,我想用awk來提取特定的值:AWK,提取細節值從文件
輸入文件:
EVS 58 (EVSTEST1):
Export name: /EXPORT1
Export path: /opt/export1
File system label: fs_test1
File system size: 18.7 GB File system free space: 16.5 GB
File system state:
formatted = Yes
mounted = Yes
failed = No
thin provisioned = No
Access snapshots: No
Display snapshots: No
Read Caching: Disabled Disaster recovery setting:
Recovered = No Transfer setting = Use file system default
Export configuration:
EVS 59 (EVSNEXT):
Export name: /next
Export path: /next
File system label: fs_next
File system size: 9.75 GB File system free space: 2.28 GB
File system state:
formatted = Yes
mounted = Yes
failed = No
thin provisioned = No
Access snapshots: No
Display snapshots: No
Read Caching: Disabled Disaster recovery setting:
Recovered = No Transfer setting = Use file system default
Export configuration:
10.26.xx.xx(rw,norootsquash)
10.26.xx.xx(rw,norootsquash)
10.26.xx.xx(rw,norootsquash)
我需要提取行: EVS & EVS ID,導出名稱,導出路徑,文件系統標籤和導出配置(如果存在)。
得到以下的輸出:
58;EVSTEST1;/EXPORT1;/opt/export1;fs_test1;-
59;EVSNEXT;/next;/next;fs_next;10.26.xx.xx(rw,norootsquash);10.26.xx.xx(rw,norootsquash);10.26.xx.xx(rw,norootsquash)
但是,隨着我的腳本波紋管,我不能讓所有的「導出配置」線。我只得到了魚。我試圖與for循環,如果一些沒有成功..
#!/bin/bash
LST_NFS=$(cat lst_nfs.txt | awk -F= '\
($1~/^EVS/) { gsub(/\(/,"");gsub(/\)/,"") ; gsub(/:/,""); split($0,parts," ") ;evsid=parts[2];evsname=parts[3] }
($1~/ Export name/) { split($0,parts," ") ; name=parts[3] }
($1~/ Export path/) { split($0,parts," ") ; path=parts[3] }
($1~/ File system label/) { split($0,parts," ") ; fs=parts[4] }
($1~/ Export configuration:/) { getline;
if ($1~ /^[0-9]+/) { export1=$1; getline;
printf "%s;%s;%s;%s;%s;%s\n",evsid,evsname,name,path,fs,export1}
else { export1="-";
printf "%s;%s;%s;%s;%s;%s\n",evsid,evsname,name,path,fs,export1 }
}')
echo -e "$LST_NFS"
我的腳本結果:
57;EVSTEST1;/EXPORT1;/opt/export1;fs_test1;-
59;EVSNEXT;/next;/next;fs_next;10.26.xx.xx(rw,norootsquash)
你的幫助非常感謝。
問候,
格雷格
非常感謝埃德! – Indi59