Find zipcode with Grep with optional space in middle


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find zipcode with Grep with optional space in middle
# 1  
Old 09-25-2010
Bug Find zipcode with Grep with optional space in middle

Hello everyone. I am new to unix, nice to meet you all.
I have a small problem with a question.
I have to write a command that finds all the postal codes in a txt file. All postal codes will begin on its own line in the txt file.
This is the formal of postal code: "A#A#A#" or "A#A #A#"
A being a letter and # being a number. Letters D F I O Q and U are restricted.
My problem is how to reconize the optional space in the middle. Here is what I have so far:
grep "^[WERTYPASGHJKLZXCVBNM][1-9][WERTYPASGHJKLZXCVBNM]{1-9,:space:1-9}[WERTYPASGHJKLZXCVBNM][1-9]" address.txt

The red part being the wrong part, could anyone help? thank you so much.
# 2  
Old 09-25-2010
Code:
$ 
$ 
$ cat -n f14
     1    A1B2C3
     2    D4E 5F6
     3    G7H8I9
     4    J0K 1L2
     5    M3N4O5
     6    P6Q 7R8
     7    S9T0U1
     8    V2W 3X4
     9    Y5Z6A7
$ 
$ x="[ABCEGHJKLMNPRSTVWXYZ]"
$ 
$ grep "^$x[0-9]$x[ ]*[0-9]$x[0-9]$" f14
A1B2C3
J0K 1L2
V2W 3X4
Y5Z6A7
$ 
$ 

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 09-25-2010
Quote:
Originally Posted by leonmerc
[...] I have so far:
grep "^[WERTYPASGHJKLZXCVBNM][1-9][WERTYPASGHJKLZXCVBNM]{1-9,:space:1-9}[WERTYPASGHJKLZXCVBNM][1-9]" address.txt

The red part being the wrong part, could anyone help? thank you so much.
grep -E "^[WERTYPASGHJKLZXCVBNM][1-9][WERTYPASGHJKLZXCVBNM][[:space:]]?[1-9][WERTYPASGHJKLZXCVBNM][1-9]" address.txt

You were going well. The [[:space:]] can be substituted for just a literal space or a [ ] in your case.

Last edited by Aia; 09-25-2010 at 11:43 PM..
This User Gave Thanks to Aia For This Post:
# 4  
Old 09-26-2010
thanks fight club and Aia. With your help i got this question done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

2. Shell Programming and Scripting

Zipcode + 4 change to Zipcode

I'm doing some integrity checeking. I Have found if the file contains an "invalid" entry (Zipcode +4). What I'm looking to do is modify the data so that it's just the 5 digit zipcode. I found a couple of helpful posts, but nothing that I could apply to my situation. Here's a sample input (note... (6 Replies)
Discussion started by: defjab
6 Replies

3. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

4. Shell Programming and Scripting

Find the middle character from a string using sed

Input: qwertmyuiop It should print "m". What's the command to do this with sed? (6 Replies)
Discussion started by: cola
6 Replies

5. UNIX for Dummies Questions & Answers

grep until a space

This is probably common knowledge, but I am a beginner at bioinformatics! I want a list of my sequence names, without sequence or any of the free text after the space. This is some example file: >cow|68989024|dbj|AB107582.1| Uncultured bacterium gene for 16S rRNA, partial se quence, clone:... (7 Replies)
Discussion started by: kkohl78
7 Replies

6. Shell Programming and Scripting

grep middle word between two patterns

Hi, I'm currently working on a shell script to automate a backup check on oracle database. My requirement is to grep the words between two delimiters and pass on to a variable.. for ex I have following values in my log file... (DB_NAME), (163.24 25), (16/02/10 23:40), (COMPLETED), I want... (5 Replies)
Discussion started by: senthil3d
5 Replies

7. Shell Programming and Scripting

How to grep space?

Hi, I want to know how to grep space. Eg: input file contains 1565432 432 546 3514432 432 545 6541332 432 354 5464412 565 355 8456145 546 534 5632465 432 654 From this i need output of the lines which has 432 which are bold. My requirement is whenever 432 comes... (3 Replies)
Discussion started by: rakeshbharadwaj
3 Replies

8. Shell Programming and Scripting

grep with regular expression optional value

field 12345 12345 field 12345 field 12345 123456 last fleld (from three) is optional, but if it occures It has to be composed only of nummbers and maximum 5 positions long. If I use: grep "^field \{5\}" I get: field 12345 12345 field 12345 field 12345 123456 But I wont... (11 Replies)
Discussion started by: necroman08
11 Replies

9. UNIX for Dummies Questions & Answers

Is it possible to grep with a newline in the middle of an expression?

I'm hoping that there is a way to do this. I'm sure I won't be able to get the answer I need on the deadline I need it...but at least I'll learn how to solve this problem. I have a file that looks like this: (00:14:25\$ head -27 QNHDSPACEDVR Name: PollDhctAVFSInfo 00:0F:21:4B:00:6A Name:... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

Grep with wildcard in middle of word

How can grep G.*schema give me the result: ${Gacntg_dt}""'"' doesn't G.*schema say give me an unlimited number of characters between G and schema? :confused: (3 Replies)
Discussion started by: danmauer
3 Replies
Login or Register to Ask a Question