Thread: Modifying Text Files

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    Question Modifying Text Files

    Could someone experienced in manipulating files help me with a sticky problem? I am trying to take a large, specified number of separate .txt files containing comma-separated data and condense them into one, large file that can be entered into an analysis program in one fell swoop. However, the values from each file need to be appended onto their respective lines to form new columns, rather than just tacked onto the bottom of the file.

    Does anyone have a suggestion on how to do this? Sadly, I am not very experienced with C, so you may have to be somewhat patient with me.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Something like

    File 1:
    London,1
    Paris,2

    File 2:
    London,3
    Paris,4

    Producing a combined result of
    London,1,3
    Paris,2,4
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Surely, a task made for a computer.

    I'd need to see a sample of your input files - just something short, not the whole thing, and then what your desired output would be, as well.

    Something like what Salem posted, but I would like to see a sample of the actual data and actual result; just a concrete snippet.

    How you're identifying "their" respective lines is very important. Please explain in your sample of data you post.

    Adak

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    Quote Originally Posted by Salem
    Something like

    File 1:
    London,1
    Paris,2

    File 2:
    London,3
    Paris,4

    Producing a combined result of
    London,1,3
    Paris,2,4
    Yes, that's it exactly. Thanks to both of you for replying so quickly.

    I attatched some of the data. I'm actually not all that familiar with what it means; I'm an undergrad physics student; I ended up being paid to help some graduate students make their analysis more efficient after I talked with one of my physics professors and mentioned that I had "taken a class in C++ once." Much of their explanation of what they were doing is stuff I know virtually nothing about. Anyway, at the moment, they're entering hundreds of files like this into the analysis software one at a time, manually! I know that the leftmost column (analogous to London, Paris) is a bunch of frequencies; that's the part I'm fuzzy on the meaning of. The rest are tiny DC voltages. This all comes from a study of quantum dots.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    This would be the result I'm trying to get, if you assume the extra elements on the line come from other files like the original one. I guess, to clarify, what I want to do is take the data on each line of a bunch of files like this one that corresponds with each one of the frequencies and add it on a line of data that begins with that corresponding frequency.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    C really isn't the tool for this job.

    Perl on the other hand is.
    Code:
    #!/bin/perl -w
    use strict;
    
    # A hash containing all the composite lines
    my %lines = ();
    
    while ( <> ) {              # for every line of every file
      chomp;s/\r//;             # remove any \n or \r
      if ( /^([0-9]+)(.*)/ ) {  # separate the first number($1) and the rest line($2)
        if ( ! exists $lines{$1} ) {
          $lines{$1} = $1;      # start each new line with the key
        }
        $lines{$1} .= $2;       # append the rest of the line
      }
    }
    
    # print all the lines when we've done reading files.
    for my $i ( sort keys %lines ) {
      print "$lines{$i}\n";
    }
    Which produces these kinds of results
    Code:
    $ perl foo.pl foo.txt
    787500000, -8.07900e+01, -2.14748e+06, -2.14748e+06
    787562500, -8.09020e+01, -2.14748e+06, -2.14748e+06
    787625000, -8.04260e+01, -2.14748e+06, -2.14748e+06
    787687500, -8.05870e+01, -2.14748e+06, -2.14748e+06
    787750000, -8.04020e+01, -2.14748e+06, -2.14748e+06
    
    $ perl foo.pl foo.txt foo.txt
    787500000, -8.07900e+01, -2.14748e+06, -2.14748e+06, -8.07900e+01, -2.14748e+06, -2.14748e+06
    787562500, -8.09020e+01, -2.14748e+06, -2.14748e+06, -8.09020e+01, -2.14748e+06, -2.14748e+06
    787625000, -8.04260e+01, -2.14748e+06, -2.14748e+06, -8.04260e+01, -2.14748e+06, -2.14748e+06
    787687500, -8.05870e+01, -2.14748e+06, -2.14748e+06, -8.05870e+01, -2.14748e+06, -2.14748e+06
    787750000, -8.04020e+01, -2.14748e+06, -2.14748e+06, -8.04020e+01, -2.14748e+06, -2.14748e+06
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Reading text files?
    By Kate in forum C Programming
    Replies: 3
    Last Post: 06-30-2006, 01:22 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM