grep or awk problem, unable to extract numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep or awk problem, unable to extract numbers
# 1  
Old 08-30-2007
grep or awk problem, unable to extract numbers

Hi, I've trouble getting some numbers from a html-file. The thing is that I have several html-logs that contains lines like this:

nerdnerd, how_old_r_u:45782<br>APPLY: <hour_second> Verification succeded

This is some of what I've extracted from a html file but all I really want is the number in the middle. When using awk I get:


how_old_r_u:45782<br>APPLY:

since there is a space at each end, like a separator for awk.

And I tried using grep "[0-9]" but it only takes the whole line containing the number so I get the whole line again. Is there any command that can retreive the numbers only?
# 2  
Old 08-30-2007
The pattern is not very clear. But you can try

Code:
grep -oE "[[:digit:]]{1,}" input.txt

If that does not satisfy your requirement, perhaps this.

Code:
sed -n -e "s/.*:\([0-9]*\).*/\1/p" input.txt

# 3  
Old 08-31-2007
Quote:
Originally Posted by vino
The pattern is not very clear. But you can try

Code:
grep -oE "[[:digit:]]{1,}" input.txt

If that does not satisfy your requirement, perhaps this.

Code:
sed -n -e "s/.*:\([0-9]*\).*/\1/p" input.txt

But if there is more numbers on that line for example:

how_old_r_u:45782<br>APPLY:[30000,t3,t4]:Plummet

It seems when I run the command

Code:
grep -oE "[[:digit:]]{1,}" input.txt

I also get the other numbers is there some way to get only 45782?
# 4  
Old 08-31-2007
cut -f2 -d: inputfile |sed s/[^0-9]//g
# 5  
Old 08-31-2007
Is that number composed of 5 digits only?
if YES, then you can use the awk command and you can print that substring only....

code:
cat input.txt|awk 'BEGIN {FS=":"} {print substr($2,1,5)}'

this may help.....
# 6  
Old 08-31-2007
Quote:
Originally Posted by charbel
Is that number composed of 5 digits only?
if YES, then you can use the awk command and you can print that substring only....

code:
cat input.txt|awk 'BEGIN {FS=":"} {print substr($2,1,5)}'

this may help.....
no need for cat.
Code:
awk 'BEGIN {FS=":"} {print substr($2,1,5)}' input.txt

# 7  
Old 08-31-2007
Quote:
Originally Posted by baghera
But if there is more numbers on that line for example:

how_old_r_u:45782<br>APPLY:[30000,t3,t4]:Plummet

It seems when I run the command

Code:
grep -oE "[[:digit:]]{1,}" input.txt

I also get the other numbers is there some way to get only 45782?
Which is why the sed alternative was provided. Did you try that ? Does that give you what you are looking for ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed / awk / grep to extract information from log

Hi all, I have a query that runs that outputs data in the following format - 01/09/12 11:43:40,ADMIN,4,77,Application Group Load: Name(TESTED) LoadId(5137-1-0-1XX-15343-15343) File(/dir/dir/File.T03.CI2.RYR.2012009.11433350806.ARD) InputSize(5344) OutputSize(1359) Rows(2) Time(1.9960)... (8 Replies)
Discussion started by: jeffs42885
8 Replies

2. Shell Programming and Scripting

Router ping log extract data from it Awk/Sed/grep

Hi, I am new to this world.. Using expect i loging to router and checking ping response to my links. I need to genarate report using this output and that report contains only three file link name, packet loss, latency. my output of script is like below: -bash-3.00$ monmw/mwbkp... (2 Replies)
Discussion started by: jkmistry
2 Replies

3. Shell Programming and Scripting

Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV. The strings could be on different line numbers, depending on size of log. Example Log File: File = foo.bat Date = 11/11/11 User = Foo Bar Size = 1024 ... CSV should look like: "foo.bat","11/11/11","Foo Bar","1024" (7 Replies)
Discussion started by: chipperuga
7 Replies

4. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

5. Shell Programming and Scripting

Problem with sub command (awk) and numbers

Hi, I am trying to perform a simple soustraction between two floating numbers and cannot get it done for some reason due to the use of the sub command. The following is the straight-forward result of the soustraction: $ echo | gawk '{a=968;b=967.99;c=a-b;print c}' ... (2 Replies)
Discussion started by: Indalecio
2 Replies

6. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

7. UNIX for Dummies Questions & Answers

Using GREP/AWK to extract a number and store it as a variable

Hello, I have a question regarding the awk command. Here is the line I need to grep: 1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157 Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any... (3 Replies)
Discussion started by: modey3
3 Replies

8. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies

9. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

10. Shell Programming and Scripting

problem with floating point numbers in awk

hi all, i have the following problem using awk in a script i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this num = $4 i cant find a way to convert the variable from string to floating point to perform... (7 Replies)
Discussion started by: kanagias
7 Replies
Login or Register to Ask a Question