Extract Numbers from a log file | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Extract Numbers from a log file

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-26-2013
Sajjadmehdi Sajjadmehdi is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 11 March 2014, 8:34 PM EDT
Posts: 2
Thanks: 3
Thanked 0 Times in 0 Posts
Extract Numbers from a log file

Hi,

I am trying to grep/extract the number list from this log file, can I get some help on this. I can grep the word 'href' to see the numbers, but it is resulting with the complete line.

Content of my file:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Index of Directory/Temp</title>
</head>
<body>
<h1>Index of Directory/Temp</h1>
<pre>Name  Last modified      Size</pre>
<hr/>
<pre><a href="http://www.unix.com/">../</a>
<a href="129/"">129/</a>   22-Aug-2013 10:00    -
<a href="133/"">133/</a>   21-Aug-2013 14:04    -
<a href="134/"">134/</a>   21-Aug-2013 14:10    -
<a href="135/"">135/</a>   21-Aug-2013 14:12    -
<a href="136/"">136/</a>   21-Aug-2013 14:15    -
</pre>
<hr/>
<address style="font-size:small;"> Server at Test Port 80</address>
</body>
</html>

Expecting output is :

Code:
129
133
134
135
136


Last edited by Franklin52; 08-26-2013 at 04:36 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 08-26-2013
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 30 June 2014, 4:32 AM EDT
Location: Chennai, India
Posts: 443
Thanks: 22
Thanked 70 Times in 68 Posts
This solution is not so generic. Works fine if all the numbers are three digit numbers.


Code:
awk -F">" '/^<a href/{print substr($2,1,3)}' filename

Sponsored Links
    #3  
Old 08-26-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 28 November 2014, 7:27 AM EST
Location: Amsterdam
Posts: 9,624
Thanks: 294
Thanked 2,457 Times in 2,204 Posts
Try:

Code:
awk '/^a href/{sub("/",x,$2); print $2}' RS=\< FS=\> file


Code:
..
129
133
134
135
136

The Following User Says Thank You to Scrutinizer For This Useful Post:
Sajjadmehdi (09-03-2013)
    #4  
Old 08-26-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 28 November 2014, 3:27 AM EST
Location: India
Posts: 1,834
Thanks: 25
Thanked 526 Times in 505 Posts

Code:
awk -F'[</>]' '/a href="[0-9]/ {print $4}' file

Sponsored Links
    #5  
Old 08-26-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 28 November 2014, 7:14 AM EST
Location: Aachen, Germany
Posts: 4,718
Thanks: 80
Thanked 1,172 Times in 1,101 Posts
Which three-digit-number do you want, the first or the second?
Code:
sed -rn '/a href/ s/^[^"]*"([0-9]{3}).*$/\1/p' file
129
133
134
135
136

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract rows from file based on row numbers stored in another file shoaibjameel123 Shell Programming and Scripting 3 10-06-2011 05:03 AM
Need help please with Grep/Sed command to extract text and numbers from a file mnassiri Shell Programming and Scripting 14 04-03-2011 03:06 PM
Extract numbers from .txt file eggali UNIX for Dummies Questions & Answers 5 08-15-2010 07:01 PM
Extract numbers from text file work out average rich@ardz Shell Programming and Scripting 14 08-04-2010 08:20 AM
extract only the "numbers" that are present in this file to a seperate file.. rohit.shetty84 UNIX for Dummies Questions & Answers 4 09-16-2008 08:34 PM



All times are GMT -4. The time now is 09:16 AM.