[ale] swap 2 fields (in a tabbed file)Re: [ale] swap 2 fields (in a tabbed file)

cfowler cfowler at outpostsentinel.com
Wed Jan 29 20:48:51 EST 2003



int
main(int argc, char **argv) {
    FILE *fp;
    char buffer[1024];
    char *ptr, *s1, *s2;

    fp = fopen(argv[1], "r");

    while(fgets(buffer, 1024, fp) != NULL) {
      
       // Find first tab
       s1 = buffer;
       if((ptr = strchr(buffer, '\t')) != NULL)
           *ptr++ = 0;

       // Find the <CR>
       s2 = ptr;
       if((ptr =strchr(ptr, 0x0d)) != NULL)
           *ptr++;

       // Write to stdout and reverse them
       fprintf(stdout("%s\t%s\r\n", s2, s1);
    }
   
}

  
That was C.  How about someone do perl now.

On 12/31/1969, "matty91 at bellsouth.net" <matty91 at bellsouth.net> wrote:

>
>awk '{ print $2, " ", $1 }' /some/path/to/file
>
>On Wed, 29 Jan 2003, Christopher Bergeron wrote:
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Does anyone know how I can swap 2 fields in a tab delimited file
>> containing 2 fields?
>>
>> Here's an example of my data:
>> ~ <TAB>WOODSTOCK OUTLET<CR>
>> AF100<TAB>METRO AUDIO DYNAMI<CR>
>>
>> What I'd like to do is swap the 2 fields (or do a `sort` based on the
>> second field!) such that I end up with this:
>> WOODSTOCK OUTLET<TAB> <CR>
>> METRO AUDIO DYNAMICS<TAB>AF100<CR>
>>
>> Does anyone know of a way I can accomplish this?  I tried using cut
>> - -f2,1 -d"t" but no adeu...
>>
>> As always, much thanks in advance!
>> - -CB
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.2.1 (MingW32)
>> Comment: Using GnuPG with Netscape - http://enigmail.mozdev.org
>>
>> iD8DBQE+OBDCTKCy0t3zQgURAjNTAJ4+scGLwJ68zyWNCQi3zOioVQHYSgCgsbsd
>> OQXrEikvL57cV8/Tj1vuqLg=
>> =3GQm
>> -----END PGP SIGNATURE-----
>>
>>
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://www.ale.org/mailman/listinfo/ale
>>
>_______________________________________________
>Ale mailing list
>Ale at ale.org
>http://www.ale.org/mailman/listinfo/ale
>
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale






More information about the Ale mailing list