counting how many time the order of two consecutive number in a file are reversed in a second file in BASH

前端 未结 2 373
予麋鹿
予麋鹿 2021-01-07 02:24

I got the following problem:

Given 2 files of N numbers like

file1.dat: 1,2,3,4,5,6,7,8,9,0

file2.dat: 2,5,4,7,6,9,8,1,0,3

The file2.dat is a

2条回答
  •  佛祖请我去吃肉
    2021-01-07 02:54

    I know that you already have an accepted clean answer.

    Just sharing:

    sgeorge-mn:~ sgeorge$ cat stack.sh 
    VALUE1=$1
    VALUE2=$2
    for POS in `sed 's/,/ /g' file1.dat`
    do
    ((COUNT++))
    if [[ $VALUE1 == $POS ]] ; then
        VAL1_POS=$COUNT
    fi
    
    if [[ $VALUE2 == $POS ]] ; then
            VAL2_POS=$COUNT
    fi
    done
    
    
    for MATCH in `sed 's/,/ /g' file2.dat`
    do
    ((COUNT2++))
    if [[ $VALUE1 == $MATCH ]] ; then
            VAL1_POS2=$COUNT2
    fi
    if [[ $VALUE2 == $MATCH ]] ; then
            VAL2_POS2=$COUNT2
    fi
    done
    
    if [[ $VAL1_POS -gt $VAL2_POS ]] ; then
        P1=1
    fi
    if [[ $VAL1_POS2 -gt $VAL2_POS2 ]] ; then
        P2=1
    fi
    
    if [[ $VAL1_POS -lt $VAL2_POS ]] ; then
        P1=2
    fi
    if [[ $VAL1_POS2 -lt $VAL2_POS2 ]] ; then
        P2=2
    fi
    
    if [[ $VAL1_POS -eq $VAL2_POS ]] ; then
        P1=3
    fi
    if [[ $VAL1_POS2 -eq $VAL2_POS2 ]] ; then
        P2=3
    fi
    
    if [[ $P1 == $P2 ]]; then
        echo "No order change"
    else
        echo "Order changed"
    fi
    

    How to execute the script:

    I am assuming following:

    • Both file have exactly same numbers in same order.
    • You will not give non existing number (not existing in file*.dat) as input to the script
    sgeorge-mn:~ sgeorge$ bash stack.sh 5 7
    No order change
    sgeorge-mn:~ sgeorge$ bash stack.sh 4 5
    Order changed
    sgeorge-mn:~ sgeorge$ bash stack.sh 9 0
    No order change
    sgeorge-mn:~ sgeorge$ bash stack.sh 1 2
    Order changed
    

提交回复
热议问题