Sucio
July 20, 2010, 1:37pm
1
Here is my problem.
I have a list of phone numbers that I want to use only the last 4 digits as PINs for something I am working on. I have all the numbers in a file but now I want to be removed all items EXCEPT the last 4 digits.
I have seen sed commands and some grep commands but I am having some trouble.
Here is what I want to do.
file(before edit)
718-555-5465
(718) 555-1234
718-555-5678
file(after edit)
5465
1234
5678
thanks a lot guys.
sed 's/.*\(....\)/\1/' infile
1 Like
sucio:
the last 4 digits
cat data | sed 's/[- ()]*//g' | awk '{print substr($1,length($1)-4,4)}'
# grep -o "....$" myfile
5465
1234
5678
joeyg
July 20, 2010, 3:21pm
5
echo 123-456-7890 | gawk -F"[ -]" '{print $NF}'
7890
1 Like
# sed 's/[^[:digit:]]//g' myfile | cut -b7,8,9,10
5465
1234
5678
# cat myfile | tr -d '[:punct:]' | cut -b7,8,9,10
5465
5123
5678
# grep -o "[[:digit:]]\{4\}$" myfile
5465
1234
5678
# sed "s/.*[^[:digit:]\{4\}$]//g" myfile
5465
1234
5678
Sucio
July 27, 2010, 11:57am
8
Thanks for the help everyone. I got this part done. Lets see how I do with my next mission.
Jairaj
July 27, 2010, 1:22pm
9
Try this also.
echo 123-456-7890 | rev | cut -c 1-4| rev
Not the most efficient solution.
kurumi
July 27, 2010, 9:20pm
11
!/bin/bash
while read -r LINE
do
echo "${LINE:(-4)}"
done <"file"
1 Like