Thread: rearrangement of columns in data file

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    3

    rearrangement of columns in data file

    HI i am new to c programming and my area is research mainly. I am attaching here two files 1. forum_new.txt 2. forum_old.txt
    Forum_old.txt is the file i am getting from software package and forum_new.txt is the file i require to analyze the data. This time i done it manually as size of fiel is small. If there is any program that can convert automatically Forum_old.txt to forum_new.txt.
    basically this is re-arrangement of columns
    the first column is repeated after 10 values.

    It will be very halpful to me if i get any kind of help.
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,796
    Normally we ask you produce some kind of effort to solving your own problems.
    More than "I'm new, here's my homework".

    So here's a meta answer.
    Code:
    $ cat foo.pl
    #!/usr/bin/perl
    while ( <> ) {
      @f = split;
      push @{$lines{$f[0]}},$f[1] if ( $#f == 1 );
    }
    foreach $l ( sort keys %lines ) {
      print $l . "\t" . join("\t",@{$lines{$l}}) . "\n";
    }
    $ perl foo.pl FORUMS_OLD.txt 
    0.0000	-5.6701	6.2418	6.2418	6.2418	8.8145
    0.0333	-5.6653	6.2093	6.2206	6.2206	8.8023
    0.0667	-5.6507	6.1159	6.1601	6.1601	8.7671
    0.1000	-5.6263	5.9704	6.0666	6.0666	8.7103
    0.1333	-5.5923	5.7837	5.9484	5.9484	8.6347
    0.1667	-5.5486	5.5662	5.8131	5.8131	8.5436
    0.2000	-5.4952	5.3259	5.6669	5.6669	8.4402
    0.2333	-5.4322	5.0687	5.5145	5.5145	8.3267
    0.2667	-5.3596	4.8000	5.3596	5.3596	8.2083
    0.3000	-5.2775	4.5226	5.2046	5.2046	8.0865
    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
    Jul 2017
    Posts
    3
    thanks for your reply but could you please answer in C language

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,796
    Like I said, what do you know so far?

    Can you open files using fopen?

    Can you read lines using fscanf or fgets?

    Do you know what structs are?

    Can you use strcmp()?

    Prove that you're not just being lazy and hoping to score free homework.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2017
    Posts
    3
    yes i am familiar to fopen, scanf, printf, fscanf, frpintf, for loop whie loop, data types declaration , mathematical expressions in C but not efficient in file handling

    fopen(file pointer, "mode")
    scanf("abcd %f gggg %d ", &a, &b)
    Last edited by paliphy; 07-17-2017 at 12:46 AM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,347
    Since you can do it manually, maybe try to imagine a new 2D array in your head, try to notice how the notepad page could match up to the array in your head, and work out the steps.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,796
    > but not efficient in file handling
    Worry about getting it to work before you worry about making it efficient.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    This is not a good problem for C. Something like perl or awk is better suited. Here's some awk:
    Code:
    awk 'NF==2 {a[$1]=a[$1]"\t"$2}; END {for(x in a) print x,a[x]}' FORUMS_OLD.txt  | sort -n
    (Beware anyone playing with the given data: it has the windows line endings. (dos2unix can fix that.))

    In C, the problem is matching up the first column numbers. A linear search is good enough for the data you've shown, but if the real data has more 1st-column values, then a binary search may be better. Both the perl and awk solutions use hash tables for the purpose. Incidentally, they also treat all the numbers as strings, which is a good way to perfectly preserve the input values (instead of converting them to an internal floating-point format and back to an output string again).

    A C solution is very ugly compared to the perl and awk solutions. And to write it in full generality (making it equivalent to the perl and awk solutions) would be somewhat painful.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,796
    Just an idea....

    Read all the lines into memory, then do this
    qsort(3): sort array - Linux man page

    Then you're looking at data arranged as follows
    Code:
    0.0000  -5.6701
    0.0000  6.2418
    0.0000  6.2418
    0.0000  6.2418
    0.0000  8.8145
    0.0333  -5.6653
    0.0333  6.2093
    0.0333  6.2206
    0.0333  6.2206
    0.0333  8.8023
    It would be a lot easier to pick off the data you want in the order you want.
    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.

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    That's a good idea since the key fields are spread out in the original data. Bringing them together really simplifies things.

    There's another possibility if we're allowed to assume that the same key fields appear in the same order in each group, and each group is separated by a blank line (which is the case in the given data).
    Code:
    char table[MAX_ROWS][MAX_LINE]
    
    row = 0
    loop:
      read a line
      if the line is blank
        break
      else
        strcpy(table[row++], line)
    
    row = 0
    loop:
      read a line
      if EOF
        break
      else if line is blank
        row = 0
        continue
      else
        strcat(table[row], "\t")
        strcat(table[row++], second field of line)
    
    print out the table
    You could add a check to ensure that (for all groups after the first) the first field of the input line matched the first field in the current table element (fatal error, otherwise).

    That's probably the simplest solution, but only because it makes the most assumptions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying columns from a text file.
    By alienxx in forum C Programming
    Replies: 2
    Last Post: 12-20-2012, 06:42 AM
  2. Replies: 8
    Last Post: 03-17-2011, 08:37 AM
  3. how to read specfic columns of data in a file.
    By zidangus in forum C Programming
    Replies: 4
    Last Post: 06-16-2010, 07:39 AM
  4. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM
  5. write to .txt file in columns
    By Seron in forum C Programming
    Replies: 4
    Last Post: 11-25-2002, 04:34 PM

Tags for this Thread