none
Shell Script not returning expected results... Please somebody help RRS feed

  • Question

  • Hi there,

    Please can someone assist me. I have two files with a list of strings in each file. I want to loop through the one file and detect whether or not the string exists in the other file or not. If it exists, then write it to a 'found' file, if not, write it to a 'not found' file.

    Below is the code of the shell script, as well as the files with the data.

    sh-4.2$ ls -ltr
    total 16
    -rw-r--r-- 1 dovaled ifrs_sys 354 Jul 21 09:48 main.txt
    -rwxr-xr-x 1 dovaled ifrs_sys 568 Jul 21 09:51 a5.sh
    -rw-r--r-- 1 dovaled ifrs_sys 278 Jul 21 09:51 dd3.txt
    -rwxrwxrwx 1 dovaled ifrs_sys   0 Jul 21 09:55 found_contracts.csv
    -rwxrwxrwx 1 dovaled ifrs_sys 278 Jul 21 09:55 missing_contracts.csv
    sh-4.2$
    sh-4.2$
    sh-4.2$ cat dd3.txt
    10409561579
    10647342472
    10410751718
    1-10310205358
    1-11960551636
    1-11960384578
    1-11709322079
    1-10727806535
    1-10761366384
    1-8794955492
    1-10697440355
    1-12105301440
    1-10647342472
    1-13148049981
    1-12982602245
    1-13056449255
    1-13094644397
    1-13180589200
    1-13037776752
    sh-4.2$
    sh-4.2$
    sh-4.2$
    sh-4.2$ cat main.txt
    FX103_1-13228638029|1-10310205358|
    FX103_1-13228638029|1-13228638029|
    FX103_1-13228638029|1-13228638029|
    FX103_1-13228638029|1-13228638029|
    FX103_1-1350504122|1-1350504122|
    FX103_1-1350504122|1-1350504122|
    FX103_1-1350504122|1-1350504122|
    FX103_1-13762982144|1-13762982144|
    FX103_1-13762982144|1-13762982144|
    FX103_1-13762982144|1-13762982144|
    sh-4.2$
    sh-4.2$
    sh-4.2$
    sh-4.2$ cat a5.sh
    echo "Please enter the filename : "
    read infile
    echo 'Input file='$infile
    rm found_contracts.csv missing_contracts.csv
    touch found_contracts.csv missing_contracts.csv
    chmod 777 found_contracts.csv missing_contracts.csv
    #grep 'INCEPTION|CDET' FIXED_CRM_31122016_01.txt > INCEPTIONS.txt
    for i in `cat dd3.txt | awk '{print $1}'`
    do
         cnt2=0
         echo 'processing...' $i
         cnt2=`grep $i $infile | wc -l`
         echo $cnt2 'rows found'
         if [ $cnt2 == 0 ]
         then
            echo $i >> missing_contracts.csv
         else
            echo $i >> found_contracts.csv
         fi
    done
    sh-4.2$

    sh-4.2$ sh a5.sh
    Please enter the filename :
    main.txt
    Input file=main.txt
    processing... 10409561579
    0 rows found
    processing... 10647342472
    0 rows found
    processing... 10410751718
    0 rows found
    processing... 1-10310205358
    0 rows found
    processing... 1-11960551636
    0 rows found
    processing... 1-11960384578
    0 rows found
    processing... 1-11709322079
    0 rows found
    processing... 1-10727806535
    0 rows found
    processing... 1-10761366384
    0 rows found
    processing... 1-8794955492
    0 rows found
    processing... 1-10697440355
    0 rows found
    processing... 1-12105301440
    0 rows found
    processing... 1-10647342472
    0 rows found
    processing... 1-13148049981
    0 rows found
    processing... 1-12982602245
    0 rows found
    processing... 1-13056449255
    0 rows found
    processing... 1-13094644397
    0 rows found
    processing... 1-13180589200
    0 rows found
    processing... 1-13037776752
    0 rows found
    sh-4.2$

    sh-4.2$ grep 1-10310205358 main.txt
    FX103_1-13228638029|1-10310205358|
    sh-4.2$

    Please someone tell me what is wrong with my code.

    Thank you

    • Moved by Bill_Stewart Wednesday, September 13, 2017 8:58 PM This is not "scripts on demand"
    Friday, July 21, 2017 8:58 AM

All replies

  • It appears you are posting in the wrong forum.

    This forum is for admin scripting with WSH and PowerShell.  It is not a forum for Unix shell scripts.  Post in a Unix forum for your issues.


    \_(ツ)_/

    Friday, July 21, 2017 9:05 AM
  • Hi,

    Just in case anyone else runs across this, here's a PowerShell script that will help.

    $file1 = Get-Content .\file1.txt
    $file2 = Get-Content .\file2.txt
    
    foreach($line in $file1)
    {
        if($file2 -contains $line)
        {
            $line |
                Out-File exist.txt -Append
        }
        else
        {
            $line |
                Out-File notExist.txt -Append
        }
    }



    Friday, July 21, 2017 2:00 PM