Solved

regular expression

Posted on 2004-10-08
11
344 Views
Last Modified: 2010-04-20
I have a file

My File
----
This is me, yeah
My number is 12 3
Hello world
hi are you ok?
I am king
no problem.


I like to have a grep regular expression that will give me lines with a-z, A-Z, commas, and blanks. That's my output should be

This is me, yeah
Hello world
I am king


cat myfile | grep ???????
0
Comment
Question by:cutie2000
[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
  • 6
  • 3
  • 2
11 Comments
 
LVL 10

Expert Comment

by:Luxana
ID: 12257834
cat myfile | grep [.,H]
0
 

Author Comment

by:cutie2000
ID: 12257859
thanks for replying but then it's not what I wanted.
I need to give me lines with a-z, A-Z, commas, or blanks. Any other characters will be ignored. Sorry for the confusion
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12257917
>a-z, A-Z, commas, or blanks

this expresion will match every lines uper and lower case also , . and then your output will be all lines . Because a-z A-Z mach every letter and in your input we have letters on each line then the output will look like this:

cat myfile | grep [.,a-zA-Z]
This is me, yeah
My number is 12 3
Hello world
hi are you ok?
I am king
no problem.


expresion a-z and A-Z match each line !!!

0
Stressed Out?

Watch some penguins on the livecam!

 
LVL 10

Expert Comment

by:Luxana
ID: 12257970
grep [.,H] myfile
This is me, yeah
Hello world
no problem.

grep reads each line in file and if your expresion match to line it will be printed out.

even space is on each line:

grep [" "] myfile
This is me, yeah
My number is 12 3
Hello world
hi are you ok?
I am king
no problem.
0
 

Author Comment

by:cutie2000
ID: 12258132
it won't work. i need not only accept space.
i need to accept space, alphabets and comma
lines with illegal characters will not be printed
hope it's clear
0
 
LVL 2

Expert Comment

by:Matt_Avery
ID: 12258342

grep '^[a-z,A-Z,0-9,\.\,]*$'


(match any string where every character ("*") between the start of the string ("^") and the end ("$") is alphanumeric, space, comma or fullstop ([a-z,A-Z,0-9\.\,])
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12258385
>i need to accept space, alphabets and comma

this match every line in file, so all of them will be printed out.
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12258453
to Matt:

grep '^[a-z,A-Z,0-9,\.\,]*$'

this mach all lines which starts with a-z A-Z number . or ,

you wanted probably:

grep '[a-z,A-Z,0-9,\.\,]'
0
 
LVL 2

Accepted Solution

by:
Matt_Avery earned 40 total points
ID: 12258454

Silly me - question said "A-z,a-z, commas and blanks"

So I should have said:

grep '^[a-z,A-Z, \,]*$'
0
 
LVL 2

Expert Comment

by:Matt_Avery
ID: 12258629
Luxana,

The original poster wanted a RE to match all lines that contain *only* characters from a certain set.

That's why I'm using '^[xyz]*$'  instead of just '[xyz]'

'[xyz]' would match all lines that contain *any* characters from a certain set.

 
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12258697
matt

I understand that but I been confused with question:))

>I like to have a grep regular expression that will give me lines with a-z, A-Z, commas, and blanks.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

707 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