#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Unix shell script help


    I want to compare two files.

    File1.txt contains the following data
    Hi
    How
    are
    you?

    File2.txt contains the following data
    hello
    world
    good morning
    Hi
    How
    are
    you?

    What i need is.. I want to check whether the content in File1.txt present in File2.txt (In Same sequence) or not.

    Condition is data should be in same sequence after "Hi" there should be "How" and then in next line "are" and then "you?"

    can please help me..
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    14
    Rep Power
    0
    Can file2 contains other lines after those in file1?
    You could use the command diff to compare the files. But if there are other checks to do, better build up a script that compare files line by line.
    For example, should the script consider it correct if the content in file1 starts from line 5 in file2, having lines from 0 to 4 with any other words?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    grep -n will be your friend.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by gregorio.palama
    Can file2 contains other lines after those in file1?
    You could use the command diff to compare the files. But if there are other checks to do, better build up a script that compare files line by line.
    For example, should the script consider it correct if the content in file1 starts from line 5 in file2, having lines from 0 to 4 with any other words?

    Reply:
    File 2 can contain other data after those lines.

    The content in File 1 should present in File 2 exactly any where but in sequence.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by SimonJM
    grep -n will be your friend.

    Sorry it was not working.

    i want to search all the lines in file1 with file2. can please help me with any logic.. Need it in emergency
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    How general purpose would this be? How many times will the lines from file 1 appear in file 2? How many lines would be in file 1?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by SimonJM
    How general purpose would this be? How many times will the lines from file 1 appear in file 2? How many lines would be in file 1?
    The data in file 1 appears only once in file 2.

    File 1 may contain any number of lines but the exact data in File 1 should appear in File 2.

    Any help please
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    Ok, in which case:

    Code:
    tmpf=file.tmp
    grep -f File1.txt File2.txt > $tmpf
    diff -q $tmpf File1.txt
    Should give you a start - there are issues with this method, so be warned!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo