Solved

regular expression

Posted on 2004-10-08
11
342 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get maximum transfer speed over LAN 4 106
capture pcap with filtered traffic 1 86
awk file 6 79
The better OS Architecture 13 68
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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 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.

830 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