grep or sed. How to remove certain characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep or sed. How to remove certain characters
# 1  
Old 07-20-2010
grep or sed. How to remove certain characters

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)

Code:
718-555-5465
(718) 555-1234
718-555-5678

file(after edit)
Code:
5465
1234
5678

thanks a lot guys.

Last edited by radoulov; 07-20-2010 at 02:39 PM.. Reason: Code tags, please!
# 2  
Old 07-20-2010
Code:
sed 's/.*\(....\)/\1/' infile

This User Gave Thanks to radoulov For This Post:
# 3  
Old 07-20-2010
Quote:
Originally Posted by Sucio
the last 4 digits
Code:
cat data | sed 's/[- ()]*//g' | awk '{print substr($1,length($1)-4,4)}'

# 4  
Old 07-20-2010
MySQL

Code:
# grep -o "....$" myfile
5465
1234
5678

# 5  
Old 07-20-2010
Hammer & Screwdriver Using awk...

Code:
echo 123-456-7890 | gawk -F"[ -]" '{print $NF}'
7890

This User Gave Thanks to joeyg For This Post:
# 6  
Old 07-20-2010
Another one:
Code:
sed 's/.*-//' file

# 7  
Old 07-20-2010
Code:
# sed 's/[^[:digit:]]//g' myfile | cut -b7,8,9,10
5465
1234
5678

Code:
# cat myfile | tr -d '[:punct:]' | cut -b7,8,9,10
5465
5123
5678

Code:
# grep -o "[[:digit:]]\{4\}$" myfile
5465
1234
5678

Code:
 
# sed "s/.*[^[:digit:]\{4\}$]//g" myfile
5465
1234
5678

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep to remove and add specified characters

I have the following type of 2 column file: motility - role - supplementation - age b ancestry b purity b recommendation b serenity b unease b carving f expansion f I would like to print only certain sections of the file depending on the value of the second column. For instance,... (6 Replies)
Discussion started by: owwow14
6 Replies

2. Shell Programming and Scripting

Grep to remove non-ASCII characters

I have been having an encoding problem that I need to solve. I have an 4-column tab-separated file: I need to remove all of the lines that contain the string 'vis-à-vis' achiever-n vis-à-vis+ns-j+vp oppose-v 1 achiever-n vis-à-vis+ns-the+vg assess-v 1 administrator-n ... (4 Replies)
Discussion started by: owwow14
4 Replies

3. Shell Programming and Scripting

How does this sed expression to remove non-alpha characters work?

Hello! I know that this expression gets rid of non-alphanumeric characters: sed 's///g' and I understand that it is replacing them with nothing - hence the '//'-, but I don't understand how it's doing it. It seems it's finding strings that begin with alphanumeric and replacing them with... (2 Replies)
Discussion started by: bgnersoon2be#1
2 Replies

4. Shell Programming and Scripting

Sed - remove special characters

Hi, I have a file with this line, it's always in the first line: I want to remove these special characters: ´╗┐ file1 ´╗┐\\bar\c$\test2\;3.348.118 Bytes;160 ;3 \\bar\c$\test\;35 Bytes;2 ;1 I want the same file to be only \\bar\c$\test2\;3.348.118 Bytes;160 ;3 \\bar\c$\test\;35... (4 Replies)
Discussion started by: nakaedu
4 Replies

5. Shell Programming and Scripting

Remove the Characters '[' and ']' with Sed

Hi, I am new to Sed and would like to know if it is possible to remove the characters . I have a couple of files with a keyword and would like to remove the substring. I am Using sed s/// but Its not working Thanks for your Support Andrew Borg (2 Replies)
Discussion started by: andrewborg
2 Replies

6. Shell Programming and Scripting

sed or tr to remove specific group of special characters

Hi, I have a input of the form: ..., word1, word2, word3... I want out put of the form word1, word2, word3 I tried echo '..., word1, word2, word3...' | tr -d '...,' but that takes out the commas in the middle too so I get word1 word2 word3 but I want the commas in the middle. ... (3 Replies)
Discussion started by: forumbaba
3 Replies

7. Shell Programming and Scripting

Sed or trim to remove non alphanumeric and alpha characters?

Hi All, I am new to Unix and trying to run some scripting on a linux box. I am trying to remove the non alphanumeric characters and alpha characters from the following line. <measResults>883250 869.898 86432.4 809875.22 804609 60023 59715 </measResults> Desired output is: 883250... (6 Replies)
Discussion started by: jackma
6 Replies

8. UNIX for Dummies Questions & Answers

sed command to remove characters help!

I am trying to analyse a large file of sequencing data, example of first 10 lines below, @HWUSI-EAS656_0044_FC:7:1:2447:1039#GCAATT/1 GNCTATGGCTTGCCGGGCTCAGGGAAGACAATCATAGCCATGAAAATCATGGAAAAGATCAGAAAAACATTTCAA +HWUSI-EAS656_0044_FC:7:1:2447:1039#GCAATT/1... (1 Reply)
Discussion started by: Adeleh
1 Replies

9. Shell Programming and Scripting

sed: remove characters between and including 2 strings

I have the following line: 4/23/2010 0:00:38.000: Copying $$3MSYDDC02$I would like to use sed (or similiar) to remove everthing between and including $ that appears in the line so it ends up like this. 4/23/2010 0:00:38.000: Copying 3MSYDDC02I have been trying these but i'm really just... (5 Replies)
Discussion started by: jelloir
5 Replies

10. Shell Programming and Scripting

sed to remove last 2 characters of txt file

sed 's/^..//' file1.txt > file2.txt this will remove the first two characters of each line of a text file, what sed command will remove the last two characters? This is a similar post to my other....sry if I'm being lazy.... I need a file like this (same as last post) >cat file1.txt 10081551... (1 Reply)
Discussion started by: ajp7701
1 Replies
Login or Register to Ask a Question