« Previous Next »

Thread: Parse a text file with "|" as field delimiter and "^" as record delimiter

Last post 06-13-2009 4:35 PM by PatrickMc. 2 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (3 items)

Sort Posts:

  • 05-07-2009, 10:48 AM

    • albertop
    • Not Ranked
    • Joined on 05-07-2009, 2:45 PM
    • Posts 1

    Parse a text file with "|" as field delimiter and "^" as record delimiter

    Hi,

    Is it possible to parse a text file which is basically a CSV file but it has "|" as field delimiter and "^" as record delimiter?

    Example:

    This is field 1|This is field 2|This is field 3^
    This is field 1|This is field 2|This is field 3^

    If yes, how?

    Regards,

    Alberto.

  • 05-27-2009, 10:01 PM In reply to

    Re: Parse a text file with "|" as field delimiter and "^" as record delimiter

     Sure...I broke this into 3 files...but you can make it 2.

    1. Log.txt

    This is field 1|This is field 2|This is field 3^
    This is field 5|This is field 6|This is field 7^

    2. demo.sql

    select * from log.txt

    3. run.bat

    logparser -i:TSV -headerRow:off -iSeparator:"|" file:demo.sql -o:datagrid

     
    I had it spit out into a datagrid since it is easier to read. The headerRow is set to off since no header is in this .txt file. The separator is a | and by using TSV it does not care much about the ^ being end of line row delimiter.

    Hope this helps.

    Check out my blog for other cool tips and tricks:

    http://joelangley.blogspot.com/
  • 06-13-2009, 4:35 PM In reply to

    Re: Parse a text file with "|" as field delimiter and "^" as record delimiter

     

    You can also use biterscripting ( http://www.biterscripting.com ) for CSV files. The $lsep variable describes what separates rows. The $wsep variable describes what separates columns.

     For your case ( | and ^)

     

     

    set $lsep="^"

    set $wsep = "|"

    # Read file in.

    var str data ; cat "file.csv" > $data

    # Process rows one by one.

    while ($data <> "")

    do

        var str row ; lex -e "1" $data > $row

        # Process columns one by one

        while ($row <> "")

        do

            wex -e "1" $row

            echo ","

        done

        echo "\n"

    done

     

     

    The above script will put your CSV file in the more traditional format (comma and newline).

     

    Patrick

     

Page 1 of 1 (3 items)
Microsoft Communities