简体   繁体   English

将第一个文件的每一行的逗号前的第一个值与第二个文件逐行匹配

[英]Match 1st value before comma of each line from first file with second file line by line

My 1st file -我的第一个文件 -

#cat 123
tom,123
jack,222
rock
google,908
mohan,323
ram,789

My 2nd file -我的第二个文件 -

#cat www
vicky,tom,home
google,monk,uber
dhoom,monk,uber
ram,monk,uber
rock,monk,uber
jack,monk,uber

Desired output -所需 output -

#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

For now, I'm getting only this -现在,我只得到这个 -

#cat match_output.txt
rock,monk,uber

My Script -我的脚本 -

#!/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

Not able to read 1st value of 1st file before the comma of each line and match with 2nd file line by line and print the output in a new file....无法在每行逗号前读取第一个文件的第一个值并逐行与第二个文件匹配并在新文件中打印 output....

To get the first value before the comma, you can use the cut command.要获取逗号前的第一个值,可以使用cut命令。

With the following code使用以下代码

    if [[ $string == *"$string2"* ]]; then
        echo $string >> match_output.txt
        echo "wrote "$string" to match_output.txt..."
    fi

you compare the complete lines.你比较完整的行。 If you want to compare $string with only the first value (before the comma) of $string2 , you need to adjust this comparison.如果您只想将$string$string2的第一个值(逗号之前)进行比较,则需要调整此比较。

    string2FirstValue=`echo "$string2" |cut -d',' -f1`
    if [[ $string == *"$string2FirstValue"* ]]; then
        echo $string2,$string >> match_output.txt
    fi

Safer and more efficient way is to use awk :更安全和更有效的方法是使用awk

awk '
BEGIN {FS=OFS=","}
FNR == NR {
   map[$1] = $0
   next
}
{
   for (i=1; i<=NF; ++i)
      if ($i in map) {
         print map[$i], $0
         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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在文件中找到的每一行的增量(第1个)数字,其中一个 - Increment (1st) number of each line found in file with one 匹配两个文件中的字符串,并在第一个文件中向第二个文件的行尾添加匹配字符串 - Match strings from two files and append line with matching string from first file to end of line of second file 逐行读取文件,并在没有匹配项时打印每行中的第一个匹配项或“ no_data” - Read file line by line and print the first match in each line or “no_data” when nothing matches 从文件中删除第一行时 sed -i 不在 solaris 系统上工作 - sed -i is not working on solaris system while removing 1st line from file Linux SED 脚本找到匹配模式的第一行并将其删除 - Linux SED script find the 1st line that match pattern and delete it 在使用sed遇到“[ERROR] -17-12-2015”模式之前,如何删除从第1行到第1行的行? - How to delete the lines starting from the 1st line till line before encountering the pattern '[ERROR] -17-12-2015' using sed? 在Linux中文件的每一行之前添加文本 - Adding text before each line in file in linux 如何使用 sed 在文件的第一行插入文本? - How do I insert text to the 1st line of a file using sed? 文件中的Python匹配行 - Python Match Line in File 逐行读取文件-&gt;搜索另一个文本文档中的每一行,如果匹配,则输出到另一个文本文件 - Read a file line by line -> search for each line in another text document, if match, output to another text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM