我有2个输入文件,如下所示。
File1
India USA China Russia France England
File2
India
USA
China
Russia
France
England
我需要验证文件2中的所有列是否按相同顺序在文件1中可用。在awk脚本(ksh)中实现此目的的有效方法是什么
我已经编写了一个示例脚本,如下所示。但是,我想知道简单有效的解决方案。
#!/bin/ksh
i=1
while read line
do
val=`cat File1 | cut -d" " -f$i`
echo $val $line
if [ $val = $line ]
then
echo "Matches"
else
echo "Not matches"
fi
i=$((i+1))
done < ./File2
答案 0 :(得分:3)
能否请您尝试一次?仅使用提供的样品进行了测试。
SELECT *
FROM tickets t_1, tickets t_2
where t_1.client = t_2.client
and DATEDIFF(hour, t_1.date_buy, t_2.date_buy) < 24
输出如下。
awk '
{
sub(/[[:space:]]+$/,"")
}
FNR==NR{
a[FNR]=$0
next
}
{
for(i=1;i<=NF;i++){
count=a[i]==$i?++count:count
}
if(count==length(a)){
print "Line number " FNR " whose contents are:" $0 " present in both the files."
}
count=""
}' Input_file2 Input_file1
如果您的字符串的值中可能包含大写字母或小写字母,请添加该解决方案以解决该问题。
Line number 1 whose contents are:India USA China Russia France England present in both the files.
或者在聊天代码中讨论的其他条件。
awk '
{
sub(/[[:space:]]+$/,"")
}
FNR==NR{
a[FNR]=tolower($0)
next
}
{
for(i=1;i<=NF;i++){
count=a[i]==tolower($i)?++count:count
}
if(count==length(a)){
print "Line number " FNR " whose contents are:" $0 " present in both the files."
}
count=""
}' Input_file2 Input_file1
答案 1 :(得分:1)
即使File1
有多行,也可以使用以下awk命令。
awk 'FNR == NR { for (i=1;i<=NF;++i) a[++n] = $i; next }
{ print "Line " FNR ": " ($0 == a[FNR] ? "matches" : "does not match.") "." }' file1 file2
输出:
Line 1: matches.
Line 2: matches.
Line 3: matches.
Line 4: matches.
Line 5: matches.
Line 6: matches.
答案 2 :(得分:0)
$ paste <(tr ' ' '\n' < file1) file2 | awk '{print $0, ($1 == $2 ? "" : "no ") "match"}'
#India India match
#USA USA match
#China China match
#Russia Russia match
#France France match
#England England match
答案 3 :(得分:0)
使用Perl
$ cat vinoth1.txt
India USA China Russia France England
$ cat vinoth2.txt
India
USA
China
Russia
France
England
$ perl -lane ' BEGIN{@k=map{chomp($_);$_} qx(cat vinoth2.txt)} print "Line:$. matches" if join(" ",@k) eq join(" ",@F) ' vinoth1.txt
Line:1 matches