Computing.Net > Forums > Unix > Shell script with regular expressio

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Shell script with regular expressio

Reply to Message Icon

Name: terry
Date: December 25, 2002 at 22:46:27 Pacific
OS: unix
CPU/Ram: -
Comment:

Hi all,
I have a problem about developing shell
script to extract words with regular expression.
For example, I have one text file that contain sth below....

This is a pen.I have a car.My name is John
I have no pencil but I can find for you.
aaaaaa bbb pkkkkkkkdcils lss

I want my word in this text file that begin with
"p" and end with "cil" (p*cil)
in this text file you can find
pencil in line 2 and
pkkkkkkkdcil in line 3

that i can do with grep....
but there is a problem that I want only word
i want pencil and pkkkkkkkdcil only.

Thanks ,
Terry




Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: December 26, 2002 at 06:55:06 Pacific
Reply:

Since grep outputs entire lines that contain the target expression, and you want individual words, we can first convert the file to one word per line:

fmt -1 myfile | grep ^p.*encil$

The above will print words that begin with p and end with cil. The last line of your test file contains the word pkkkkkkkdcils which would not qualify since it ends with cils and not cil.


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Shell script with regular expressio

use variable of shell script in awk www.computing.net/answers/unix/use-variable-of-shell-script-in-awk/4570.html

shell script help! www.computing.net/answers/unix/shell-script-help-/4809.html

help with shell script www.computing.net/answers/unix/help-with-shell-script/7840.html