在 awk 中使用 printf
Using printf in awk
我有两个文件 $A 和 $B
$A 包含(运算符各不相同,可能是 =、> 或 !=)基本上我有 awk 命令,因为它应该我只想从失败的 $A 添加行
number1 = 460
number2 = 12
number3 > 5
number4 > 20
number5 != 39
number6 != 0
$B 包含
number1 453
number2 12
number3 7
number4 19
number5 39
number6 4
我有一个 awk 命令可以比较两个文件并告诉我数字是否不匹配
output=`awk '
{
getline buf <f2;
split( buf, a, " " );
if( == a[1] && == ">" && +0 > a[2]+0 )
printf( "%s\n", buf );
else if( == a[1] && == "!=" && +0 == a[2]+0 )
printf( "%s\n", buf );
else if( == a[1] && == "=" && +0 != a[2]+0 )
printf( "%s\n", buf );
}
' f2="$B" $A`
echo "$output"
number1 453
number4 19
number5 39
我正在尝试获取此输出:
echo "$output"
This is the line that failed: number1 = 460 #coming from $A
This is the correct number: number1 453 #coming from $B
This is the line that failed: number4 > 20 #coming from $A
This is the correct number: number4 19 #coming from $B
This is the line that failed: number5 != 39 #coming from $A
This is the correct number: number5 39 #coming from $B
因为我更喜欢使用 sed
而不是 awk
,所以有一些东西:
diff -y file1 file2 |
sed 's/^\(.*\) = *\([0-9]\+\)[ \o11]\+|[ \o11]\+/ :: !/p;d'
number1 :: 460 ! = 453
你可以安排:
diff -y file? |
sed 's/^\(.*\) = *\([0-9]\+\)[ \o11]\+|[ \o11]\+ = *\([0-9]\+\)/This is the line that failed: = # coming from file1\nThis is the correct number: = # comming from file2/p;d'
This is the line that failed: number1 = 460 # coming from file1
This is the correct number: number1 = 453 # comming from file2
$ cat tst.awk
NR==FNR {
map[] = +0
next
}
in map {
succ = 0
if ( ( ( == "=" ) && (map[] == ) ) \
|| ( ( == "!=") && (map[] != ) ) \
|| ( ( == ">" ) && (map[] > ) ) \
|| ( ( == ">=") && (map[] >= ) ) \
|| ( ( == "<" ) && (map[] < ) ) \
|| ( ( == "<=") && (map[] <= ) ) \
) {
succ = 1
}
if ( !succ ) {
printf "This is the line that failed: %s #coming from %s\n", [=10=], FILENAME
printf "This is the correct number: %s %s #coming from %s\n", , map[], ARGV[1]
print ""
}
}
$ awk -f tst.awk B A
This is the line that failed: number1 = 460 #coming from A
This is the correct number: number1 453 #coming from B
This is the line that failed: number4 > 20 #coming from A
This is the correct number: number4 19 #coming from B
This is the line that failed: number5 != 39 #coming from A
This is the correct number: number5 39 #coming from B
我有两个文件 $A 和 $B
$A 包含(运算符各不相同,可能是 =、> 或 !=)基本上我有 awk 命令,因为它应该我只想从失败的 $A 添加行
number1 = 460
number2 = 12
number3 > 5
number4 > 20
number5 != 39
number6 != 0
$B 包含
number1 453
number2 12
number3 7
number4 19
number5 39
number6 4
我有一个 awk 命令可以比较两个文件并告诉我数字是否不匹配
output=`awk '
{
getline buf <f2;
split( buf, a, " " );
if( == a[1] && == ">" && +0 > a[2]+0 )
printf( "%s\n", buf );
else if( == a[1] && == "!=" && +0 == a[2]+0 )
printf( "%s\n", buf );
else if( == a[1] && == "=" && +0 != a[2]+0 )
printf( "%s\n", buf );
}
' f2="$B" $A`
echo "$output"
number1 453
number4 19
number5 39
我正在尝试获取此输出:
echo "$output"
This is the line that failed: number1 = 460 #coming from $A
This is the correct number: number1 453 #coming from $B
This is the line that failed: number4 > 20 #coming from $A
This is the correct number: number4 19 #coming from $B
This is the line that failed: number5 != 39 #coming from $A
This is the correct number: number5 39 #coming from $B
因为我更喜欢使用 sed
而不是 awk
,所以有一些东西:
diff -y file1 file2 |
sed 's/^\(.*\) = *\([0-9]\+\)[ \o11]\+|[ \o11]\+/ :: !/p;d'
number1 :: 460 ! = 453
你可以安排:
diff -y file? |
sed 's/^\(.*\) = *\([0-9]\+\)[ \o11]\+|[ \o11]\+ = *\([0-9]\+\)/This is the line that failed: = # coming from file1\nThis is the correct number: = # comming from file2/p;d'
This is the line that failed: number1 = 460 # coming from file1
This is the correct number: number1 = 453 # comming from file2
$ cat tst.awk
NR==FNR {
map[] = +0
next
}
in map {
succ = 0
if ( ( ( == "=" ) && (map[] == ) ) \
|| ( ( == "!=") && (map[] != ) ) \
|| ( ( == ">" ) && (map[] > ) ) \
|| ( ( == ">=") && (map[] >= ) ) \
|| ( ( == "<" ) && (map[] < ) ) \
|| ( ( == "<=") && (map[] <= ) ) \
) {
succ = 1
}
if ( !succ ) {
printf "This is the line that failed: %s #coming from %s\n", [=10=], FILENAME
printf "This is the correct number: %s %s #coming from %s\n", , map[], ARGV[1]
print ""
}
}
$ awk -f tst.awk B A
This is the line that failed: number1 = 460 #coming from A
This is the correct number: number1 453 #coming from B
This is the line that failed: number4 > 20 #coming from A
This is the correct number: number4 19 #coming from B
This is the line that failed: number5 != 39 #coming from A
This is the correct number: number5 39 #coming from B