I have a project, the goal it is to extract phone numbers of a data.txt file, so I did a program. Like that :
grep -E '^[ ]{0,9}([0-9]{3}) [0-9]{3}-[0-9]{4}[ ]{0,9}$' $1 | sed 's/^[ t]*//' > all-result-phonenumber-filter.txt count=$(wc -l all-result-phonenumber-filter.txt) echo "The number of line is :$count" >> all-result-phonenumber-filter.txt
My problem is when I want to use this program and to execute on my terminal I had to use pipe for my terminal command. I tried many different commands on my terminal and the last one is :
cat data.txt | ./all-phone-number-filter.sh | cat all-result-phonenumber-filter.txt
But this command doesn’t work, and I don’t know why. So what is the correct command I had to use with this above format please?
I had to use the following format SDTIN | STDOUT for the pipe.
I give you the data.txt file :
(512) 258-6589 (205) 251-6584 (480) 589-9856 (303) 548-9874 (808) 547-3215 (270) 987-6547 (225) 258-9887 (314) 225-2543 (979) 547-6854 (276) 225-6985 les numeros suivants ne sont pas valables pour ce programme : +512 325 +512 251 2545654654 +512 6546 6464646 +512546546646564646463313 (314) sgf225-2543 (314) 225-2543fsgaf (314afd) 225-2543 FSd(314) 225-2543
The result I need to have is :
(512) 258-6589 (205) 251-6584 (480) 589-9856 (303) 548-9874 (808) 547-3215 (270) 987-6547 (225) 258-9887 (314) 225-2543 (979) 547-6854 (276) 225-6985 The number of line is :10 all-result-phonenumber-filter.txt
Advertisement
Answer
A filter will:
- Read from stdin
- Write to stdout
Your program will:
- Read from stdin
- Write to a hard-coded filename
So your program is not a filter and can not be used similarly to what you want.
The easiest/worst way of turning your program into a filter is to write to a temporary file instead, and then cat
that:
#!/bin/sh # TODO: Rewrite to be a nicer filter that doesn't write to files tmp=$(mktemp) grep -E '^[ ]{0,9}([0-9]{3}) [0-9]{3}-[0-9]{4}[ ]{0,9}$' $1 | sed 's/^[ t]*//' > "$tmp" count=$(wc -l < "$tmp") echo "The number of line is :$count" >> "$tmp" cat "$tmp" rm "$tmp"
Now you can treat it as a filter:
$ cat data.txt | ./all-phone-number-filter.sh | tail -n 3 (979) 547-6854 (276) 225-6985 The number of line is :10 $ cat data.txt | ./all-phone-number-filter.sh > myfile.txt (no output) $ nl myfile.txt | tail -n 5 7 (225) 258-9887 8 (314) 225-2543 9 (979) 547-6854 10 (276) 225-6985 11 The number of line is :10