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

  Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Extract Numbers from a log file

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-26-2013
Sajjadmehdi Sajjadmehdi is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 21 July 2016, 1:57 AM EDT
Posts: 3
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 03:36 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 08-26-2013
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 July 2016, 3:22 AM EDT
Location: Bangalore, India
Posts: 450
Thanks: 27
Thanked 71 Times in 69 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 Unix and Linux 08-26-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 July 2016, 2:58 PM EDT
Location: Amsterdam
Posts: 10,937
Thanks: 417
Thanked 3,005 Times in 2,673 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 Unix and Linux 08-26-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 22 July 2016, 9:30 PM EDT
Location: India
Posts: 1,949
Thanks: 30
Thanked 562 Times in 538 Posts

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

Sponsored Links
    #5  
Old Unix and Linux 08-26-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 July 2016, 1:11 PM EDT
Location: Aachen, Germany
Posts: 9,014
Thanks: 174
Thanked 2,604 Times in 2,440 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
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04: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 02:06 PM
Extract numbers from .txt file eggali UNIX for Dummies Questions & Answers 5 08-15-2010 06:01 PM
Extract numbers from text file work out average rich@ardz Shell Programming and Scripting 14 08-04-2010 07: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 07:34 PM



All times are GMT -4. The time now is 08:59 PM.