Link to home
Start Free TrialLog in
Avatar of shepson990
shepson990

asked on

Sorting file contents

hi

i have a file that i need to sort, using sed, gawk or a program written in c (on linux). The file looks like this below.
enterprises.529.12.3.1.1.290337373 = 290337373
enterprises.529.12.3.1.1.290337377 = 290337377
enterprises.529.12.3.1.1.290337388 = 290337388
enterprises.529.12.3.1.1.290337414 = 290337414
enterprises.529.12.3.1.1.290337428 = 290337428
enterprises.529.12.3.1.1.290337437 = 290337437
enterprises.529.12.3.1.1.290337460 = 290337460
enterprises.529.12.3.1.1.290337465 = 290337465
enterprises.529.12.3.1.1.290337469 = 290337469
enterprises.529.12.3.1.1.290337470 = 290337470
enterprises.529.12.3.1.1.290337471 = 290337471
enterprises.529.12.3.1.1.290337472 = 290337472
enterprises.529.12.3.1.1.290337473 = 290337473
enterprises.529.12.3.1.1.290337474 = 290337474
enterprises.529.12.3.1.1.290337475 = 290337475
enterprises.529.12.3.1.1.290337478 = 290337478
enterprises.529.12.3.1.1.290337479 = 290337479
enterprises.529.12.3.1.2.290337373 = 14
enterprises.529.12.3.1.2.290337377 = 98
enterprises.529.12.3.1.2.290337388 = 90
enterprises.529.12.3.1.2.290337414 = 27
enterprises.529.12.3.1.2.290337428 = 68
enterprises.529.12.3.1.2.290337437 = 80
enterprises.529.12.3.1.2.290337460 = 59
enterprises.529.12.3.1.2.290337465 = 41
enterprises.529.12.3.1.2.290337469 = 13
enterprises.529.12.3.1.2.290337470 = 50
enterprises.529.12.3.1.2.290337471 = 16
enterprises.529.12.3.1.2.290337472 = 49
enterprises.529.12.3.1.2.290337473 = 53
enterprises.529.12.3.1.2.290337474 = 72
enterprises.529.12.3.1.2.290337475 = 58
enterprises.529.12.3.1.2.290337478 = 79
enterprises.529.12.3.1.2.290337479 = 38
enterprises.529.12.3.1.3.290337373 = 2
enterprises.529.12.3.1.3.290337377 = 2
enterprises.529.12.3.1.3.290337388 = 2
enterprises.529.12.3.1.3.290337414 = 2
enterprises.529.12.3.1.3.290337428 = 2
enterprises.529.12.3.1.3.290337437 = 2
enterprises.529.12.3.1.3.290337460 = 2
enterprises.529.12.3.1.3.290337465 = 2
enterprises.529.12.3.1.3.290337469 = 2
enterprises.529.12.3.1.3.290337470 = 2
enterprises.529.12.3.1.3.290337471 = 2
enterprises.529.12.3.1.3.290337472 = 2
enterprises.529.12.3.1.3.290337473 = 2
enterprises.529.12.3.1.3.290337474 = 2
enterprises.529.12.3.1.3.290337475 = 2
enterprises.529.12.3.1.3.290337478 = 2
enterprises.529.12.3.1.3.290337479 = 2
enterprises.529.12.3.1.4.290337373 = "skoen"
enterprises.529.12.3.1.4.290337377 = "linlyn"
enterprises.529.12.3.1.4.290337388 = "beech"
enterprises.529.12.3.1.4.290337414 = "cras"
enterprises.529.12.3.1.4.290337428 = "eugcyber"
enterprises.529.12.3.1.4.290337437 = "agray"
enterprises.529.12.3.1.4.290337460 = "barrybea"
enterprises.529.12.3.1.4.290337465 = "illinois"
enterprises.529.12.3.1.4.290337469 = "bella"
enterprises.529.12.3.1.4.290337470 = "keloside"
enterprises.529.12.3.1.4.290337471 = "malcat"
enterprises.529.12.3.1.4.290337472 = "mulligan"
enterprises.529.12.3.1.4.290337473 = "asdowd"
enterprises.529.12.3.1.4.290337474 = "gdrew"
enterprises.529.12.3.1.4.290337475 = "jwright"
enterprises.529.12.3.1.4.290337478 = "haidee"
enterprises.529.12.3.1.4.290337479 = "atrees"
enterprises.529.12.3.1.5.290337373 = IpAddress: 202.76.1.180
enterprises.529.12.3.1.5.290337377 = IpAddress: 202.76.1.184
enterprises.529.12.3.1.5.290337388 = IpAddress: 202.76.1.166
enterprises.529.12.3.1.5.290337414 = IpAddress: 202.76.1.170
enterprises.529.12.3.1.5.290337428 = IpAddress: 202.76.1.189
enterprises.529.12.3.1.5.290337437 = IpAddress: 202.76.1.171
enterprises.529.12.3.1.5.290337460 = IpAddress: 202.76.1.172
enterprises.529.12.3.1.5.290337465 = IpAddress: 202.76.1.177
enterprises.529.12.3.1.5.290337469 = IpAddress: 202.76.1.182
enterprises.529.12.3.1.5.290337470 = IpAddress: 202.76.1.183
enterprises.529.12.3.1.5.290337471 = IpAddress: 202.76.1.185
enterprises.529.12.3.1.5.290337472 = IpAddress: 202.76.1.186
enterprises.529.12.3.1.5.290337473 = IpAddress: 202.76.1.187
enterprises.529.12.3.1.5.290337474 = IpAddress: 202.76.1.188
enterprises.529.12.3.1.5.290337475 = IpAddress: 202.76.1.161
enterprises.529.12.3.1.5.290337478 = IpAddress: 202.76.1.164
enterprises.529.12.3.1.5.290337479 = IpAddress: 202.76.1.165
enterprises.529.12.3.1.6.290337373 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337377 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337388 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337414 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337428 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337437 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337460 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337465 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337469 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337470 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337471 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337472 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337473 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337474 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337475 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337478 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337479 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.7.290337373 = 3
enterprises.529.12.3.1.7.290337377 = 3
enterprises.529.12.3.1.7.290337388 = 3
enterprises.529.12.3.1.7.290337414 = 3
enterprises.529.12.3.1.7.290337428 = 3
enterprises.529.12.3.1.7.290337437 = 3
enterprises.529.12.3.1.7.290337460 = 3
enterprises.529.12.3.1.7.290337465 = 15
enterprises.529.12.3.1.7.290337469 = 3
enterprises.529.12.3.1.7.290337470 = 3
enterprises.529.12.3.1.7.290337471 = 3
enterprises.529.12.3.1.7.290337472 = 3
enterprises.529.12.3.1.7.290337473 = 15
enterprises.529.12.3.1.7.290337474 = 3
enterprises.529.12.3.1.7.290337475 = 3
enterprises.529.12.3.1.7.290337478 = 3
enterprises.529.12.3.1.7.290337479 = 3

 As you can see the number after "enterprises.529.12.3.1.1." is an ID. What i want to do is have a script/program sort the file to produce the following

skoen 202.76.1.180
linlyn 202.76.1.184
and so on......

So all the i want it to do is export the "username's" and the "ipaddress to go with that username into a file.
Any help would be great.   Sorry for the hard to understand explaination but it is hard to explain
Avatar of sunnycoder
sunnycoder
Flag of India image

>skoen 202.76.1.180
>linlyn 202.76.1.184
>and so on......

where did the names pop up from ??? I do not see a single name in the file

>enterprises.529.12.3.1.2.290337377 = 98
to print the id
>"enterprises.529.12.3.1.1." is an ID
use

awk -F"=" ' { print $1 } ' input_file_name
Avatar of shepson990
shepson990

ASKER

you may want to have a look again because there are names in that file!. Below i have a shorter version of the file.
the id is the number that looks like this 290337373

enterprises.529.12.3.1.1.290337373 = 290337373
enterprises.529.12.3.1.1.290337377 = 290337377
enterprises.529.12.3.1.2.290337373 = 14
enterprises.529.12.3.1.2.290337377 = 98
enterprises.529.12.3.1.3.290337373 = 2          
enterprises.529.12.3.1.3.290337377 = 2
enterprises.529.12.3.1.4.290337373 = "skoen"
enterprises.529.12.3.1.4.290337377 = "linlyn"
enterprises.529.12.3.1.5.290337373 = IpAddress: 202.76.1.180
enterprises.529.12.3.1.5.290337377 = IpAddress: 202.76.1.184
enterprises.529.12.3.1.6.290337373 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.6.290337377 = IpAddress: 255.255.255.255
enterprises.529.12.3.1.7.290337373 = 3
enterprises.529.12.3.1.7.290337377 = 3

I want to delete all lines the cotain "enterprises.529.12.3.1.2, enterprises.529.12.3.1.3, enterprises.529.12.3.1.6, enterprises.529.12.3.1.7 as the data in those lines is not needed. after deleting those lines it would leave the following

enterprises.529.12.3.1.1.290337373 = 290337373
enterprises.529.12.3.1.1.290337377 = 290337377
enterprises.529.12.3.1.4.290337373 = "skoen"
enterprises.529.12.3.1.4.290337377 = "linlyn"
enterprises.529.12.3.1.5.290337373 = IpAddress: 202.76.1.180
enterprises.529.12.3.1.5.290337377 = IpAddress: 202.76.1.184

Now you can see that in the example above there two id's each have 3 lines!.
what i want to do is create a file containing the following
(id username ipaddress)

290337373 skoen 202.76.1.180
290337377 linlyn 202.76.1.184
>you may want to have a look again because there are names in that file!.
sorry, my mistake

>I want to delete all lines the cotain "enterprises.529.12.3.1.2, enterprises.529.12.3.1.3, enterprises.529.12.3.1.6,
>enterprises.529.12.3.1.7 as the data in those lines is not needed.

sed -e 's/.*enterprises\.529\.12\.3\.1\.[2367].*//'  inputfile > outputfile

this will do it

>what i want to do is create a file containing the following
>(id username ipaddress)
will the three lines always be in the order ID, name, IP address or can they be jumbled up in the file
no they must be in oreder
I would suggest using sed to clean the file (remove unwanted lines) and then a C program to get the information
I am giving some snippets, fill in the rest

struct info
{
        char ip[17];
        char name[20];
        char id[10];
};

struct info information[100];

FILE * infile;
char buffer[200], tempbuf[20];
char * temp, * temp1;

int j,i = 0;

infile = fopen ( "inputfile" , "r" );

while ( fgets ( buffer, 200, infile ) != NULL )
{
            temp = strchr (buffer, '=' );
            while ( isblank(temp) )
                      temp++;
            if ( isdigit(temp) )
            {
                      strcpy ( information[i], temp );
                      i ++;
                      continue;
            }
            if ( * temp == '"' )
            {
                      temp1 = strrchr ( buffer, '.' );
                      strncpy ( tempbuf, temp1+1, LENGTH_OF_ID );
                      temp++;
                      for ( j=0; j < i ; j ++ )
                      {
                                 if ( strcmp ( tempbuf, information[j].id ) != 0 )
                                          continue;
                                 strcpy ( information[j].name, temp);
                      }
                      continue;
            }
            if ( * temp == 'I' )
            {
                      *(temp-1) = '\0';
                      temp1 = strrchr ( buffer, '.' );
                      temp = temp + 10;
                      strncpy ( tempbuf, temp1+1, LENGTH_OF_ID );
                      for ( j=0; j < i ; j ++ )
                      {
                                 if ( strcmp ( tempbuf, information[j].id ) != 0 )
                                          continue;
                                 strcpy ( information[j].ip, tempbuf );
                      }
            }
}

after this loop, you will have all values in information[]

NOTE: I typed the code directly here without compiling it
what function would i use to put values from information into the new file
you can use fputs() or fprintf() or fwrite()
I have an affinity for fputs()
I have tried putting the values into the new file but i am completely lost. I should let you know that i am only a begginer. I do know how to put single char's into a file but dont know how to the multiple values from information[] into a file. Can you show me. Below is what i have which i know is wrong

FILE  outfile;

outfile = fopen("finished", "w");

fputs(information[], outfile);
I would use "cut" or "sed'" to strip out the redundant information.
Then use "grep" to split the file into the name data portion and the address data portion.

cut -d. -f7- x > x2

grep "\"" x2 > x_names
grep "IpAddress" x2 | grep -v ": 255" > x_addr

Produces:
[~]$ more x_names
290337373 = "skoen"
290337377 = "linlyn"
[~]$ more x_addr
290337373 = IpAddress: 202.76.1.180
290337377 = IpAddress: 202.76.1.184

After this, you can use a short shell script or a fairly straightforward merge filter to glue the two files together.


#include <ctype.h>
#include <stdio.h>
#include <string.h>

struct info
{
       char ip[17];
       char name[20];
       char id[10];
};

struct info information[100];

int main ()
{
        FILE * infile;
        char buffer[200], tempbuf[20];
        char * temp, * temp1;

        int j,k,i = 0;

        infile = fopen ( "test.txt" , "r" );

while ( fgets ( buffer, 200, infile ) != NULL )
{
//         printf ("buffer:::\n %s",buffer);
           temp = strchr (buffer, '=' );
         temp++;
           while ( isblank( *temp) )
                     temp++;
           if ( isdigit(*temp) )
           {
                     strcpy ( information[i].id, temp );
                     i ++;
                 j =0;
                 while ( isdigit (information[i-1].id[j]))
                       j++;
                 information[i-1].id[j]=0;
                             
                     continue;
           }
           if ( * temp == '"' )
           {
                     temp1 = strrchr ( buffer, '.' );
                     strncpy ( tempbuf, temp1+1, 9 );
                 tempbuf[9] = 0;
                     temp++;
                     for ( j=0; j < i ; j ++ )
                     {
                                if ( strncmp ( tempbuf, information[j].id,9 ) != 0 )
                                         continue;
                                strcpy ( information[j].name, temp);
                        k=0;
                             while ( isalpha(information[j].name[k]))
                                   k++;
                          information[j].name[k]=0;
                     }
                     continue;
           }
           if ( * temp == 'I' )
           {
                     *(temp-1) = 0;
                     temp1 = strrchr ( buffer, '.' );
                     temp = temp + 10;
                     strncpy ( tempbuf, temp1+1, 9 );
                 tempbuf[9] = 0;
                     for ( j=0; j < i ; j ++ )
                     {
                                if ( strncmp ( tempbuf, information[j].id,9 ) != 0 )
                                         continue;
                                strcpy ( information[j].ip, temp );
                     }
           }
}

for ( j = 0 ; j < i; j++ )
      printf ( "%s %s %s",information[j].id, information[j].name, information[j].ip);

      fclose(infile);
      }

:::sorry indenation is screwed up ...
:::assumes id is always 9 chars in length
:::will work for no more than 100 triplets etc... you can change the constants
:::I have compiled and tested it ... it runs fine .... no error checking... add it
also note ::: it is absolutely unoptimized and written on the fly .. optimize it if you so desire
okay yes that's great it works fine but what i didnt know is that the id changes all the time, somtimes its 9 charaters long then its 2 or 3. Any suggestions
change all the sections of code where fixed length id has been used to
if ( isdigit(*id_pointer) )
        id_pointer++;

//here you have the first location after id in id_pointer ... use the text between start and this as id
Sorry i just dont understand what you are doing here!! can you show me where to it in the code or what it is replacing
if ( isdigit(*id_pointer) )
        id_pointer++;
see, previously I had assumed fixed length ids, so I had used statements like
>strncpy ( tempbuf, temp1+1, 9 );
now you cannot copy it blindly ....
I am assuming that id will be all digits (tell me if its is not so)

isdigit() is defined in ctype.h and returns true if the argument is a digit
man isdigit()

the statement like above, will be replaced with something similar to

while ( isdigit((int)*temp1 )
{
       tempbuf[i++] = *temp1;
       temp1++;
}

i.e. as long as we keep getting digits, keep copying the id
one more question. i am trying to put the below chars into a file results but it oly put the first lin, how can i get it to got to the end of file

for ( j = 0 ; j < i; j++ )
     printf ( "%s %s %s",information[j].id, information[j].name, information[j].ip);

     fclose(infile);
     }
what is the value of i ? 1 ?

"%s %s %s\n" might be better
sunnycoder would you be able to post the below code with the addition of the id's being any length. I have tried what you said in you previous post but i just cant get it to work.
you said to replace
>strncpy ( tempbuf, temp1+1, 9 );
with
while ( isdigit((int)*temp1 )
{
       tempbuf[i++] = *temp1;
       temp1++;
}
but what about this line
>if ( strncmp ( tempbuf, information[j].id,9 ) != 0 )
thank alot in advance


#include <ctype.h>
#include <stdio.h>
#include <string.h>

struct info
{
       char ip[17];
       char name[20];
       char id[10];
};

struct info information[100];

int main ()
{
        FILE * infile;
        char buffer[200], tempbuf[20];
        char * temp, * temp1;

        int j,k,i = 0;

        infile = fopen ( "test.txt" , "r" );

while ( fgets ( buffer, 200, infile ) != NULL )
{
//        printf ("buffer:::\n %s",buffer);
           temp = strchr (buffer, '=' );
        temp++;
           while ( isblank( *temp) )
                     temp++;
           if ( isdigit(*temp) )
           {
                     strcpy ( information[i].id, temp );
                     i ++;
               j =0;
               while ( isdigit (information[i-1].id[j]))
                    j++;
               information[i-1].id[j]=0;
                         
                     continue;
           }
           if ( * temp == '"' )
           {
                     temp1 = strrchr ( buffer, '.' );
                     strncpy ( tempbuf, temp1+1, 9 );
               tempbuf[9] = 0;
                     temp++;
                     for ( j=0; j < i ; j ++ )
                     {
                                if ( strncmp ( tempbuf, information[j].id,9 ) != 0 )
                                         continue;
                                strcpy ( information[j].name, temp);
                    k=0;
                          while ( isalpha(information[j].name[k]))
                              k++;
                       information[j].name[k]=0;
                     }
                     continue;
           }
           if ( * temp == 'I' )
           {
                     *(temp-1) = 0;
                     temp1 = strrchr ( buffer, '.' );
                     temp = temp + 10;
                     strncpy ( tempbuf, temp1+1, 9 );
               tempbuf[9] = 0;
                     for ( j=0; j < i ; j ++ )
                     {
                                if ( strncmp ( tempbuf, information[j].id,9 ) != 0 )
                                         continue;
                                strcpy ( information[j].ip, temp );
                     }
           }
}

for ( j = 0 ; j < i; j++ )
     printf ( "%s %s %s\n",information[j].id, information[j].name, information[j].ip);

     fclose(infile);
     }
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops .. last print in the loop will be with \n
Sorry sunnycoder but would you be able to post the whole code again that works with the id of any length???
while ( fgets(buffer, BUF_SIZE,infile) != NULL )
{
       temp = strrchr ( buffer, ' ' );
       while ( * temp != '\n' )
       {
                putchar ( * temp );
                temp++;
       }
       putchar ( ' ' );
       fgets ( buffer, BUF_SIZE,infile );
       temp = strrchr ( buffer, ' ' );
       while ( * temp != '\n' )
       {
                putchar ( * temp );
                temp++;
       }
       putchar ( ' ' );
       fgets ( buffer, BUF_SIZE,infile );
       temp = strrchr ( buffer, ' ' );
       printf ( "%s", temp );
}
what is the above code that you have posted???, its completely different to the other code you posted
yes, perhaps you did not read the previous post carefully enough

run your input file through this script
sed -e 's/enterprises\.529\.12\.3\.1\..\.//' test1.txt | sort
this will reduce the input file like shown above to

290337373 = 290337373
290337373 = IpAddress: 202.76.1.180
290337373 = "skoen"
290337377 = 290337377
290337377 = IpAddress: 202.76.1.184
290337377 = "linlyn"


the above code extracts from this file
okay sorry,
BUF_SIZE undeclared when compiling.
also is this okay, or should it be different
        char buffer[200], tempbuf[20];
        char * temp, * temp1;
you need to declare BUF_SIZE as a #define ... the cpacity of the buffer

#define BUF_SIZE 200

char buffer [BUF_SIZE];
char * temp;

I do not believe we are using temp1 and tempbuf anymore
okay sorry about this but there is just one problem. the code is part of a cgi program written in c. when i run the cgi which has some of my code in it (not mentioned here) it works fine. But as soon as i add the code that you game me the web server displays an error message "unexpected end of script". i have had this problem before on another cgi program written in c. Below is how i fixed the other program with the same problem
before:
char tempc[250];
all i had to do was increase it to
char tempc[900];
and that fixed the problem
but with this program id doesnt seem to work. Any ideas
>"unexpected end of script".
not much idea about cgi .... hope it is not one of those dos and unix things... If you are writing code in windows/dos and executing it on *nix then try running the file through dos2unix .. apart from that, I am not much familiar with cgi
still cant get that code to work.
what would happen if i lust wanted to what user has a specified ip address
from bellow     ip 202.76.1.180 belongs to skoen. how would i go about doing this?
290337373 = 290337373
290337373 = IpAddress: 202.76.1.180
290337373 = "skoen"
290337377 = 290337377
290337377 = IpAddress: 202.76.1.184
290337377 = "linlyn"
Once you have run the program above, it will all be on the same line and results will be trivial to obtain

try running the code without cgi and see if it produces the desired results
I tried your code without the cgi an it worked, but i needed the the cgi feature so i wrote the code bellow which work as cgi. is the code written ok?.  thanks for all your help

while (fgets(buffer, 1000, infile))
{
      if(strstr(buffer,MIPcpy) != NULL)
      {
            sscanf(buffer,"%s %s\n",token[1],token[2]);      
            
      }
      
}
fclose(infile);

infile=fopen(szTemp1,"rw");
while (fgets(line,1000,infile))
{
      if(strchr(line,'"'))
      {
            if(strstr(line,token[1]) !=NULL)
            {
                  strtok(line,"\"");
                  id=strtok(NULL,"\"");
                  printf("\n%s\n",id);
            }
      }
}