Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting just a phone number from a file

I'm sure the answer to this is already online however i don't know what i am looking for. I just started a course in Unix/Linux and my dad asked me to make him something for his work. He has a text file and on every fourth line there is a 10 digit number somewhere. How do i make a list of just the numbers? I assume the file looks something like this:

Random junk
Random junk fake number 1234567809
Random junk
My phone number is 1234567890 and it is here random numbers 32131;1231
Random junk
Random junk another fake number 2345432345
Random junk
Just kidding my phone number is here 1234567890 the date is mon:1231:31231

I assume its something like grep [1-9].\{9\} file but how do i get just lines 4,8,12 etc. Because i tested it and i get all phone numbers on every line. Also how do i get just the number not the whole line?

Any help will be greatly appreciated, even if its pointing me in the right direction so i can research it myself. Thanks.

like image 255
Kevin Avatar asked Apr 04 '14 15:04

Kevin


1 Answers

You can do it in two steps:

$ awk '!(NR%4)' file | grep -Eo '[0-9]{10}'
1234567890
1234567890
  • awk '!(NR%4)' file prints those lines whose number is multiple of 4. It is the same as saying awk '(NR%4==0) {print}' file.
  • grep -Eo '[0-9]{10}' prints the numbers that appear on blocks of 10. Note that -o is for "just print the matches" and -E to use extended regular expressions.

Or also

$ awk '!(NR%4)' file | grep -Eo '[1-9][0-9]{9}' #check if first number is <>0
like image 157
fedorqui 'SO stop harming' Avatar answered Oct 31 '22 08:10

fedorqui 'SO stop harming'