将第一个文件的每一行的逗号前的第一个值与第二个文件逐行匹配
Match 1st value before comma of each line from first file with second file line by line
我的第一个文件 -
#cat 123
tom,123
jack,222
rock
google,908
mohan,323
ram,789
我的第二个文件-
#cat www
vicky,tom,home
google,monk,uber
dhoom,monk,uber
ram,monk,uber
rock,monk,uber
jack,monk,uber
期望的输出 -
#cat match_output.txt
tom,123,vicky,tom,home
rock,rock,monk,uber
jack,222,jack,monk,uber
google,908,google,monk,uber
ram,789,ram,monk,uber
目前,我只得到这个 -
#cat match_output.txt
rock,monk,uber
我的脚本-
#!/bin/bash
# because File2 is bigger, it gets the main loop.
# read each line of File2
>/var/lib/jenkins/Jack/match_output.txt
while IFS=, read -r string; do
# read each line of File1.txt
while IFS=, read -r string2; do
# check match, and write if needed.
if [[ $string == *"$string2"* ]]; then
echo $string >> match_output.txt
echo "wrote "$string" to match_output.txt..."
fi
done < /var/lib/jenkins/Jack/123
done < /var/lib/jenkins/Jack/www
无法读取每行逗号前第一个文件的第一个值并逐行与第二个文件匹配并在新文件中打印输出....
要获取逗号前的第一个值,可以使用cut
命令。
用下面的代码
if [[ $string == *"$string2"* ]]; then
echo $string >> match_output.txt
echo "wrote "$string" to match_output.txt..."
fi
你比较完整的行。如果你只想比较$string
和$string2
的第一个值(逗号前),你需要调整这个比较。
string2FirstValue=`echo "$string2" |cut -d',' -f1`
if [[ $string == *"$string2FirstValue"* ]]; then
echo $string2,$string >> match_output.txt
fi
更安全高效的方法是使用awk
:
awk '
BEGIN {FS=OFS=","}
FNR == NR {
map[] = [=10=]
next
}
{
for (i=1; i<=NF; ++i)
if ($i in map) {
print map[$i], [=10=]
next
}
}' 123 www
tom,123,vicky,tom,home
google,908,google,monk,uber
ram,789,ram,monk,uber
rock,rock,monk,uber
jack,222,jack,monk,uber
我的第一个文件 -
#cat 123
tom,123
jack,222
rock
google,908
mohan,323
ram,789
我的第二个文件-
#cat www
vicky,tom,home
google,monk,uber
dhoom,monk,uber
ram,monk,uber
rock,monk,uber
jack,monk,uber
期望的输出 -
#cat match_output.txt
tom,123,vicky,tom,home
rock,rock,monk,uber
jack,222,jack,monk,uber
google,908,google,monk,uber
ram,789,ram,monk,uber
目前,我只得到这个 -
#cat match_output.txt
rock,monk,uber
我的脚本-
#!/bin/bash
# because File2 is bigger, it gets the main loop.
# read each line of File2
>/var/lib/jenkins/Jack/match_output.txt
while IFS=, read -r string; do
# read each line of File1.txt
while IFS=, read -r string2; do
# check match, and write if needed.
if [[ $string == *"$string2"* ]]; then
echo $string >> match_output.txt
echo "wrote "$string" to match_output.txt..."
fi
done < /var/lib/jenkins/Jack/123
done < /var/lib/jenkins/Jack/www
无法读取每行逗号前第一个文件的第一个值并逐行与第二个文件匹配并在新文件中打印输出....
要获取逗号前的第一个值,可以使用cut
命令。
用下面的代码
if [[ $string == *"$string2"* ]]; then
echo $string >> match_output.txt
echo "wrote "$string" to match_output.txt..."
fi
你比较完整的行。如果你只想比较$string
和$string2
的第一个值(逗号前),你需要调整这个比较。
string2FirstValue=`echo "$string2" |cut -d',' -f1`
if [[ $string == *"$string2FirstValue"* ]]; then
echo $string2,$string >> match_output.txt
fi
更安全高效的方法是使用awk
:
awk '
BEGIN {FS=OFS=","}
FNR == NR {
map[] = [=10=]
next
}
{
for (i=1; i<=NF; ++i)
if ($i in map) {
print map[$i], [=10=]
next
}
}' 123 www
tom,123,vicky,tom,home
google,908,google,monk,uber
ram,789,ram,monk,uber
rock,rock,monk,uber
jack,222,jack,monk,uber