Need to extract only decimal numbers for a glob of text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to extract only decimal numbers for a glob of text
# 1  
Old 12-20-2012
Need to extract only decimal numbers for a glob of text [SOLVED]

If you have a look at this thread, you'll see that users have been posting the output a script which are numbers that range from 2 to 5 decimal places. If I dump this entire thread to txt file, how can I:

1) Delete everything except for numbers of the following formats (where 'x' is a digit and '.' is a decimal point)?
2) Format the output to be one target per line?

x.xx
x.xxx
x.xxxx
x.xxxxx

xx.xx
xx.xxx
xx.xxxx
xx.xxxxx

I have experimented with some sed strings but am not making any traction. Perhaps your perl or awk ninjas have a good solution?

EDITED PER MOD REQUEST REMOVING LINK TO FILE AND PASTING IT HERE:
Code:
San2ban wrote:
8.09943
Surprised!. I will remove some bloat and try again
I am overbloated and got a 8.73; try the opposite 
Offline
Report Quote
#352012-03-04 7:42 am
Blµb
Member
Registered: 2008-02-10
Posts: 174
(a) 8.38748
(b) 8.39183
(b) Includes my ~/bin which contains a set of scripts to ease my life. And cbmc, a huge binary, actually a statistical outlier even, since it's 4.6M.
How about taking a look at binaries and scripts seperately? Since scripts, for example, lack all the ELF format overhead their executing binaries have...
root@blubpc ~ # cat > /tmp/filter.sh << EOF
#!/bin/sh
if head "\$1" | grep -q '^#!'; then
  stat -c '%s' "\$1"
	else
	  stat -c '%s' "\$1" >&2
		fi
		EOF
		root@blubpc ~ # chmod +x /tmp/filter.sh
		root@blubpc ~ # find {,/usr}/{s,}bin /lib /boot /etc /home/blub/bin -type f -executable -exec /tmp/filter.sh '{}' "%s" ';' > /tmp/filtered_scripts 2> /tmp/filtered_bins
		root@blubpc ~ # T1=$(cat /tmp/filtered_bins | awk '{t+=$1} END {print t}')
		root@blubpc ~ # T2=$(cat /tmp/filtered_scripts | awk '{t+=$1} END {print t}')
		root@blubpc ~ # echo $T1 $T2
		868139304 30871849
		root@blubpc ~ # cat /tmp/filtered_bins | awk -v t=$T1 '{p=$1/t; h += -p*log(p)/log(2)} END {print h}'
		8.25136
		root@blubpc ~ # cat /tmp/filtered_scripts | awk -v t=$T2 '{p=$1/t; h += -p*log(p)/log(2)} END {print h}'
		6.06017
		root@blubpc ~ # echo sanity check, the following should equal b=8.39183
		sanity check, the following should equal b=8.39183
		root@blubpc ~ # cat /tmp/filtered_bins /tmp/filtered_scripts | awk -v t=$[T1 + T2] '{p=$1/t; h += -p*log(p)/log(2)} END {print h}'
		8.39183
		root@blubpc ~ # cat /tmp/filtered_bins | awk -v t=$[T1 + T2] '{p=$1/t; h += -p*log(p)/log(2)} END {print h}'
		8.01669
		root@blubpc ~ # cat /tmp/filtered_scripts | awk -v t=$[T1 + T2] '{p=$1/t; h += -p*log(p)/log(2)} END {print h}'
		0.375133
		Not really what I was expecting O.o
		You know you're paranoid when you start thinking random letters while typing a password.
		A good post about vim
		Python has no multithreading.
		Offline
		Report Quote
		#362012-03-04 8:40 am
		azleifel
		Member
		From: St Evenage, UK
		Registered: 2007-10-28
		Posts: 475
		Email
		8.72211

---------- Post updated at 07:45 PM ---------- Previous update was at 07:12 PM ----------

I'm getting closer but still no cigar:
Code:
% grep '[0-9]\.[0-9][0-9]' unixness_thread.txt | sed 's/[^0-9.]*//g'


Last edited by graysky; 12-24-2012 at 08:54 AM.. Reason: mod request/just provided a sample
# 2  
Old 12-20-2012
Please do not use a reference to pastebin, but post a relevant sample here using code tags..
# 3  
Old 12-21-2012
Code:
grep -E -o '\<[0-9]{1,2}\.[0-9]{2,5}\>' file

# 4  
Old 12-21-2012
@graysky, Thanks for posting the sample. It is a bit much. What usually gets done is posting a smaller sub sample and if need be post the entire sample by uploading it as an attachment..

Last edited by Scrutinizer; 12-21-2012 at 07:45 AM..
# 5  
Old 12-21-2012
If i get your requirement correctly you might want to consult this thread.

I hope this helps.

bakunin
# 6  
Old 12-24-2012
Quote:
Originally Posted by Scrutinizer
@graysky, Thanks for posting the sample. It is a bit much. What usually gets done is posting a smaller sub sample and if need be post the entire sample by uploading it as an attachment..
Edited. Didn't know pastebin was taboo here nor did I know that I could upload attachments.

Quote:
Originally Posted by bakunin
If i get your requirement correctly you might want to consult this thread.
Thanks for the post. Here is an elegant solution:
Code:
grep -E -o '\<[0-9]{1,2}\.[0-9]{2,5}\>' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Comparing decimal numbers between 0 and 1

For numbers between 0 and 1 the below logic is not working. Output of above shall be "correct" but its echoing "incorrect".Kindly suggest a=.1 if then echo correct else echo incorrect fi Video tutorial on how to use code tags in The UNIX and Linux Forums. (3 Replies)
Discussion started by: itsvikas
3 Replies

3. 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

4. UNIX for Dummies Questions & Answers

If then else for decimal numbers part2

Hi, I have a small problem with my script. I have everything in order but it doesnt seem to compare anything less than 1 correctly. If the input is more than 1, then the results is correct. If the input is 0.xxx (anything) it returns erroneous results. Pls help input=0.12 if ; then ... (7 Replies)
Discussion started by: streddy
7 Replies

5. UNIX for Dummies Questions & Answers

Condition for decimal numbers

Hi experts, My number output has somehting like below filename /temp 0.23 10.23 How do i put a condition to the above numbers? e.g if then the . seem to give me problems. Pls help. thanks ---------- Post updated at 05:25 PM ---------- Previous update was at 05:23 PM... (9 Replies)
Discussion started by: streddy
9 Replies

6. Shell Programming and Scripting

Extract numbers from text file work out average

Just wondering if someone could assist me with shell script I'm trying to write. I need to read the final column of a text file (shown below) and workout what the average number is. The text file will have a variable number of lines, I just want the script to pull out the values in the final field... (14 Replies)
Discussion started by: rich@ardz
14 Replies

7. Shell Programming and Scripting

Regarding decimal numbers

Hello... I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole numbers........ for example..if a program needs to calculate the average of some numbers........ @ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be... (7 Replies)
Discussion started by: ravindra22
7 Replies

8. Shell Programming and Scripting

Comparing Decimal Numbers

Im trying to compare two numbers with decimals but its not working as expected. a=1 b=1.1 if then echo "equal" fi When I do this it says that the numbers are equal. Ultimately Im using -le and -ge in the if statements but I tested with -eq for simplicity. Any way to make this... (3 Replies)
Discussion started by: Grizzly
3 Replies

9. Shell Programming and Scripting

decimal numbers

Hi friends How can I use "for loop" for decimal numbers? ex: 0.1 < x < 0.6 I used this commands but does'nt work. LIMIT=0.6 for ((x=0.1; x<=LIMIT; x++)) do - - - done Many thanks (1 Reply)
Discussion started by: snow
1 Replies

10. Shell Programming and Scripting

Devision of Decimal Numbers?

How can i devide decimal numbers? I am getting this kind of error: line 18: 200.2/40.234: syntax error in expression (error token is ".2/40.234") What can i do to work around this problem? Thanks for any advice. (4 Replies)
Discussion started by: Vozx
4 Replies
Login or Register to Ask a Question