?
Solved

regular expression

Posted on 2004-10-08
11
Medium Priority
?
345 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 160 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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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