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
Solved

Need help with grep command pattern

Posted on 2013-07-01
2
379 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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSH commands for Nas4free 21 460
LINUX ZIP - UNCOMPRESS WINDOWS PATH 3 104
Linux: disable vim auto-comment 7 136
Linux Scripting 3 144
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…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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