LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-21-2009, 05:50 PM   #16
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,014

Rep: Reputation: 2884Reputation: 2884Reputation: 2884Reputation: 2884Reputation: 2884Reputation: 2884Reputation: 2884Reputation: 2884Reputation: 2884Reputation: 2884Reputation: 2884

As I said above, grep is the better option. Else see the manpage for sed - "sed -n ..." and print any matches.
 
Old 12-21-2009, 06:29 PM   #17
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by warrentaylor View Post
I am having the same problem....sort of. I want to extract a combination of character if they exist. If they don't exist, I want nothing. My problem is that if my pattern doesn't exist, I get the whole line returned.

if I have .....AA9999999999999999....., I want AA9999999999999999
if I have ............................, I want nothing.

where AA9999999999999999 is 2 capital alphas followed by 16 numerics.

I use 's/.*\(AA[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\).*/\1/'

because \{16\} as a repeater doesn't work.
show examples of your data what you want to get exactly.
 
Old 12-22-2009, 08:54 AM   #18
warrentaylor
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Rep: Reputation: 0
examples?

for the purpose of the question, these are examples. Of both the data and the regex. I will go off and try 'print' and maybe grep but I haven't yet found how to extract data using grep.
 
Old 12-22-2009, 09:09 AM   #19
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i mean show a better input sample file, and show the output you want clearly.
 
Old 12-22-2009, 10:31 AM   #20
warrentaylor
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Rep: Reputation: 0
OK, got it.

for ANY string

.......AA9999999999999999......

where '.' represents any character, I want to extract only the characters that fit the pattern AA followed by 16 numerics. Any digits in these positions is a match and the pattern could exist anywhere in the line. If this 'exact' pattern is not found then output nothing.

the above suggested solution actually worked for me:

'grep -o --only-matching "ZR[0-9]\{16\}"'

and I have what I want. So, thanks much.

Sorry for the confusion on the example.
 
Old 12-22-2009, 10:41 AM   #21
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Quote:
Originally Posted by warrentaylor View Post
if I have .....AA9999999999999999....., I want AA9999999999999999
if I have ............................, I want nothing.

where AA9999999999999999 is 2 capital alphas followed by 16 numerics.

I use 's/.*\(AA[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\).*/\1/'

because \{16\} as a repeater doesn't work.
Repeaters are extended regexp functions, meaning you have use "sed -r" or "grep -E/egrep". On the plus side, this also means you don't have to escape the parentheses or brackets. Also, it's recommended to use the posix matching classes for the standard ranges of characters. Either of the following should work:
Code:
sed -rn 's/.*(AA[[:digit:]]{16}).*/\1/p'

egrep -o 'AA[[:digit:]]{16}'
Note that there are a couple of weaknesses in the above, though they may or may not be a concern for you. First, it will match number strings of any length, but only print the first 16. Second, it will match any combination of numerals, meaning something like AA1234567890123456 will also match. I'm not sure what you'd need to do if you need to isolate only a single repeating number.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
extract substring from string in C baddah Programming 6 02-02-2010 04:22 AM
Sed/awk help with regular expressions needed AP81 Programming 3 07-28-2008 07:26 AM
Extract substring matching a regular expression tikit Linux - General 2 02-18-2008 01:47 PM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM
Sed and regular expressions tchernobog Linux - Software 2 08-14-2003 12:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration