Results 1 to 5 of 5

Thread: Using shell loops in unix for comparing files

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Using shell loops in unix for comparing files

    hi,

    can someone help me with a loop condition to compare two files (f1 & f2), f1 against f2.. display only the contents from f1 that do not match that of f2.

    thanks in advance

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    458
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    hi,

    why do you think you need a loop?
    GNU systems have coreutils to do such things, see
    Code:
    info coreutils
    A problem clearly stated is a problem half solved.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    thanks for your valuable suggestion but below is code that does not work as intended.. not sure where i am getting it wrong ..

    Code:
    while read  line 
    do
    for i in $(cat f2)
    do
    if [ $line != $i ]; then 
    echo "  mismatch found $line"
     fi 
    done 
    done < f1
    can you please help me wih this ..
    Last edited by nixcraft; 14th May 2013 at 04:07 PM.

  4. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    458
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    not sure where i am getting it wrong
    `for' loops on words, not on lines.

    you need an extra file descriptor.
    Code:
    while read line_file1
    do
       read line_file2 <&11
       test "$line_file1" = "$line_file2" && echo "$line_file1 are the same" || echo "$line_file1 is different from $line_file2"
    done <file1 11<file2
    als, you have to put line between quotes.
    A problem clearly stated is a problem half solved.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    thanks for your suggestions .. this way i am getting numerous lines of output rather than finding only those words that dont have a match

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Solved] Linux / Unix Shell Script To Edit DNS zone files
    By jbooth45 in forum Domain Name Server
    Replies: 5
    Last Post: 8th October 2012, 04:04 AM
  2. Replies: 2
    Last Post: 20th September 2011, 08:06 PM
  3. [Solved] Small error in unix shell script that is used for backing up files
    By aish in forum Shell scripting
    Replies: 4
    Last Post: 24th June 2011, 05:44 PM
  4. Comparing filename-substrings and remove unnecessary files
    By cypher82 in forum Shell scripting
    Replies: 1
    Last Post: 28th May 2008, 01:53 PM
  5. Replies: 1
    Last Post: 11th February 2008, 06:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •