LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX
User Name
Password
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.

Notices


Reply
  Search this Thread
Old 10-06-2009, 03:29 AM   #1
quanba
Member
 
Registered: Jul 2009
Posts: 41

Rep: Reputation: 15
awk: What pattern has the same meaning with egrep -w ?


Hi there,

when I was processing text, I got the problem:

1: test
2: adsf;test;dfas
3: asdf test asdfa
4: asdf-test-bcd
5: adb,test,add
6: atestabc

I can grep the word "test" with egrep -w, but I want to process the text with awk, what pattern do I need?

thanks in advance.
 
Old 10-06-2009, 06:19 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
From 'man awk':

Quote:
\y matches the empty string at either the beginning or the end of a word.
\B matches the empty string within a word.
\< matches the empty string at the beginning of a word.
\> matches the empty string at the end of a word.
\w matches any word-constituent character (letter, digit, or underscore).
\W matches any character that is not word-constituent.
So to match the word test, you would use:
Code:
awk '/\<test\>/'
 
Old 10-06-2009, 06:28 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by quanba View Post
I can grep the word "test" with egrep -w, but I want to process the text with awk,
It would help to say what you actually need to do, and why you need to use AWK. For simple search and replace, I would use SED.

The "pattern" you refer to is typically called a "Regular Expression" (Regex), and is using in many utilities.
 
Old 10-06-2009, 06:40 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
\y matches the empty string at either the beginning or the end of a word.
\B matches the empty string within a word.
\< matches the empty string at the beginning of a word.
\> matches the empty string at the end of a word.
\w matches any word-constituent character (letter, digit, or underscore).
\W matches any character that is not word-constituent.
Interesting---I had never seen some of these. They all work in SED except "\y"......
 
Old 10-06-2009, 03:04 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by quanba View Post
Hi there,

when I was processing text, I got the problem:

1: test
2: adsf;test;dfas
3: asdf test asdfa
4: asdf-test-bcd
5: adb,test,add
6: atestabc

I can grep the word "test" with egrep -w, but I want to process the text with awk, what pattern do I need?

thanks in advance.
From that request it's not quite clear what you're
after. If you just need to find lines that match the
word test, then /<test>/ will print those lines.

If you want to operate on the matched string alone,
that's not going to help you.

You'd be looking at something somewhat more complex.
Code:
'{res=gensub(/.*(\<test\>).*/, "\\1","g"); if(res!=$0){print res}}'

Btw, line 6 in your sample won't be matched by either the
egrep or the awk ...

Last edited by Tinkster; 10-06-2009 at 03:09 PM.
 
Old 10-11-2009, 11:35 PM   #6
quanba
Member
 
Registered: Jul 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks for your supports. But I was missing information from the title. I am practicing on HPUX and none of the patterns works.

Have anyone experience the problem on HPUX?
 
Old 10-12-2009, 12:22 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by quanba View Post
Have anyone experience the problem on HPUX?
Have you checked the man pages and other documentation?

I'm moving this thread since it is not a Linux question......good luck.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printing like xls pattern using awk bharatbsharma Programming 6 09-12-2008 04:07 AM
:newbish: hacking a nautilus script (egrep, awk) gub Linux - Software 1 02-03-2008 08:04 AM
How to get the pattern using sed or awk? ahpin Programming 3 08-02-2007 03:16 AM
diffrence between grep, sed, awk and egrep Fond_of_Opensource Linux - Newbie 3 08-18-2006 08:15 AM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX

All times are GMT -5. The time now is 10:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration