how to find the field has more than 2 decimals


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find the field has more than 2 decimals
# 8  
Old 11-16-2016
Quote:
Originally Posted by ken6503
I tried below command but it gave me the record with one decimal as well.

Code:
awk -F"," '$2!~/\.[0-9][0-9]$/ {print $0}' test

That is a good start. The ERE you are using is matching a decimal point followed by two decimal digits (which as you have found only matches lines with exactly two decimal places) at the end of the 2nd field. By negating the match, it finds lines with more than two digits following a decimal point, less than two digits following a decimal point, and fields that do not contain a decimal point. If we use a positive search for three digits following a decimal point (i.e., $2~/[.][0-9][0-9][0-9]/) not anchored to the end of the field, we can find any field that contains three or more decimal digits after a decimal point.

In the 1st post in this thread you said you wanted to print the 1st two fields from lines that matched your criteria. Using print $0 prints the entire input line; not just the 1st two fields.

Try:
Code:
#!/bin/ksh
/usr/xpg4/bin/awk '
BEGIN {	FS = OFS = ","
}
$2 ~ /[.][0-9][0-9][0-9]/ {
	print $1, $2
}' test

and see if that does what you want.

Note that itkamaraj's solution won't necessarily work with input that has extraneous spaces such as:
Code:
xyz, 123456.78 , xxx

which has three characters after the decimal point, but not three digits after the decimal point.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 9  
Old 11-16-2016
Hello ken6503,

Could you please try following and let me know if this helps.
Code:
awk -F", " '($2 ~ /\.[0-9]{3}+/){print $1,$2}'   Input_file

Output will be as follows.
Code:
xyz, 123456.789

Also I have tested it in GNU awk, I hope it should work in all versions too(not tested on others though). On a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 11-16-2016
Quote:
Originally Posted by Don Cragun
Instead of searching the internet, why don't you look back through all of the solutions you have been given to solve your previous problems and use what you have learned from those solutions to try come up with a very simple nawk script that will do what you have asked for here?
thanks for the suggestion, I'll review the old post to find the solution first in the future.
This User Gave Thanks to ken6503 For This Post:
# 11  
Old 11-17-2016
Since you always have a period in the second field, a simple

Code:
    grep -E  '[^,]+,[ 0-9]+[.][0-9]{3}'

on your file should do.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-17-2016 at 03:25 AM.. Reason: Added CODE tags.
# 12  
Old 11-17-2016
grep
Code:
grep -o '.*\.[0-9]\{3\}' file

# 13  
Old 11-17-2016
Quote:
Originally Posted by looney
grep
Code:
grep -o '.*\.[0-9]\{3\}' file

A .* at the start of a pattern is redundant.

Also, the pattern incorrectly reports occurances of the pattern in the first or third field. The OP wanted to check explicitly the second field only. While the example suggests that these fields might contain only non-digit contain, we don't know for sure that this is the case.
# 14  
Old 11-17-2016
Quote:
Originally Posted by rovf
A .* at the start of a pattern is redundant.

Also, the pattern incorrectly reports occurances of the pattern in the first or third field. The OP wanted to check explicitly the second field only. While the example suggests that these fields might contain only non-digit contain, we don't know for sure that this is the case.
Hi rovf,
Good catch. Note, however, that your suggestion in post #11:
Code:
grep -E  '[^,]+,[ 0-9]+[.][0-9]{3}'

can also match a decimal point and three digits in the 3rd or subsequent fields. To avoid that possibility, you need to anchor your grep search pattern to the start of a line:
Code:
grep -E  '^[^,]+,[ 0-9]+[.][0-9]{3}'

And, if you go back to post #1 in this thread, you will notice that it appears that the OP only wants to print the first two fields of matched lines; not the entire line. It seems that that is why looney used the -o option and the leading .* in the search pattern. To restrict that match to the 2nd field and to print the entire contents of the 1st two fields if there are more digits or non-digit characters after a decimal point and 3 digits in the 2nd field, you might want something more like:
Code:
grep -Eo  '^[^,]*,[ 0-9]+[.][0-9]{3}[^,]*'

if the version of grep on your system supports the -o option (which is not required by the standards). (Note that I used [^,]* in both places in this suggestion rather than [^,]+ because there is no stated requirement that the 1st field be non-empty.) If your system's grep does not include a -o option, you'll have to use something like awk, perl, or sed instead of grep to print a partial line match.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find the difference in specific field

Hi All, Seeking for your assistance to get the difference of field1 and field2 and output all the records if there's a difference. please see below scenario. file1.txt 250|UPTREND FASHION DESIGN,CORP.|2016-04-04 09:36:13.991257 74|MAINSTREAM BUSINESS INC.|2016-04-04 09:36:13.991257... (1 Reply)
Discussion started by: znesotomayor
1 Replies

2. Shell Programming and Scripting

Find pattern in first field of file

Hello all I have two files. 1. Pattern.txt - It contains patterns to be matched. It has large number of patterns to be matched. Cat Pattern.txt Ram Shyam Mohan Jhon I have another file which has actual data and records are delimted by single or multiple spaces. 2.... (8 Replies)
Discussion started by: krsnadasa
8 Replies

3. Shell Programming and Scripting

Find a blank field

Find a blank field Hi I have set of fields that have some blank values, how to find that and get its line noumbers in output file. Ex: Col1 col2 col3 11 ss 103 12 104 13 105 14 se 106 (2 Replies)
Discussion started by: Shenbaga.d
2 Replies

4. Shell Programming and Scripting

how to find the 2nd field

java....4059... compsite 62u IPv4 170747 TCP *:9400 (LISTEN) java...... 05... compsite 109u IPv4 171216 TCP *:9401 (LISTEN) This is Joust formated like this Please Repace "." with space" " All are Right Justfied Output :- 4058 and 05 so that i can kill this (1 Reply)
Discussion started by: pareshpatra
1 Replies

5. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

6. Shell Programming and Scripting

find the field number

######################## SOLVED ################## Hi I have a header file like the following and the field "IDENTIFIER" can be at any possition on this line, The line can containt a variable number of field, not alway the same depending of the header file i use ... (6 Replies)
Discussion started by: kykyboss023
6 Replies

7. Shell Programming and Scripting

find the last field in a string

Hi all I have strings with fields separated by <space> and I want to automatic find the value of "the last field -1" for each string Strings don't have the same nb of fields I know it's possible with awk but I didn't find syntax... Many thanks for your help ;) (3 Replies)
Discussion started by: madmat
3 Replies

8. Shell Programming and Scripting

Find top N values for field X based on field Y's value

I want to find the top N entries for a certain field based on the values of another field. For example if N=3, we want the 3 best values for each entry: Entry1 ||| 100 Entry1 ||| 95 Entry1 ||| 30 Entry1 ||| 80 Entry1 ||| 50 Entry2 ||| 40 Entry2 ||| 20 Entry2 ||| 10 Entry2 ||| 50... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

9. Shell Programming and Scripting

convert Regular decimals to Packed decimals

Hi, I am trying to find if there is a way to convert regular decimal values to Paced decimal values. I tried to find a c program but I could get a Packed converted to regular decimal not the other way round. If not unix please let me know if any other progrimming language I can use to do... (2 Replies)
Discussion started by: mgirinath
2 Replies

10. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies
Login or Register to Ask a Question