LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 08: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, 08:16 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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

Code:
sed -r 's/.*number=\"([0-9]+)\".*/\1/'  filename > newfilename
 
Old 01-19-2010, 09: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, 10:45 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
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, 11:12 AM   #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, 11:15 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 11:24 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
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, 11:34 AM   #8
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
sed stands for Stream Editor for a reason....
 
Old 01-19-2010, 12:13 PM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,309

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
heres my stab at it:
Code:
grep -o number.*" " index.html | awk -F = '{print $2}' > numbers.txt
 
Old 01-19-2010, 12: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, 01:34 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
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 11:54 AM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
Sed, Awk, grep,Search,delete joyds219 Linux - Newbie 6 04-03-2008 06:15 AM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 05:28 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:33 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