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

Joe jknapka at earthlink.net
Thu Jan 30 11:47:12 EST 2003


cfowler <cfowler at outpostsentinel.com> writes:

With explanatory comments, please? And really, all those calls to
stdlib functions seem like cheating...

Thanks,

-- Joe

> On Wed, 2003-01-29 at 22:45, Geoffrey wrote:       .file   "test.c"
>         .version        "01.01"
> gcc2_compiled.:
>                 .section        .rodata
> .LC0:
>         .string "r"
> .LC1:
>         .string "%s\t%s\r\n"
> .text
>         .align 4
> .globl main
>         .type    main, at function
> main:
>         pushl   %ebp
>         movl    %esp, %ebp
>         subl    $1064, %esp
>         subl    $8, %esp
>         pushl   $.LC0
>         movl    12(%ebp), %eax
>         addl    $4, %eax
>         pushl   (%eax)
>         call    fopen
>         addl    $16, %esp
>         movl    %eax, %eax
>         movl    %eax, -12(%ebp)
>         .p2align 2
> .L3:
>         subl    $4, %esp
>         pushl   -12(%ebp)
>         pushl   $1024
>         leal    -1048(%ebp), %eax
>         pushl   %eax
>         call    fgets
>         addl    $16, %esp
>         movl    %eax, %eax
>         testl   %eax, %eax
>         jne     .L5
>         jmp     .L4
>         .p2align 2
> .L5:
>         leal    -1048(%ebp), %eax
>         movl    %eax, -1056(%ebp)
>         subl    $8, %esp
>         pushl   $9
>         leal    -1048(%ebp), %eax
>         pushl   %eax
>         call    strchr
>         addl    $16, %esp
>         movl    %eax, -1052(%ebp)
>         movl    -1052(%ebp), %eax
>         testl   %eax, %eax
>         je      .L6
>         movl    -1052(%ebp), %eax
>         movb    $0, (%eax)
>         leal    -1052(%ebp), %eax
>         incl    (%eax)
> .L6:
>         movl    -1052(%ebp), %eax
>         movl    %eax, -1060(%ebp)
>         subl    $8, %esp
>         pushl   $13
>         pushl   -1052(%ebp)
>         call    strchr
>         addl    $16, %esp
>         movl    %eax, -1052(%ebp)
>         movl    -1052(%ebp), %eax
>         testl   %eax, %eax
>         je      .L7
>         leal    -1052(%ebp), %eax
>         incl    (%eax)
> .L7:
>         pushl   -1056(%ebp)
>         pushl   -1060(%ebp)
>         pushl   $.LC1
>         pushl   stdout
>         call    fprintf
>         addl    $16, %esp
>         jmp     .L3
>         .p2align 2
> .L4:
>         movl    $0, %eax
>         leave
>         ret
> .Lfe1:
>         .size    main,.Lfe1-main 
>         .ident  "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.3 2.96-113)"
>                                                                         
> > I want to see it in assembler..
> > 
> > cfowler wrote:
> > > 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
> > > 
> > > 
> > 
> > -- 
> > Until later: Geoffrey		esoteric at 3times25.net
> > 
> > The latest, most widespread virus?  Microsoft end user agreement.
> > Think about it...
> > 
> > _______________________________________________
> > 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
> 

-- 
  "I'd rather chew my leg off than maintain Java code, which
   sucks, 'cause I have a lot of Java code to maintain and
   the leg surgery is starting to get expensive." - Me
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale






More information about the Ale mailing list