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

Author Topic: Batch script to remove columns from txt file  (Read 7328 times)

0 Members and 1 Guest are viewing this topic.

carmine

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows 7
    Batch script to remove columns from txt file
    « on: November 26, 2014, 08:59:44 PM »

    I have a text file that has columns delimited using a pipe character ("|"). The csv has hundreds of columns, and I want to remove column 16&17 and rest of the columns to be intact. How do i create a batch script that will create a new output.txt file.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch script to remove columns from txt file
    « Reply #1 on: November 26, 2014, 09:07:15 PM »
    May I ask why?  :)
    Why do you need to do that?
    Why use a script in Batch?
    Programs the use CSV have ways to read and modify the files.
    Microsoft says one can use Excel to do it.
    But f batch is better, they would have recommended it.

    Reference:
    Microsoft Excel Tutorial: How to work with CSV files

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Batch script to remove columns from txt file
    « Reply #2 on: November 27, 2014, 07:45:32 AM »
    I have a text file that has columns delimited using a pipe character ("|"). The csv has hundreds of columns, and I want to remove column 16&17 and rest of the columns to be intact. How do i create a batch script that will create a new output.txt file.

    It does depends on the text makeup of the file, unicode, foreign text, the maximum length of a line, the total size of the file.


    dbenham



      Greenhorn

      Thanked: 3
      • Experience: Expert
      • OS: Windows 7
      Re: Batch script to remove columns from txt file
      « Reply #3 on: November 27, 2014, 08:35:52 AM »
      If the file is ASCII, then you can do this very quickly with JREPL.BAT

      Assuming none of the columns contain the quoted pipe characters as part of the value, then:
      Code: [Select]
      jrepl "^((?:.*?\|){15}).*?\|.*?\|" "$1" /f "yourFile.csv" /o -

      The above will overwrite the original file. If you want to preserve the original and write a new file, then use /o "newFile.csv" instead of /o -

      It can also be made to work if column values can contain quoted pipe literals. It even supports quotes within the quoted value (escaped as ""):
      Code: [Select]
      jrepl "^((?:\q(?:\q\q|[^\q])*\q\||.*?\|){15})(?:\q(?:\q\q|[^\q])*\q\||.*?\|){2}" "$1" /x /f "yourFile.csv" /o -


      Dave Benham
      « Last Edit: November 27, 2014, 08:56:30 AM by dbenham »
      Dave Benham

      carmine

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Windows 7
        Re: Batch script to remove columns from txt file
        « Reply #4 on: November 29, 2014, 12:10:14 PM »
        Hi,

        Many thanks to Dave Benham for helping.
        Thanks.