Re: keep only numeric character

"[email protected] [sed-users]" <[email protected]> 09 Feb 2016 11:21:23 -0800
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
The 2nd field appears to be where all the action is, so we fence it with newline characters since it is guaranteed never to come in a line.
 

 sed -e '
    s/;/\n/;s/;/\n/;   # fence the 2nd field
 

    # null the 2nd field in lines that have a nonnumeric/non-whitespace
    # character present in their 2nd fields.
    /\n".*[^0-9 ].*"\n/bnull
 

    # if we reach here, means the 2nd field has just spaces and/or
    # numerics in them.
    # just need to determine whether the digits are exactly 10 or not.
 

    # store for later recall
    # strip whitespaces globally, dont worry as only 2nd field matters.
    h;s/ //g
 

    # Bingo!! we have a line which has precisely 10 digits in its 2nd field.
    # Remember we had shaved off the whitespaces,
    # hence, we recall from hold area & revert the newlines to field seps
    # and display the line without any modifs
    /\n".\{10\}"\n/{
       g;y/\n/;/;b
    }
 

    # these lines, though have only spaces&/or numbers but they are
    # either != 10 (digits) or all spaces. Hence this 2nd field needs to
    # nulled out. But first we recover from the hold area the actual line.
    g
    :null
    s/\n.*\n/;"";/
 '  yourfile

 

 

 


 ---In [email protected], <rachid.mokrani@...> wrote :

 Hi,
 
 Please, some assistance for the sed command to get the result below.
 
 Input.txt
 "Paul";"01 26 30 69 69";"NY"
 "Dan";"05 26 30 69";"CA"
 "Jane";"26 30 69";"SL"
 "Bill";"03 26 30 69 69";"BT"
 "Steve";"03-26-30-69-69";"BT"
 "Daniel";"NA";"BO"
 "Karen";"01/02/03";"YO"
 
 
 I would like to have (only line with 10 digits) - remove all field less or greater than 10 digits and all fields with not numeric
 
 Output.txt
 "Paul";"01 26 30 69 69";"NY"
 "Dan";"";"CA"
 "Jane";"";"SL"
 "Bill";"03 26 30 69 69";"BT"
 "Steve";"";"BT"
 "Daniel";"";"BO"
 "Karen";"";"YO"
 
 Thanks.
 
 
 __________________________ 
 Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing ! 
 Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message. 
 __________________________
 
 
 [Non-text portions of this message have been removed]



[Non-text portions of this message have been removed]