Results 1 to 5 of 5

Thread: How to put a 0 in front of numbers in a text file in cygwin

  1. #1
    Join Date
    Apr 2005
    Location
    Mars
    Beans
    Hidden!
    Distro
    Ubuntu

    How to put a 0 in front of numbers in a text file in cygwin

    Hi.

    Basically, I have CSV file. The contents of the file might look like this:
    Code:
    "08-41M",43,5,83,"M01",,
    "08-41G",43,56,3,"M01",,
    "08-41T",3,15,83,"M01",,
    But I want it to look like this (notice the prepended 0s):
    Code:
    "08-41M",43,05,83,"M01",,
    "08-41G",43,56,03,"M01",,
    "08-41T",03,15,83,"M01",,
    How would I do this? And this has to work for numbers in those 3 columns only. I'd do this by hand, but there are over 60,000 records .

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: How to put a 0 in front of numbers in a text file in cygwin

    well you could use 'awk' and print the specific fields with fixed width / zero-padding

    Code:
    $ awk -F"," '{ printf "%s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }' file.csv
    "08-41M",43,05,83,"M01",,
    "08-41G",43,56,03,"M01",,
    "08-41T",03,15,83,"M01",,

  3. #3
    Join Date
    Apr 2005
    Location
    Mars
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: How to put a 0 in front of numbers in a text file in cygwin

    Quote Originally Posted by steeldriver View Post
    well you could use 'awk' and print the specific fields with fixed width / zero-padding

    Code:
    $ awk -F"," '{ printf "%s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }' file.csv
    "08-41M",43,05,83,"M01",,
    "08-41G",43,56,03,"M01",,
    "08-41T",03,15,83,"M01",,
    This is what I got when I ran the command:
    Code:
    $ awk -F"," '{ printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }' MIRIAM-only-output.txt
    awk: cmd. line:1: { printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }
    awk: cmd. line:1:          ^ syntax error
    awk: cmd. line:1: { printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }
    awk: cmd. line:1:             ^ syntax error
    awk: cmd. line:1: { printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }
    awk: cmd. line:1:                  ^ syntax error
    awk: cmd. line:1: { printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }
    awk: cmd. line:1:                       ^ syntax error
    awk: cmd. line:1: { printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }
    awk: cmd. line:1:                            ^ syntax error
    awk: cmd. line:1: { printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }
    awk: cmd. line:1:                               ^ syntax error
    awk: cmd. line:1: { printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }
    awk: cmd. line:1:                                  ^ backslash not last character on line
    awk: cmd. line:1: { printf %s,%02d,%02d,%02d,%s,%s,\n", $1, $2, $3, $4, $5, $6 }
    awk: cmd. line:1:                                  ^ syntax error
    [edit]

    Yeah, I got that error because I typed in the command by hand and made a stupid error somewhere. When I copied and pasted, it worked fine.

    Thanks for your help!
    Last edited by YourSurrogateGod; January 10th, 2013 at 06:27 PM.

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: How to put a 0 in front of numbers in a text file in cygwin

    OK glad that worked for you - just be aware it is hard-coded for only the 6 fields per record shown in your snippet - so if your files can have more fields than that you will need to modify it

  5. #5
    Join Date
    Apr 2005
    Location
    Mars
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: How to put a 0 in front of numbers in a text file in cygwin

    Quote Originally Posted by steeldriver View Post
    OK glad that worked for you - just be aware it is hard-coded for only the 6 fields per record shown in your snippet - so if your files can have more fields than that you will need to modify it
    Yup, well aware. Thanks for your help!

    Now trying to get phpMyAdmin to not give me an Internal Server Error (runs on Apache) every time I try to upload the file .

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •