Using grep to select specific patterns in text file?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using grep to select specific patterns in text file?
# 1  
Old 09-30-2016
Question Using grep to select specific patterns in text file?

How do I use grep to select words that start with I or O, end in box, and contain at least one letter in between them?

the text file mailinfo.txt contains

Code:
Inbox
the Inbox
Is a match box
Doesn't match
INBOX
Outbox
Outbox1
InbOX
Ibox
I box

If the command works correctly it should select
Code:
Inbox
Is a match box
Outbox

Here's what I've been trying:

Code:
grep -a  '[I|O]''[a-z].\+''box\>' mailinfo.txt

And I only get:
Code:
Is a match box
Outbox

Why doesn't it retrieve "Inbox" as well? And how to I make it?

Thanks

Last edited by rbatte1; 10-03-2016 at 05:45 AM.. Reason: Addtional code tags
# 2  
Old 09-30-2016
with sed:
Code:
sed -n '/^[IO].*[a-zA-Z].*box$/p' mailinfo.txt


Last edited by vgersh99; 09-30-2016 at 04:29 PM.. Reason: harderned regex
# 3  
Old 09-30-2016
Hello steezuschrist96,

Welcome to forums, hope you will enjoy learning and sharing knowledge here with us, could you please try following and let us know how it goes then.
Code:
awk '($0 ~ /^[IO]/ && $0 ~ /box$/ && $0 ~ /[IO].*[a-zA-Z].*box/)'  Input_file
OR more precisely
awk '($0 ~ /^[IO].*[a-zA-Z].*box$/)'   Input_file

Output will be as follows.
Code:
Inbox
Is a match box
Outbox

EDIT: You could use simple greptoo, I had tested this in GNU grep.
Code:
grep "^[IO].*[a-zA-Z].*box$"   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-30-2016 at 04:41 PM.. Reason: Added one more solution successfully too now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 09-30-2016
Yes! That worked, thank you!
# 5  
Old 09-30-2016
Note: it is best to use character classes rather than character ranges. This will match all letters, not just the 2x26. Also, this avoid issues with collation order (for example in some locales [A-Z] might include lower case letters):
Code:
grep '^[IO].*[[:alpha:]].*box$'

For example:
Code:
$ echo 'Iåbox' | grep '^[IO].*[a-zA-Z].*box$' 
$ echo 'Iåbox' | grep '^[IO].*[[:alpha:]].*box$' 
Iåbox


Last edited by Scrutinizer; 10-01-2016 at 02:34 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. Shell Programming and Scripting

Bash to select panel then specific file in directory

I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used. while true; do read -p "Do you want to get coverage of a specific panel?" yn case $yn in * ) menu; break;; * ) exit;; * ) echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

4. UNIX for Dummies Questions & Answers

Using grep to select data from file

Hello all, Once again I need to call upon the masters for help. I have a file called endpoint_data. IN that file I have groups of endpoints. However all I need from the file is this.... ENDPOINT NAME = SIPWOODSBC SIG PRI FQDN/IP ADDRESS/DOMAIN NAME = 10.xxx.xxx.xxx SIG SEC... (4 Replies)
Discussion started by: jay11789
4 Replies

5. Shell Programming and Scripting

Using perl to grep a list of patterns from an input file

I have been struggling to grep a file of NGrams (basically clusters of consonants or Consonant and Vowel) acting as a pattern file from an Input file which contains a long list of words, one word per line. The script would do two things: Firstly read a text pattern from a large file of such... (5 Replies)
Discussion started by: gimley
5 Replies

6. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

7. UNIX for Dummies Questions & Answers

Locating and Extracting Specific Patterns from a file

Hi all, 1. I have a file that is getting continously refreshed (appended) I want to grep all the strings containing substring of the type abcdf123@aaa.xxx.yyy.zzz:portnumber: where, before @, any letters or numbers combination, after @, IP address then symbol : then port... (4 Replies)
Discussion started by: kokoras
4 Replies

8. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

9. Shell Programming and Scripting

shell script to format file based on specific patterns

Please help me out and drag me out the deadlock I am stuck into: I have a file. I want the statements under a if...then condition be listed in a separate file in the manner condition|statement.Following are the different input pattern and corresponding output parameters.any generic code to... (7 Replies)
Discussion started by: joyan321
7 Replies

10. Shell Programming and Scripting

grep patterns - File

Hi I have 3 patterns for example to be searched. These three patterns are available in file1. The patterns to be searched are in file2. I want to search the pattern of file1 to file2. Can any one help with example? Regards Dhana (1 Reply)
Discussion started by: dhanamurthy
1 Replies
Login or Register to Ask a Question