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

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Shell script with regular expressio

Reply to Message Icon

Original Message
Name: terry
Date: December 25, 2002 at 22:46:27 Pacific
Subject: Shell script with regular expressio
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



Report Offensive Message For Removal


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

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.


Report Offensive Follow Up For Removal







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








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software