将两个文件与特定格式所需的字符串和输出进行比较
Compare two files with string and output required for specific format
感谢您在 的回答
@RavinderSingh13
在questions/71080087中,我通过字段分隔符比较了两个文件。
还有一个问题...
我想问个更详细的问题
这是两台服务器各自提供的文件系统列表。
cat volume1.txt (by server1)
/ 80G xfs /dev/mapper/rootvg-lv_root
/boot 1014M xfs /dev/sda2
/boot/efi 500M vfat /dev/sda1
/swlogs 10G xfs /dev/mapper/datavg-lv_swlogs
# cat volume2.txt (by server2)
/ 33G xfs /dev/mapper/rhel-root
/boot/efi 599M vfat /dev/sda1
/boot 1014M xfs /dev/sda2
是否可以输出如下? (行的顺序无关紧要)
volume1.txt volume2.txt
/ 80G xfs /dev/mapper/rootvg-lv_root : [ NotMatch ] : / 33G xfs /dev/mapper/rhel-root
/boot 1014M xfs /dev/sda2 : [ OK ] :
/boot/efi 500M vfat /dev/sda1 : [ NotMatch ] : /boot/efi 599M vfat /dev/sda1
/swlogs 10G xfs /dev/mapper/datavg-lv_swlogs : [ NotExist ] :
我的作品...
FOOD1=`cat ./food1.txt`
FOOD2=`cat ./food2.txt`
echo "$FOOD1" | while read ACCOUNT
do
grep -w $ACCOUNT ./food2.txt >/dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "-----[ OK ] : $ACCOUNT"
else
echo "-----[ WARN ] : $ACCOUNT"
fi
done
如果你不明白我的问题,请告诉我。
使用您显示的示例,请尝试以下 awk
代码。在 GNU awk
.
中编写和测试
awk '
BEGIN{
print ARGV[1]" "ARGV[2]
}
FNR==NR{
arr1[]=[=10=]
next
}
( in arr1){
if([=10=]==arr1[]){
print [=10=] " :[ OK ] : "
}
else if([=10=]!=arr1[]){
print arr1[]" :[ NotMatch ] : "[=10=]
}
arr2[]
next
}
{
print [=10=]" :[ NotExist ] : "
}
END{
for(i in arr1){
if(!(i in arr2)){
print arr1[i]" :[ NotExist ] : "
}
}
}
' volume1.txt volume2.txt
解释:为以上添加详细解释。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here.
print ARGV[1]" "ARGV[2] ##Printing passed Input_file names here.
}
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when food1.txt is being read.
arr1[]=[=11=] ##Creating array named arr1 with index of 1st field and value is [=11=].
next ##next will skip all further statements from here.
}
( in arr1){ ##Checking condition if is present in arr1 then do following.
if([=11=]==arr1[]){ ##Checking condition if whole line is equal to arr1 value.
print " :[ OK ] : " [=11=] ##Printing ok message with current line of food2.txt here.
}
else if([=11=]!=arr1[]){ ##Else(in case whole line is NOT equal to arr1 value) then do following.
print arr1[]" :[ NotMatch ] : "[=11=] ##Printing first field FS value of arr1 followed by NotMatch followed by current line from food2.txt.
}
arr2[] ##Making an entry of current for arr2 array here.
next ##next will skip all further statements from here.
}
{
print [=11=]" :[ NotExist ] : " ##printing current line followed by NotExist statement.
}
END{ ##Starting END block for this program from here.
for(i in arr1){ ##Traversing through arr1 elements here.
if(!(i in arr2)){ ##Checking condition if key i is NOT present in arr2 then do following.
print " :[ NotExist ] : "i FS arr1[i] ##printing NOtExist statements followed by i FS and arr1 value.
}
}
}
' volume1.txt volume2.txt ##Mentioning Input_file names here.
感谢您在
在questions/71080087中,我通过字段分隔符比较了两个文件。
还有一个问题... 我想问个更详细的问题
这是两台服务器各自提供的文件系统列表。
cat volume1.txt (by server1)
/ 80G xfs /dev/mapper/rootvg-lv_root
/boot 1014M xfs /dev/sda2
/boot/efi 500M vfat /dev/sda1
/swlogs 10G xfs /dev/mapper/datavg-lv_swlogs
# cat volume2.txt (by server2)
/ 33G xfs /dev/mapper/rhel-root
/boot/efi 599M vfat /dev/sda1
/boot 1014M xfs /dev/sda2
是否可以输出如下? (行的顺序无关紧要)
volume1.txt volume2.txt
/ 80G xfs /dev/mapper/rootvg-lv_root : [ NotMatch ] : / 33G xfs /dev/mapper/rhel-root
/boot 1014M xfs /dev/sda2 : [ OK ] :
/boot/efi 500M vfat /dev/sda1 : [ NotMatch ] : /boot/efi 599M vfat /dev/sda1
/swlogs 10G xfs /dev/mapper/datavg-lv_swlogs : [ NotExist ] :
我的作品...
FOOD1=`cat ./food1.txt`
FOOD2=`cat ./food2.txt`
echo "$FOOD1" | while read ACCOUNT
do
grep -w $ACCOUNT ./food2.txt >/dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "-----[ OK ] : $ACCOUNT"
else
echo "-----[ WARN ] : $ACCOUNT"
fi
done
如果你不明白我的问题,请告诉我。
使用您显示的示例,请尝试以下 awk
代码。在 GNU awk
.
awk '
BEGIN{
print ARGV[1]" "ARGV[2]
}
FNR==NR{
arr1[]=[=10=]
next
}
( in arr1){
if([=10=]==arr1[]){
print [=10=] " :[ OK ] : "
}
else if([=10=]!=arr1[]){
print arr1[]" :[ NotMatch ] : "[=10=]
}
arr2[]
next
}
{
print [=10=]" :[ NotExist ] : "
}
END{
for(i in arr1){
if(!(i in arr2)){
print arr1[i]" :[ NotExist ] : "
}
}
}
' volume1.txt volume2.txt
解释:为以上添加详细解释。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here.
print ARGV[1]" "ARGV[2] ##Printing passed Input_file names here.
}
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when food1.txt is being read.
arr1[]=[=11=] ##Creating array named arr1 with index of 1st field and value is [=11=].
next ##next will skip all further statements from here.
}
( in arr1){ ##Checking condition if is present in arr1 then do following.
if([=11=]==arr1[]){ ##Checking condition if whole line is equal to arr1 value.
print " :[ OK ] : " [=11=] ##Printing ok message with current line of food2.txt here.
}
else if([=11=]!=arr1[]){ ##Else(in case whole line is NOT equal to arr1 value) then do following.
print arr1[]" :[ NotMatch ] : "[=11=] ##Printing first field FS value of arr1 followed by NotMatch followed by current line from food2.txt.
}
arr2[] ##Making an entry of current for arr2 array here.
next ##next will skip all further statements from here.
}
{
print [=11=]" :[ NotExist ] : " ##printing current line followed by NotExist statement.
}
END{ ##Starting END block for this program from here.
for(i in arr1){ ##Traversing through arr1 elements here.
if(!(i in arr2)){ ##Checking condition if key i is NOT present in arr2 then do following.
print " :[ NotExist ] : "i FS arr1[i] ##printing NOtExist statements followed by i FS and arr1 value.
}
}
}
' volume1.txt volume2.txt ##Mentioning Input_file names here.