sed to keep only numbers from the list

sachin
edited December 2021 in Linux

from this sample list of data i want to exract numbers only using sed in linux

9898922479
'19897774506'
9896460144

"9896300029"
98924645271

9858392130
9852150122
ss
9802108399sac
9802088710txt
ss

into output like this format

9898922479
9897774506
9896460144
9896300029
9858392130
9852150122

Answers

  • sachin
    edited December 2021
    #replace LF; with CR;LF; Carriage Return (Line Feed / CRLF)
    unix2dos file.txt
    
    #create new line at the end if missing
    echo \ >> file.txt
    
    #this will remove all the single and double quotes
    sed -i 's/[\"'\''\ ]//g' file.txt
    
    #this will remove all lines which do not match criteria (number starting with 1 and of 11 digits or 10 digit number not starting with 1)
    sed -i -E '/^1?[^1][0-9]{9}\s$/!d' file.txt
    
    #any spaces, tabs in lines
    sed -i '/^[[:space:]]*$/d' file.txt
    
    #replacing 1 in first postion with blank
    sed -i 's/^1//' file.txt
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!