从两个输入文件创建udev规则文件

时间:2016-08-23 04:45:59

标签: linux perl awk

我正在寻找为Linux创建Oracle ASM udev规则文件的解决方案。我有两个输入文件。 file1有ASM磁盘要求的信息,file2有磁盘信息。

例如,file1的第2行显示DATA12需要每个128G的3个磁盘(DATA12_01,DATA12_02,DATA12_03)。 file2包含大小的所有磁盘信息。从这两个输入文件我需要创建下面显示的输出文件。

cat file1

Count - size - name
3 - 128 GB DATA12
1 - 128 GB TEMP02
2 - 4 GB ARCH03
2 - 1 GB ARCH04
1 - 3 GB  ORAC01

cat file2

UUID                                        Size
360060e80166ef70000016ef700006700           128.00 GiB
360060e80166ef70000016ef700006701           128.00 GiB
360060e80166ef70000016ef700006702           128.00 GiB
360060e80166ef70000016ef700006703           128.00 GiB
360060e80166ef70000016ef700006730             4.00 GiB
360060e80166ef70000016ef700006731             4.00 GiB
360060e80166ef70000016ef700006733             1.00 GiB
360060e80166ef70000016ef700006734             1.00 GiB
360060e80166ef70000016ef700006735             3.00 GiB

输出文件

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006700", SYMLINK+="udevlinks/DATA12_01"

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006701", SYMLINK+="udevlinks/DATA12_02"

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006702", SYMLINK+="udevlinks/DATA12_03"

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006703", SYMLINK+="udevlinks/TEMP02_01"

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006730", SYMLINK+="udevlinks/ARCH03_01"

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006731", SYMLINK+="udevlinks/ARCH03_02"

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006733", SYMLINK+="udevlinks/ARCH04_01"

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006734", SYMLINK+="udevlinks/ARCH04_02"

ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006735", SYMLINK+="udevlinks/ORAC01_01"

1 个答案:

答案 0 :(得分:0)

这是AWK中的一个:

$ cat > test.awk
BEGIN {FS="([.]| +)"}   # field separator do deal with "." in file2 128.00
FNR==1 {next}           # skip header
NR==FNR {               # read available disks to pool from file1
    for(i=1; i<=$1; i++) 
        a[$5"_"0i]=$3   # name and set the disks into pool
    next} 
{
    for(i in a) {       # look for right sized disk
        if(a[i]==$2) {  # when found, print...
            printf "%s%s%s%s%s", "ACTION==\"add|change\", ENV{DM_NAME}==\"",$1,"\",\"SYMLINK+=\"udevlinks/",i,"\"\n"
            delete a[i] # ... and remove from pool
            break
        }
    } # if no device was found:
    old=len; len=length(a); if(old==len) {print "No device found for ",$0}
}
$ awk -f test.awk file1 file2
ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006700","SYMLINK+="udevlinks/DATA12_01"
ACTION=="add|change", ENV{DM_NAME}=="360060e80166ef70000016ef700006701","SYMLINK+="udevlinks/DATA12_02"
...
No device found for  THIS_IS_AN_EXAMPLE_OF_MISSING_DISK           666.00 GiB

由于使用for(i in a)进行磁盘搜索,因此无法保证从池中读取磁盘的顺序。