Comparing two text files in a UNIX using shell programming

Hi All,
Sorry for posting a UNIX shell query on Database forums.
I want to compare two text files using shell programming. The two text files are actually Business Objects output extracted in a text format. The two output files are not in a tabular format and are no way similar in looking. I want to take one file e.g. file1 as an input file and compare each line of file1 with the other file e.g. file2. During comparison I want to extract the differences found in another file and if possible the similar data as well.
Below is how the files will look like
File 1:
BILL1000000 1111251 122222
RMG1A2 023425100000000010001 11135 102650111100
UHL1 *6999999* *454540001* Weekly *000*
0544424556895PS DATA 01MPS100000/03 MR A A PERTH UTL1234567893106923455053106900000010000005
File 2:
AUTO
APPLICATION=STARTPOINT
START
PROCESSING DATE=01012011
1598301270320099TEST C E 00000031615 123456
7854301276140499TES P 00000062943 234567
UHL1 *6999999* *454540001* Weekly *000*
5569931233333499/123456789 00000013396 345678
4471931233333499ER K J 00000031835123456789012456789
33652931233333499E J L 00000034729123456789012567890
45783123333349921/123456789 00000039080 678901
1452931233333499T R 00000040678123456789012789012
59689312333334994/987654321 00000048614 890123
4112931233333499/987654321 00000060631 901234
1236931217836899 K S 00000043704 012345
END
As you can see above the file are not at all matching except for one record UHL1, but its just an example. As an output I would like to have a third file containing all these records, highlighting the differences, and a fourth file where in only the matched records should get populated.
Please post any useful scripts related to the above scenario.
Many Thanks.
Edited by: 848265 on 06-Apr-2011 04:13

Hi;
For your issue i suggest close your thread here as changing thread status to answere and move it to Forum Home » Linux which you can get more quick response
Regard
Helios

Similar Messages

  • Compare two text file

    i am comparing two text file by checking occurance of a line in file 1 by comparing it with each line in file 2(not line by line)
    i have to print in 3rd text file as difference
    please see my progrm and tell me modification required
    package comp.vnet.comparator;
    import java.io.*;
    import comp.vnet.comparator.NewFile;
    class FileComparator {
         public static void main(String[] args) throws IOException{
              String file1,file2,String1,String2;
              BufferedReader br1,br2;
              int fileCount1=0;
              int fileCount2=0;
              br1= new BufferedReader(new InputStreamReader (System.in));
              // File file = new File ("output.txt");
              FileWriter fstream = new FileWriter("out.txt");
              // FileOutputStream fo = new FileOutputStream("E:/Filecomparator/FileComparator/output.txt");
              System.out.println("Enter First file name");
              file1="b.txt";
              //file1=br1.readLine();
              System.out.println("Enter Second file name");
              file2="a.txt";
              //file2=br2.readLine();
              NewFile nf= new NewFile();
              br1=nf.creatingFile(file1);
              br2=nf.creatingFile(file2);
    while ((String1= br1.readLine()) != null) {
         fileCount1++;
    while ((String2= br2.readLine()) != null) {
         fileCount2++;
    System.out.println("fileCount1+ : " + fileCount1);
    System.out.println("fileCount2+ : " + fileCount1);
    br1=nf.creatingFile(file1);
    BufferedWriter out = new BufferedWriter(fstream);
    for(int i=0;i<=fileCount1;i++)
         br2=nf.creatingFile(file2);
         String1=br1.readLine();
         for(int j=0;j<fileCount2;j++)
                   String2=br2.readLine();
                        if ( String1.equals (String2) ) {
                             System.out.println("the line is equal");
                        else{
                        out.write(String1);
                             System.out.println(String1);
    out.close();
    br1.close();
    br2.close();
              }

    thanks alot for that reply
    but there is some error
    pleasse send me a nice reply

  • Compare two text files in Powershell and if a name is found in both files output content from file 2 to a 3rd text file

    Is it possible using PowerShell to compare the contents of two text files line by line and if a line is found output that line to a third text file?
    Lets say hypothetically someone asks us to search a text file named names1.txt and when a name is found in names1.txt we then pair that with the same name in the second text file called names2.txt
    lets say the names shown below are in names1.txt
    Bob
    Mike
    George
    Lets say the names and contents shown below are in names2.txt
    Lisa
    Jordan
    Mike 1112222
    Bob 8675309
    Don
    Joe
    Lets say we want names3.txt to contain the data shown below
    Mike 1112222
    Bob 8675309
    In vbscript I used search and replace commands to get part of the way there like this
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("testing.txt", ForReading)
    strText = objFile.ReadAll
    objFile.Close
    strNewText = Replace(strText, "Mike ", "Mike 1112222")
    Set objFile = objFSO.OpenTextFile("testing.txt", ForWriting)
    objFile.WriteLine strNewText
    objFile.Close
    That script works great when you know the name you are looking for and the correct values. Lets say someone gives you a list of 1000 employees and says import these names into a list in the correct format and one sheet has the correct names only and
    the other sheet has lots of extra names say 200000 and you only need the 1000 you are looking for in the format from names2.txt.

    Sure,
    Here's a simple one:
    $names1 = "C:\names1.txt"
    $names2 = "C:\names2.txt"
    $names3 = "C:\names3.txt"
    Get-Content $names1 | ForEach-Object {
    $names1_Line = $_
    Get-Content $names2 | Where-Object {$_.Contains($names1_Line)} | Out-File -FilePath $names3 -Append
    This basically just reads $names1 file, line by line, and then read $names2 file line by line as well.
    If the line being evaluated from $names2 file contains the line being evaluated from $names1 file, then the line from $names2 file gets output to $names3 file, appending to what's already there.
    This might need a few more tinkering to get it to perform faster etc depending on your requirements. For example:
    - If either $names1 or $names2 contain a lot of entries (in the region of hundreds) then it will be faster to load the whole content of $names2 into memory rather than opening the file, reading line by line, closing and then doing the same for every single
    line in $names1 (which is how it is currently works)
    - Make sure that your comparison is behaving as expected. The .Contains method always does a case sensitive comparison, this might not be what you are after.
    - You might want to put a condition to ignore blank lines or lines with spaces, else they'll also be brought over to $names3
    Hopefully this will get you started though and ask if you have further questions.
    Fausto

  • Compare two text files

    Guys I have two .txt files like the one below
    00001                
    00002
    00003
    00004
    00005
    00006
    Some elements are missing from one to the other one...
    I need to compare these two and find the different elements of each file...
    I been reading the old post but I cant figure anything out of them
    Any ideas???

    Hi Darin,
    I am doing more or less same sort of thing but its between the two folder number of files to be compared and then copying the missing files lets say from folder A to folder B. The first part is not a problem to read the content and then compare them but what I am stuck in is that, I want to see while the VI is running that how much data is being copying and how much is remaining my be in percentage on some status bar or may be time or may be out of 55 files 20 files copyied 35 remaining etc. Can you please guide me through this how to do it any idea. It will be great help.
    Regards,
    Naqqash
    Naqqash

  • Compare two text files for matching data

    HI
    I want to compare data in two separate spreadsheet files and store the matching data in another spreadsheet file.
    like
    1)reading data from two spread sheets
    2)compare the data of fist one with second
    3)writing the matched data to third one.
    I am find difficulty in combining them to sort string bu string
    pls help me in this
    thanks
    jyohti

    hi
    i am implementing the following logic
    1)  textfile1--------->string to array
    2)textfile 2--------->string to array
    3) compare the elements (substring) of one array to other using for loop
    4)append the match to an array every time you find a match betwwen two arrays
    5)wriitng the result in new file
    i am strugulling in with 3) i have finshed 1,2,4and 5 th part
    problelms are with indexing and loop conditions for me...
    anyways i am trying and feeling like i will finish and post that soon....

  • Compare 2 text files

    Hello guys ... I want to compare two text files and then print line if the files are identical or not. What I have here is comparing linie by line and is printing after each line. How can compare all the text and after that just print if all the file is the same or not
    import java.io.BufferedReader;
    import java.io.FileReader;
    public class compare
         public compare() throws Exception
              BufferedReader fx = new BufferedReader(new FileReader("1.txt"),1024);
              BufferedReader sx = new BufferedReader(new FileReader("2.txt"),1024);
    String s;
    String y;
    while ((s=fx.readLine())!=null)
    if ((y=sx.readLine()).equals (s))
         System.out.println("These line is the Same");
    else
             System.out.println("These line is different");
    sx.close();
    fx.close();
         public static void main(String[] args) throws Exception
                 new compare();
    }Thanks

    adi77 wrote:
    do you have any idee what could be the problem?Yes, all sorts of things.
    Don't compare Strings with ==
    sx.readLine() == fx.readLine()Use equals() instead
    sx.readLine().equals(fx.readLine())Do you know what the word 'different' means?
    if (sx.readLine() == fx.readLine())
            different = true;If so, then why do you set 'different' to true when the lines are the same?
    if (sx.readLine() != fx.readLine())
         System.out.println("This file is identical");You've already read all the lines in the files, why are you reading again? Use the boolean you set in your loop!
    if (different)
        System.out.println("File is different");

  • Compare two .txt files and show result

    HI
    Could anybody show me how to compare two text files and show the result.
    i.e.
    textfile1.txt
    harry.denmark
    karry.sweden
    textfile2.txt
    harry.denmark
    karry.sweden
    marry.usa
    Compare
    result=
    marry.usa
    The text files I want to compare are how ever much larger than this example. (up to 2-3.000 words)
    anybody ??
    Sincerly
    Peder

    HI & thanks for reply
    I know almost nothing about java so could you or anybody please show me the code to do this? Or is it perhaps too large or difficult a code?
    I know how to compile a .java file and run it in prompt :-) and thats about it (almost)
    I offcourse understand if its too much to ask for :-)

  • File Comparison using Shell Programming

    Hi All,
    I want to compare two text files using shell programming. The two text files are actually Business Objects output extracted in a text format. The two output files are not in a tabular format and are no way similar in looking. I want to take one file e.g. file1 as an input file and compare each line of file1 with the other file e.g. file2. During comparison I want to extract the differences found in another file and if possible the similar data as well.
    Below is how the files will look like
    File 1:
    BILL1000000 1111251 122222
    RMG1A2 023425100000000010001 11135 102650111100
    UHL1 6999999 454540001 Weekly 000
    0544424556895PS DATA 01MPS100000/03 MR A A PERTH UTL1234567893106923455053106900000010000005
    File 2:
    AUTO
    APPLICATION=STARTPOINT
    START
    PROCESSING DATE=01012011
    1598301270320099TEST C E 00000031615 123456
    7854301276140499TES P 00000062943 234567
    UHL1 6999999 454540001 Weekly 000
    5569931233333499/123456789 00000013396 345678
    4471931233333499ER K J 00000031835123456789012456789
    33652931233333499E J L 00000034729123456789012567890
    45783123333349921/123456789 00000039080 678901
    1452931233333499T R 00000040678123456789012789012
    59689312333334994/987654321 00000048614 890123
    4112931233333499/987654321 00000060631 901234
    1236931217836899 K S 00000043704 012345
    END
    As you can see above the file are not at all matching except for one record UHL1, but its just an example. As an output I would like to have a third file containing all these records, highlighting the differences, and a fourth file where in only the matched records should get populated.
    Please post any useful scripts related to the above scenario.
    Many Thanks.

    Nik,
    Thanks for that script. Its solving my purpose upto an extent.
    Going another level to that example, in cases where in the first column of file 1 is to be compared with second or third column in file 2, what all the changes should be made to your script. For example,
    File 1:
    1       a       APPLICATION=WWAGES      8       9
    2       b       1200301270320099TEST  C E         00000031615            123456
    3       c       1200301276140499TES  P            00000062943            234567
    4       d       APPLICATION=WWAGES      6       7
    5       e       1200301270320099TEST  C E         00000031615            123456
    6       f       1200301276140499TES  P            00000062943            234567
    7       g       APPLICATION=WWAGES      4       5
    8       h       1200301270320099TEST  C E         00000031615            123456
    9       i       1200301276140499TES  P            00000062943            234567
    10      j       APPLICATION=WWAGES      2       3
    11      k       1200301270320099TEST  C E         00000031615            123456
    12      l       1200301276140499TES  P            00000062943            234567
    File 2:
    a       1       APPLICATION=WWAGES      8       9
    b       2       1200301270320099TEST  C E         00000031615            123456
    g       7       APPLICATION=WWAGES      4       5
    h       8       11200301270320099TEST  C E         00000031615            12223456
    k       12      1200301276140499TES  P            00000062943            234567Now, in the above example First and Second Columns of File 1 are to be compared against Second and First Columns of File 2 respectively. Please see that in file 2, 12 and K are not matching with file1's record 11 and K, so they should not be picked.
    Based on the matched columns, will continue comparing rest of the fields for the records.
    Please guide me the changes to be done in your script for catering to the above scenario.
    Thanks.

  • How to compare content of two text file using StreamTokenizer

    hi....
    i have two text files...containg field like(name,number,scheme) and(number,date,value)... i want to create a third file containg field like (name,number,date,scheme,value) by using these two table. how to create

    I think this code can solve your problem.
    private static final String DELIM = ",";
    * Compile two files.
    * @param file1 String the input file 1
    * @param file2 String the input file 2
    * @param file3 String the output file
    * @throws IOException error in reading/writing
    public void compileFiles(String file1, String file2, String file3) throws
            IOException {
            BufferedReader reader1 = new BufferedReader(new InputStreamReader(
                new FileInputStream(file1)));
            BufferedReader reader2 = new BufferedReader(new InputStreamReader(
                new FileInputStream(file2)));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file3)));
            String line1 = reader1.readLine();
            String line2 = reader2.readLine();
            while (line1 != null && line2 != null) {
                writer.write(compileLines(line1, line2));
                writer.newLine();
                line1 = reader1.readLine();
                line2 = reader2.readLine();
            reader1.close();
            reader2.close();
            writer.close();
        private String compileLines(String line1, String line2) {
            StringTokenizer tok1 = new StringTokenizer(line1, DELIM);
            StringTokenizer tok2 = new StringTokenizer(line2, DELIM);
            String name = tok1.nextToken();
            String number = tok1.nextToken();
            String scheme = tok1.nextToken();
            // ignore number
            tok2.nextToken();
            String date = tok2.nextToken();
            String value = tok2.nextToken();
            StringBuffer buffer = new StringBuffer();
            buffer.append(name);
            buffer.append(DELIM);
            buffer.append(number);
            buffer.append(DELIM);
            buffer.append(date);
            buffer.append(DELIM);
            buffer.append(scheme);
            buffer.append(DELIM);
            buffer.append(value);
            return buffer.toString();
        }

  • Compare two pdf files using adobe acrobat through command line

    Does anyone know how to compare two pdf files using adobe acrobat through command line. I want to do this via command line because we want to compare hundreds of file every day through some automated windows tasks.
    If command line option is not available in acrobat, then is it feasible to make use of acrobat javascript API to do this task?
    Any kind of help will be greatly.

    Command-line: Not possible.
    JavaScript: Possible, but very limited. Basically the only thing you can do is simulate clicking the Compare Documents button. The rest has to be done manually.
    However, it *might* be possible to automate this process a bit more using a plugin. Ask over at the Acrobat SDK forum for more information...

  • How to read two text files using the Read from spreadsheet.vi and then plot it

    I need to read two text files using the read from spreadsheet function and then plot these files over time.
    The file with the .res extention is the RMS values (dt and df) and the second file contains the amplitude of a frequency spectrum.
    I really appreciate any help..thanks
    Attachments:
    RMS.txt ‏1 KB
    FREQUENCY.txt ‏1 KB

    From NI Example Finder:
    Write to Text File.vi
    Read from Text File.vi
    Jean-Marc
    LV2009 and LV2013
    Free PDF Report with iTextSharp

  • How to compare two fmx files

    Hi,
    Is it possible to compare two fmx files , there are few differences between two fmx files , i wanted to know what are the differences.............Thanks.Bcj

    by converting fmb files into xml or text and then using gvimdiff (or similar) on unix or windows will show you the differences. For fmx files, as pointed earlier - you can only see that if they are different or not. On unix you can use strings command (for fmx files) and pipe it to a text file and then do a diff on the files, but that will not be too helpful either.

  • Difference in reading a text file in Windows & Unix

    I have a code which is working fine in windows but is stopping after reading the first line of a text file in
    Unix. The code is something like below:-
    declare
    file_handle text_io.file_type; /*running form*/
    filename varchar2(60);
    v_line varchar2(200);
    begin
    message('before fopen');
    filename:=:file_name;message(filename);
    file_handle:=text_io.fopen(filename,'R');
    loop
    begin
    text_io.get_line(file_handle,v_line);
    --:line:=v_line;
    exception
    when no_data_found then
    text_io.fclose(file_handle);
    message('NO MORE DATA IS FOUND ');
    temp:='stop';
    exit;
    when others then
    text_io.fclose(file_handle);
    message('ERROR ');
    exit;
    end;
    mob_no:=substr(v_line,1,10);
    begin
    message(' Mobile NO is '||mob_no);
    message(' Mobile NO is '||mob_no);
    exit when temp='stop';
    end loop;
    commit;
    message('File SUCCESSFULLY LOADED........');
    message('File SUCCESSFULLY LOADED........');
    end;
    Is there any difference in reading a text file in Windows & Unix.
    I hope, my question is clear. Please help in solving the doubt as it is urgent.
    Regards.

    There is no difference in reading a file - you use TEXT_IO in the normal way. However there is of course a difference in the format of text files between Unix and Dos so if you had say transferred your test file from Dos to the Unix box for testing strange things might happen if you had not transferred in ASCII mode.

  • Compare two text box values????

    Hello All,
    I do have one logic, but not sure where to write it so want your guys help for the same.
    The logic is:-
    if (&P13_NEW_PASSWORD. == &P13_CONFIRM_NEW_PASSWORD.) then
    insert into tbuser (password) values (&P13_NEW_PASSWORD.);
    else
    dbms_output.line_('Both the text boxes should be same!!')
    P13_NEW_PASSWORD and P13_CONFIRM_NEW_PASSWORD are two text boxes, where in above statment I'm comparing two text box values, if yes than insert that values into the DB tables otherwise displaying else messges.
    so can anybody tell me where should i write the above logic to make it work perfect..!!!
    thanks
    regards,
    Kumar

    Kumar,
    I'm glad you asked! Here's a great blog post on a custom auth scheme that hashes passwords:
    http://djmein.blogspot.com/2007/07/custom-authentication-authorisation.html
    I would suggest a few changes to the "get_hash" function. First, the DBMS_OBFUSCATION_TOOLKIT has been deprecated in favor of DBMS_CRYPTO:
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_crypto.htm
    http://www.psoug.org/reference/dbms_crypto.html
    DBMS_CRYPTO offers Message Authentication Code (MAC) functions which are keyed hashes. If you use a non-keyed function such as MD5 (which is VERY well known), someone can easily generate a table of all possible passwords within reason and their matching hash, which essentially allows them to get the passwords. MAC functions take in the data (password in this case) and a key. You can probably just use a big random string for your key. Now someone would need to the key as well to generate a hash table. In short, just use DBMS_CRYPTO.MAC where Duncan used DBMS_OBFUSCATION_TOOLKIT.md5.
    Tyler

  • How to compare two XSD files programmatically

    Hi
    I want to compare two XSD files programmatically to find out whether one XSD is a sub set of another XSD(Means checking all the elements and attributes of one schema to be present in another schema).
    Please let me know how this can be done in an efficient way.
    Thanks in advance.
    Mithun K

    Or simply apply an oracle text index on your pdf column:
    SQL>  create table t (id integer primary key, bl blob)
    Table created.
    SQL>  declare
    bf bfile := bfilename('TEMP','b32001.pdf');
    bl blob;
    begin
    dbms_lob.createtemporary(bl,true);
    dbms_lob.open(bf,dbms_lob.lob_readonly);
    DBMS_LOB.LOADFROMFILE(bl, bf,dbms_lob.getlength(bf));
    insert into t values (1,bl);
    commit;
    dbms_lob.close(bf);
    dbms_lob.freetemporary(bl);
    end;
    PL/SQL procedure successfully completed.
    SQL>  create index t_idx on t (bl) indextype is ctxsys.context parameters ('filter ctxsys.auto_filter')
    Index created.
    SQL>  declare
       mklob   clob;
    begin
       ctx_doc.filter ('t_idx', '1', mklob, true);
       dbms_output.put_line (substr (mklob, 1, 250));
       dbms_lob.freetemporary (mklob);
    end;
    Oracle® Database
    Release Notes
    11
    g
    Release 1 (11.1) for Linux
    B32001-04
    November 2007
    This document contains important information that was not included in the
    platform-specific or product-specific documentation
    PL/SQL procedure successfully completed.This generates a text only version of your pdf and standard text comparison methods can be applied ....

Maybe you are looking for