Results 1 to 9 of 9

Thread: add new character to the beginning of some lines

  1. #1
    Join Date
    Aug 2013
    Beans
    37

    add new character to the beginning of some lines

    Hi guys

    I have a large list of uk phone numbers all of which should be 11 characters long.

    They should begin with a 0 some how ever have the 0 missing is there any way to iterate though the file and add a 0 as the first character if it does not laready exist.

    thanks pete

  2. #2
    Join Date
    Dec 2007
    Beans
    12,521

    Re: add new character to the beginning of some lines

    You should include a few lines to illustrate what you want. The experts here will then be better placed to help you.

  3. #3
    Join Date
    Aug 2013
    Beans
    37

    Re: add new character to the beginning of some lines

    list is as below

    07887123456
    7878345343
    07070712345
    799999999

    the numbers that do not start with a 0 should have a 0 added to them so all file is in a standard format !

    thanks

  4. #4
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: add new character to the beginning of some lines

    You can use printf in many scripting languages. Here is one example from awk.

    Code:
    awk '{ printf("%011d\n", $1)}' < list1.txt > list2.txt
    Edit: better double check to make sure that does what you want.
    Last edited by Lars Noodén; October 22nd, 2013 at 01:27 PM.

  5. #5
    Join Date
    Aug 2013
    Beans
    37

    Re: add new character to the beginning of some lines

    e sort of works but double zeros records already starting with zero

  6. #6
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: add new character to the beginning of some lines

    Here's a second approach.

    Code:
    awk '/^0/ { print $1; } ! /^0/ { print "0" $1 }' < file1.txt > file2.txt
    It won't check the number length, just prepend a zero if it is missing from the start of the number.

  7. #7
    Join Date
    Sep 2013
    Beans
    11

    Re: add new character to the beginning of some lines

    if you want to examine before the change is made what the change will be like:

    $> vim yourfile
    :%s/^\([1-9]\)/0\1/
    # if it has done what you have intended
    :wq

    #if not
    u
    :q!

  8. #8
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: add new character to the beginning of some lines

    The sed-based solution:
    Code:
    sed -re 's/^([0-9]{10})$/0\1/' <phonenums.txt
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  9. #9
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: add new character to the beginning of some lines

    Perl solution (assuming text file with one phone number per line):

    Code:
    $ perl -pne 's/(.*)/0$1/ if ( ! /^0/ )' numbers.txt 
    07887123456
    07878345343
    07070712345
    0799999999

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
  •