Solved

Need help with grep command pattern

Posted on 2013-07-01
2
384 Views
Last Modified: 2013-07-01
I have an input file that consists of many lines, each divided on 3 parts by colon:

test:wwwww:A
test2:nnnnnn:B
TEST3:qqqqqq:A
*test4:wwwww:C

I need to retrieve those lines that start with an alphanumeric character either upper case or lowercase and might have a digit anywhere in the middle of the string or at the end, and those that have its thord part equal to A. The line cannot start with a digit, *, # or any other character besides letters

For exaple from the sample lines above my code would need to return:

test:wwwww:A
TEST3:qqqqqq:A

Here is what I got:

grep "^[a-zA-Z]*:.*:A" foo | while read LINE
do
echo $LINE
done

Open in new window


But it returns only:

test:wwwww:A

grep "^[a-zA-Z][0-9]:.*:A" foo | while read LINE
do
echo $LINE
done

Open in new window


Does not return anything.

grep "^[a-zA-Z]*[0-9]:.*:A" foo | while read LINE
do
echo $LINE
done

Open in new window


only returns

TEST3:qqqqqq:A

and it would ignore any line with a digit in the middle of a string:
TE3T:qqqqqq:A



Can anyone help to fix my pattern?
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 250 total points
ID: 39290901
So close...  :)

grep "^[a-zA-Z][a-zA-Z0-9]*:.*:A" foo | while read LINE
do
echo $LINE
done
                                 
Try that one.  After the initial alpha, any alphanumeric should be allowed.


Kent
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 39290963
You nailed it!:)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question