Need to grep for decimal values only in the second column.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to grep for decimal values only in the second column.
# 1  
Old 04-19-2018
Need to grep for decimal values only in the second column.

Hi,

I have a file in which I need to print all the lines that have decimal values in the second column.

The below prints all the decimal values from the second column but I need the complete line to be printed.

Code:
cat hello.out | sed 's/   */:/g' | cut -d : -f 2 | ggrep  -Eo "[0-9]+\.[0-9]+"

Can you please help ?

Current output:

Code:
10.877406
10.877623
10.877905

Desired Output:

Code:
18026/1:        10.877406    lseek(10, 1480, SEEK_SET)                  = 1480
18026/1:        10.877623    fstat(1, 0xFFFFFFFF7FFFE840)                       = 0
18026/1:        10.877905    _exit(0)


Last edited by Scrutinizer; 04-19-2018 at 02:45 PM.. Reason: quote tags -> code tags
# 2  
Old 04-19-2018
Code:
$
$ cat hello.out
18026/1: 10.877406 lseek(10, 1480, SEEK_SET) = 1480
18026/1: 10.877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10.877905 _exit(0)
18026/1: 10877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10877905 _exit(0)
18026/1: 0.877905 _exit(0)
  
$ awk 'index($2,".") > 0' hello.out
18026/1: 10.877406 lseek(10, 1480, SEEK_SET) = 1480
18026/1: 10.877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10.877905 _exit(0)
18026/1: 0.877905 _exit(0)
  
$
$

That won't check for a well-formed decimal number though (which I notice you tried to do in your grep command).
Here's a more elaborate test case:

Code:
$
$
$ cat hello.out
18026/1: 10.877406 lseek(10, 1480, SEEK_SET) = 1480
18026/1: 10.877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10.877905 _exit(0)
18026/1: 10877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 1.087.7905 _exit(0)
18026/1: 0.877905 _exit(0)
18026/1: +0.877905 _exit(0)
18026/1: -0.877905 _exit(0)
18026/1: -.877905 _exit(0)
18026/1: +123.0 _exit(0)
18026/1: #0.877905 _exit(0)
18026/1: .999905 _exit(0)
18026/1: 0.87.790a5 _exit(0)
$
$ perl -lane 'print if $F[1] =~ /^[+-]*\d*\.\d+$/' hello.out
18026/1: 10.877406 lseek(10, 1480, SEEK_SET) = 1480
18026/1: 10.877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10.877905 _exit(0)
18026/1: 0.877905 _exit(0)
18026/1: +0.877905 _exit(0)
18026/1: -0.877905 _exit(0)
18026/1: -.877905 _exit(0)
18026/1: +123.0 _exit(0)
18026/1: .999905 _exit(0)
$
$

Still, this won't check for decimal numbers in scientific notation.

Last edited by durden_tyler; 04-19-2018 at 02:50 PM.. Reason: Better test case.
# 3  
Old 04-19-2018
One other option:
Code:
sed -n "/ [+-]*[0-9]*\.[0-9][0-9]* / p" hello.out

It writes nine lines given Durden's last example of hello.out.
This User Gave Thanks to wbport For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep values from column 2 in reference of column 1

Gents Is it possible to update the code to get the desired output files from the input list. I called variable to the first column. I need to consider the first column as key to grep the values in the second column according to the desired request. input list (attached ) output1 ... (12 Replies)
Discussion started by: jiam912
12 Replies

2. UNIX for Dummies Questions & Answers

Replace all decimal values in a column

Hi My input file looks String000002 GeneWise CW 48945 49354 . - 0 Pt=PEQU_00004; String000002 LEN NA 52125 52604 0.945751 - . PID=PEQU_00005;lvid_id=PEQ_28708; String000002 LEN CW 52125 52604 . - 0 ... (3 Replies)
Discussion started by: siya@
3 Replies

3. Shell Programming and Scripting

Rounding decimal values in a column

Hi, I wanted to round all the values in a column to nearest integer. I have multiple files with only two columns and I want to round values in column 2. e.g input_file A1 23.971578 A2 34.624976 A3 46.403446 A4 375 A5 1 A6 3 A7 ... (3 Replies)
Discussion started by: ashu0001
3 Replies

4. Shell Programming and Scripting

Rounding off decimal values

Hi Friends, This is my last post for today. My input file is chr1 100 200 chr1 123 300 chr1 300 400 chr1 420 520 chr10 132344343 132348674 When I try using this command awk '{v=($3+$2)/2; print $0"\t"v}' 1 This is my output chr1 100 200 150 chr1 123 300 211.5 (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

How to get decimal values ?

Hi All, In my script I've written like this- c=$( expr 100 / 3);echo $c The output coming is 33. but I want to see 33.33, decimal values too. How to get that? Thanks, Naresh (3 Replies)
Discussion started by: NARESH1302
3 Replies

6. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

7. Shell Programming and Scripting

print decimal values

Hi guys I'm trying to print average of 2 columns. awk '{print ($1+$2)/2}' file.txt Its printing average but not giving decimal values its giving 3.05521e+08 instead of 305521.... I tried %f to print float values but not quiet connected Could you help plz:confused: (5 Replies)
Discussion started by: repinementer
5 Replies

8. Shell Programming and Scripting

Evaluating Decimal values

How can I evaluate a decimal value in an if statement? echo "Enter limit:" read limit (enter a decmal value, ie: 2.5) decimallimit=`echo $limit+0|bc|quit` echo $decimallimit if then echo $decimallimit else echo "failed" fi (4 Replies)
Discussion started by: larrys721
4 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies
Login or Register to Ask a Question