LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 01-19-2010, 09:02 AM   #1
Alexr
LQ Newbie
 
Registered: Jan 2010
Posts: 7

Rep: Reputation: 0
Sed/awk/grep search for number string of variable length in text file


I need to search a text file for a string of numbers which are different lengths, and always are between number=" and " like:

number="1234567890"
number="22390"

I need to grab those numbers and pipe each one to a line in a file.

I've already tried something with awk and that didn't seem to work.

Thanks in advance.
 
Old 01-19-2010, 09:16 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
If there is only one number per line:

Code:
sed -r 's/.*number=\"([0-9]+)\".*/\1/'  filename > newfilename
 
Old 01-19-2010, 10:13 AM   #3
Alexr
LQ Newbie
 
Registered: Jan 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks pixellany, but I think I should mention that the file it is reading from will be a dynamic html page and as an unfortunate result the placement of number="$numberstring" will be between all sorts of other tags.

Cheers anyway
 
Old 01-19-2010, 11:45 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
I think my code will work......I said "one number per line"---that does not preclude other junk being on the line.
 
Old 01-19-2010, 12:12 PM   #5
Alexr
LQ Newbie
 
Registered: Jan 2010
Posts: 7

Original Poster
Rep: Reputation: 0
I found a way of doing nearly precisely what I wanted. Pretty ugly, but I've no idea what I'm doing generally - I'm pretty new to BASH and coding generally.

grep -o number="....................." filename | tr -d [:alpha:][unct:] > newfilename
 
Old 01-19-2010, 12:15 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
No more ugly than mine.....

Please use code tags---e.g it will stop thing from being read as smilies---and it preserves formatting.
 
Old 01-19-2010, 12:24 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Ummm .. that's ugly. Plus it will only work if what's in the
quotes is exactly 22 characters long.Pixellanys solution is
much cleaner & WILL work.
 
Old 01-19-2010, 12:34 PM   #8
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 547

Rep: Reputation: 115Reputation: 115
sed stands for Stream Editor for a reason....
 
Old 01-19-2010, 01:13 PM   #9
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,212

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
heres my stab at it:
Code:
grep -o number.*" " index.html | awk -F = '{print $2}' > numbers.txt
 
Old 01-19-2010, 01:20 PM   #10
Alexr
LQ Newbie
 
Registered: Jan 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Tinkster: It does work if the numberstring is less than 22, which it will be 99% of the time.

You have a point though, and I'm going to try pixellany's again, for cleanliness' sake.

pixellany: sorry about the smiley, I'll remember in future.

Schneidz: I'll give it a go.

Thanks all.
 
Old 01-19-2010, 02:34 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by Alexr View Post
Tinkster: It does work if the numberstring is less than 22, which it will be 99% of the time.
That's odd .... what your regex says is:

Code:
Show all consecutive strings that begin with
number=", followed by 22 of anything, followed
by ".
So if your number is shorter than the 22 and it
still shows up you should be very lucky to have
a quote in the 23rd position, otherwise it shouldn't
match the pattern.

And in fact, your code piece doesn't work on the
sample data you provided further up in the thread.
pixellanys does - not sure whether you really want
the bare number or not, but that's what it returns.


Cheers,
Tink
 
  


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
[Grep,Awk,Sed]Parsing text between XML tags. ////// Programming 5 07-26-2011 12:54 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 09:04 PM
Sed, Awk, grep,Search,delete joyds219 Linux - Newbie 6 04-03-2008 07:15 AM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 06:28 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 01:34 AM


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