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

发布于 2025-01-17 23:13:56 字数 1099 浏览 0 评论 0原文

我的第一个文件 -

#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

无法在每行逗号之前读取第一个文件的第一个值,并通过行与第二个文件匹配,并打印在新文件中输出...

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 -

#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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2025-01-24 23:13:57

更安全,更有效的方法是使用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

Safer and more efficient way is to use 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
北音执念 2025-01-24 23:13:56

要获取逗号之前的第一个值,可以使用 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

To get the first value before the comma, you can use the cut command.

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.

    string2FirstValue=`echo "$string2" |cut -d',' -f1`
    if [[ $string == *"$string2FirstValue"* ]]; then
        echo $string2,$string >> match_output.txt
    fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文