简体   繁体   English

Bash:嵌套循环单向比较

[英]Bash: nested loop one way comparison

I have one queston about nested loop with bash. 我有一个关于bash嵌套循环的问题。

I have an input files with one file name per line (full path) I read this file and then i make a nest loop: 我有一个输入文件,每行一个文件名(完整路径),我读取此文件,然后进行嵌套循环:

    for i in $filelines ; do
    echo $i
    for j in $filelines ; do
         ./program $i $j
    done
done

The program I within the loop is pretty low. 我在循环中的程序非常低。 Basically it compare the file A with the file B. 基本上,它将文件A与文件B进行比较。

I want to skip A vs A comparison (ie comparing one file with itslef) AND I want to avoid permutation (ie for file A and B, only perform A against B and not B against A). 我想跳过A与A的比较(即,将一个文件与其itslef进行比较),并且想要避免排列(即,对于文件A和B,仅对B执行A,而对B不执行B)。

What is the simplest to perform this? 什么是最简单的执行此操作?

Version 2: this one takes care of permutations 版本2:这个负责排列

#!/bin/bash

tmpunsorted="/tmp/compare_unsorted"
tmpsorted="/tmp/compare_sorted"

>$tmpunsorted

while read linei
do
    while read linej
    do
        if [ $linei != $linej ]
        then
            echo $linei $linej | tr " " "\n" | sort | tr "\n" " " >>$tmpunsorted
            echo >>$tmpunsorted
        fi
    done <filelines
done <filelines

sort $tmpunsorted | uniq > $tmpsorted

while read linecompare
do
    echo "./program $linecompare"
done <$tmpsorted

# Cleanup
rm -f $tmpunsorted
rm -f $tmpsorted

What is done here: 在这里做什么:

  • I use the while loop to read each line, twice, i and j 我使用while循环读取每行两次,i和j
  • if the value of the lines is the same, forget them, no use to consider them 如果这些线的值相同,则将其忘记,不要考虑它们
  • if they are different, output them into a file ($tmpunsorted). 如果它们不同,则将它们输出到文件($ tmpunsorted)中。 And they are sorted in alphebetical order before going tothe $tmpunsorted file. 在进入$ tmpunsorted文件之前,它们按字母顺序排序。 This way the arguments are always in the same order. 这样,参数始终处于相同顺序。 So ab and ba will be same in the unsorted file. 因此, abba在未排序的文件中将相同。
  • I then apply sort | uniq 然后我应用sort | uniq sort | uniq on $tmpunsorted, so the result is a list of individual argument pairs. $ tmpunsorted上的sort | uniq ,因此结果是单个参数对的列表。
  • finally loop on the $tmpsorted file, and call the program on each individual pair. 最后循环$ tmpsorted文件,并在每个对上调用该程序。
  • Since I do not have your program, I did an echo, which you should remove to use the script. 由于我没有您的程序,因此我做了一个回显,应删除该回显以使用该脚本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM