LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-07-2013, 07:12 PM   #16
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751

Just for fun here's a Perl approach for OP and Daniel's qns
Code:
#!/usr/bin/perl -w
use strict;


my ($tmp, @arr );
@arr=('this is9the way44the world123456ends35 ',
    'not 54321 with a444444bang 42 but a9whimper',
    q{<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=145148&amp;playTogether=True',960,540,943437);return false;" title="">} );
for $tmp ( @arr)
{
#   print "tmp $tmp\n";
    $tmp =~ s/([0-9]{5,7})/$1/ ;
    print $1."\n";
}
o/p
Code:
 ./t.pl
123456
54321
145148
 
1 members found this post helpful.
Old 03-07-2013, 08:48 PM   #17
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Perhaps the file OP processes always has this format...
Code:
<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=145148&amp;playTogether=True',960,540,943437);return false;" title="">
<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=1451486&amp;playTogether=True',960,540,94343);return false;" title="">
<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=1451489&amp;playTogether=True',960,540,94343);return false;" title="">
<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=45148&amp;playTogether=True',960,540,94343);return false;" title="">
In that case, extracting the numeric string which follows asv4= is a simple task.
Code:
cut -d"=" -f4 $InFile |cut -d"&" -f1 >$OutFile
Daniel B. Martin
 
Old 03-07-2013, 10:02 PM   #18
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by danielbmartin View Post
I interpret the problem statement this way:
Extract the first numeric string in each line which is of length 5, 6, or 7.
(No reliance on an equals sign.)
Code:
gawk 'match($0, /[0-9]{5,7}/) { print(substr($0, RSTART, RLENGTH)) }' $InFile
Note: requires gawk version 4+ for the {n,m} regex operator.
 
1 members found this post helpful.
Old 03-08-2013, 04:38 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Note: requires gawk version 4+ for the {n,m} regex operator
Earlier versions can use --re-interval

@ Daniel - Not sure that the generic solution you are after is possible with grep as unlike something like sed you cannot tell grep to stop after the first match (that I know of)
 
1 members found this post helpful.
Old 03-08-2013, 01:32 PM   #20
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Code:
bash-4.2$ grep -Eo "[0-9]{5,7}.*" test|grep -Eo "^[0-9]{5,7}"
145148
1451486
1451489
45148
Code:
bash-4.2$ echo "this is9the way44the world123456ends35 
not 54321 with a444444bang 42 but a9whimper"|grep -Eo "[0-9]{5,7}.*"|grep -Eo "^[0-9]{5,7}"
123456
54321
 
1 members found this post helpful.
Old 03-08-2013, 10:01 PM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm... that works but I was taking the idea that it was to be a single grep command, ie a single sed could do it easily
 
  


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
[SOLVED] Bash Shell Script to check if a string has only alphabets and digits. aswani Programming 8 08-16-2012 09:49 AM
Perl - regular expression to match variable number of digits after string kcleveland Programming 4 12-08-2011 04:46 AM
[SOLVED] sed remove all occurrences in a string hattori.hanzo Linux - Newbie 5 11-22-2010 04:46 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
[SOLVED] regex match string from start to find unique combinations fukawi2 Programming 6 02-11-2010 05:32 PM

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

All times are GMT -5. The time now is 06:31 PM.

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