Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Swap text file colomn  (Read 2518 times)

0 Members and 1 Guest are viewing this topic.

Yogesh123

    Topic Starter


    Beginner

    Swap text file colomn
    « on: November 10, 2009, 09:57:00 AM »
    Dear Experts,
    In Dos batch scripting,
    How to swap text file colums,
    I am having text file as follows,

    abc.txt
    11 26 2009
    10 23 2008
    05 15 2009

    and required output as follows within text file,
    2009 26 11
    2008 23 10
    2009 15 05

    basically wanted to replace column no 3 with coloumn no 1 and vice-versa.
    please advise how to go about it.
    Thanx in advance.

    gregflowers



      Rookie

      Thanked: 3
      Re: Swap text file colomn
      « Reply #1 on: November 10, 2009, 01:17:25 PM »
      Code: [Select]
      for /F "tokens=1,2,3" %%i in (abc.txt) do @echo %%k %%j %%i >> new-abc.txt

      Run from a cmd prompt:

          for /?


      for complete information on the 'for' command.

      Salmon Trout

      • Guest
      Re: Swap text file colomn
      « Reply #2 on: November 10, 2009, 01:19:44 PM »
      Best to have this line to start

      if exist new-abc.txt del new-abc.txt

      otherwise, each time you run it (because of using the >> [append] redirection operator) new-abc.txt will grow in size

      Yogesh, you manged to spell "column" 4 different ways!


      billrich

      • Guest
      Re: Swap text file colomn
      « Reply #3 on: November 10, 2009, 01:31:10 PM »

      C:\batextra>type tokswap.bat
      Code: [Select]
      @echo off

      for /f "tokens=1,2,3 delims= " %%a in (abc.txt) do (echo %%c %%b %%a)

      Input:
      C:\batextra>type abc.txt
      11 26 2009
      10 23 2008
      05 15 2009

      Output:
      C:\batextra>tokswap.bat
      2009 26 11
      2008 23 10
      2009 15 05

      C:\batextra>

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: Swap text file colomn
        « Reply #4 on: November 10, 2009, 06:26:59 PM »
        if you can use gawk for windows
        Code: [Select]
        c:\test> gawk "{print $3,$2,$1}" file

        billrich

        • Guest
        Re: Swap text file colomn
        « Reply #5 on: November 10, 2009, 07:40:54 PM »
        C:\batextra>type  swapawk.bat

        Code: [Select]
        c:\bin\awk "{print $3,$2,$1}" abc.txt
        OUTPUT:

        C:\batextra> swapawk.bat

        C:\batextra>c:\bin\awk "{print $3,$2,$1}" abc.txt

        2009 26 11
        2008 23 10
        2009 15 05

        INPUT:

        C:\batextra>type abc.txt
        11 26 2009
        10 23 2008
        05 15 2009

        __________________________________
        C:\batextra>cd ..

        C:\>cd bin

        C:\bin> Usage: awk [-f programfile] [-Fc] [program] [var=value ...] [file ...]

        C:\bin>

        MKS Toolkit Commands – vi, sed, grep, awk, tar, gnu binutils, sh ...
        Familiar environment enables UNIX/Linux developers to be equally productive on Windows with tools like vi, sed, grep, awk, tar, gnu binutils, sh, ksh, csh, ...
        www.mkssoftware.com/products/tk/commands.asp?product.
        « Last Edit: November 10, 2009, 07:53:52 PM by billrich »