How would I look for files in the /etc directory (but not subdirectories) that contain a standard United States long distance phone number, written using the pattern of 1-###-###-####, where each # is replaced with a numeric digit. Collect the filenames of every file in the /etc directory which contains such a pattern of numbers, and place them in the file ~/etcphone.txt, one file name per line, sorted alphabetically, using absolute references.

This is what I have so far

egrep -l "1-[[:digit:]]{3}- " /etc/* 2>/dev/null 

4 Answers 4

$ egrep -l "\b1-[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}\b" \
    /etc/* 2>/dev/null | sort > ~/etcphone.txt

The \b escape sequence matches a word boundary. This will prevent if from matching something like 1231-123-123-1234.

  • Don't I have to sort it as well
    – user72510
    Jul 11, 2014 at 20:12
  • Yes. I thought your question was just about the regular expression.
    – Barmar
    Jul 11, 2014 at 20:15
  • I've added the trivial sort and output redirection to the answer.
    – Barmar
    Jul 11, 2014 at 20:17
  • So I would write it down egrep -l "\b1-[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}\b" /etc/* 2>/dev/null | sort >> ~/etcphone.txt
    – user72510
    Jul 11, 2014 at 20:19
  • I don't understand what you're asking there. Other than using >> instead of > it looks the same as my answer. There's nothing in your question saying that the results should be appended instead of overwriting the file.
    – Barmar
    Jul 11, 2014 at 20:22

egrep -l "1(-[[:digit:]]{3}){3}[[:digit:]]" /etc/* 2>/dev/null

find /etc | xargs -I{} grep -l ‘1-[0-9]\{3\}-[0-9]\{3\}-[0-9]\{4\}’ {} | sort -nr > ~/etcphone.txt
cat filename.txt | grep '^[789][0-9]{9}'

This command is applicable only for 10 digit Indian mobile numbers starting with 7,8 or 9.

  • The OP asks to parse US phones numbers in multiple files in `/etc'. How does this answer that question? Apr 10, 2017 at 4:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .