Results 1 to 7 of 7
  1. #1
    Join Date
    May 2003
    Posts
    847

    How to keep the first five digits and delete the rest ?

    Hello

    I have .txt file with clients activity ID consist of 10 digits in each line

    How can I keep only the first five digits from each line and save them in new file?
    Note: space also counted.

    Examples

    Before
    1004512786
    After
    10045

    Or

    Before
    10 4512786
    After
    10 45


    Like that
    Keeping in mind that I have like more then 1000 lines
    how to do it via linux or Unix ?

    Hope you got my point
    Thank you in advance

  2. #2
    Join Date
    Oct 2002
    Location
    State of Disbelief
    Posts
    22,948
    Having problems, or maybe questions about WHT? Head over to the help desk!

  3. #3
    Join Date
    Apr 2002
    Location
    USA
    Posts
    117
    bear's suggestion should work, example:

    <?php
    // this will return abcde
    echo substr('abcdefghijk', 0, 5);
    echo "<br>";
    ?>

  4. #4
    Join Date
    Apr 2009
    Location
    Pittsburgh, Pennsylvania
    Posts
    582
    if you post a couple lines of the txt file or some example of it, i will be glad to help you out.

  5. #5
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    In shell:
    Code:
    $ more foo
    1004512786
    10 4512786
    
    $ cat foo | colrm 6
    10045
    10 45
    In Python:
    PHP Code:
    python -"for line in open('foo'): print line[:5]" 
    10045
    10 45 
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  6. #6
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    958
    In Ruby
    PHP Code:
    ruby -"File.read('foo').each {|line| puts line[0..4]}"
    10045
    10 45 

  7. #7
    Join Date
    Aug 2005
    Location
    UK
    Posts
    654
    While I'm sure the OP most likely wanted a PHP example, and sonic10's will defiantly suffice.. I want to join in.

    C:
    Code:
    char * before = "1004512786";
    char after[6];
    
    strncpy(after, before, 5);
    Perl:
    Code:
    $before = "1004512786";
    $after = substr($before, 0, 5);

Similar Threads

  1. Some digits on space/bandwidth
    By erapid in forum Running a Web Hosting Business
    Replies: 3
    Last Post: 07-01-2002, 01:03 PM
  2. Last 3-digits of a CC
    By progex in forum Running a Web Hosting Business
    Replies: 3
    Last Post: 04-07-2002, 12:13 PM

Posting Permissions

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