Solved

regular expression

Posted on 2004-10-08
11
340 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
Comment Utility
cat myfile | grep [.,H]
0
 

Author Comment

by:cutie2000
Comment Utility
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
Comment Utility
>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
 
LVL 10

Expert Comment

by:Luxana
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Expert Comment

by:Matt_Avery
Comment Utility

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
Comment Utility
>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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now