If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > grep pattern

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-13-03, 08:45
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
Exclamation grep pattern

hi, me again with prob guru.... i have a .txt file with numeric and words together eg . hello 1
2 how
are 3 you

which pattern do u used to get just the numeric ? eg 1 2 3
i tried many pattern .... grep [0-9] test.txt , i tried from * to ?
can u help me guru
Reply With Quote
  #2 (permalink)  
Old 10-13-03, 08:51
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Are you wanting to edit the input line to produce just the numeric portion? If so, grep is not the tool to use. Try sed...

sed 's/[^0-9]//g'

This will substitute every character NOT (that's the ^) in [0-9] for an empty string.

HTH
Reply With Quote
  #3 (permalink)  
Old 10-13-03, 08:54
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
yes, i am trying to get the numeric in the text file and sum it up ... thanx guru, never thought sed can do that
Reply With Quote
  #4 (permalink)  
Old 10-13-03, 08:58
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
guru, just say that one of the words in the txt is "world4" but i dont want the 4 to be part of the output, what pattern can be used for it ?
Reply With Quote
  #5 (permalink)  
Old 10-13-03, 09:11
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
This would work for that specific example...

sed 's/[a-Z][a-Z]*[0-9]*//g'
Reply With Quote
  #6 (permalink)  
Old 10-13-03, 09:15
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
the sed for that is not working
i have error : sed: -e expression #1, char 22: Invalid range end

btw, can u explain briefly ? i can't seem to understand why twice the [a-Z]

edit : my txt consist of hello 1
2 how
are 3 you
today4

so my output should be 1 2 3 only ... can't seem to ge the correct pattern....

Last edited by jinroh; 10-13-03 at 09:18.
Reply With Quote
  #7 (permalink)  
Old 10-13-03, 09:16
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Different version of sed!

Replace a-Z with a-zA-Z.
Reply With Quote
  #8 (permalink)  
Old 10-13-03, 09:19
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally posted by jinroh

btw, can u explain briefly ? i can't seem to understand why twice the [a-Z]
The * matches 0 or more occurences, you want at least one.

The following are all the same but I wouldn';t expect them all to work on all versions of sed...

[a-z][a-z]*
[a-z]+
[a-z]\{1,\}
Reply With Quote
  #9 (permalink)  
Old 10-13-03, 09:21
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
thanx , this sed 's/[a-z][A-Z]*[0-9]*//g' test.txt works ... but if you dont mind, can plz explain a bit ? i really want to learn , thanx guru
Reply With Quote
  #10 (permalink)  
Old 10-13-03, 09:26
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally posted by jinroh
thanx , this sed 's/[a-z][A-Z]*[0-9]*//g' test.txt works ... but if you dont mind, can plz explain a bit ? i really want to learn , thanx guru
substitute/this/forThis/globally

The 'this' pattern breaks down as...

1 character in the range a-Z followed by zero or more characters in the range a-Z followed by zero or more characters in the range 0-9.

This would match:

world4
world
world44

but not...

world 4
4


HTH
Reply With Quote
  #11 (permalink)  
Old 10-13-03, 09:30
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
thanx guru for helping me learn one or more thing
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On