Print line when closest number if found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print line when closest number if found
# 1  
Old 01-25-2016
Print line when closest number if found

so i have a code that identifies which value is the closest to a value provided by the user.

Code:
awk -F"," -v c=${COLUMN} -v t=${USTIME} '{a[NR]=$c}END{
        asort(a);d=a[NR]-t;d=d<0?-d:d;v = a[NR]
        for(i=NR-1;i>=1;i--){
                m=a[i]-t;m=m<0?-m:m
                if(m<d){
                    d=m;v=a[i]
                }
        }
        print v
}' data.file

content of data file:

Code:
blah1,blah1,1453745100,blah1,blah1,blah1
blah2,blah2,1453745196,blah2,blah2,blah2
blah3,blah3,1453745296,blah3,blah3,blah3
blah4,blah4,1453745396,blah4,blah4,blah4

right now, this code only outputs the value that it finds to be closest. but i want it to output the entire line from which that value is found.

so, say, this code outputted 1453745196. Instead of that, i want it to output

Code:
blah2,blah2,1453745196,blah2,blah2,blah2

# 2  
Old 01-25-2016
Quick hack:
Code:
awk -F"," -v c=${COLUMN} -v t=${USTIME} '{a[NR]=$c; b[NR]=$0}END{
        asort(a);d=a[NR]-t;d=d<0?-d:d;v = a[NR]; r=b[NR]
        for(i=NR-1;i>=1;i--){
                m=a[i]-t;m=m<0?-m:m
                if(m<d){
                    d=m;v=a[i]; r=b[i]
                }
        }
        print r
}' data.file

# 3  
Old 01-26-2016
Unfortunately, both my awk versions don't have the sort function; with that, this would be even simpler. Try
Code:
awk -F, -vc=3 -vt=1453745256 '{print ($c-t)*($c-t), $0}' file | sort -n | head -1 | cut -d" " -f2-
blah3,blah3,1453745296,blah3,blah3,blah3

---------- Post updated 26-01-16 at 07:18 ---------- Previous update was 25-01-16 at 22:52 ----------

Can't test this one, so please report back. According to man gawk, asorti(s) provides sorting by the indices. Try (untested)
Code:
awk -F, -vc=3 -vt=1453745256 '{S[$c-t)*($c-t)] = $0} END {asorti(S); print S[1]}' file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-26-2016
Quote:
Originally Posted by RudiC
Unfortunately, both my awk versions don't have the sort function; with that, this would be even simpler.
I might be mistaken but to me the problem seems to be quite simply to solve (unfortunately my awk skills are not the best, you can perhaps shake out an implementation that puts whatever i could come up with to shame in an instant), here is the algorithm:

Code:
read first line and store:
          - the line itself
          - the number (or the difference between the number and the user provided value)

read the following line and
          - calculate the difference between its number and the user value
          IF this difference is smaller than the stored one
               - replace the stored line data with the current lines data
          FI
until end of file

You do not need to sort the file at all, just cycle through it once. If the user provided value can be higher or lower than the values in the file you of course need to calculate the absolute value of the difference instead of the difference itself (multiply by -1 if lower than zero).

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

How to print the entire line if the mentioned match is found?

Hello Everyone, I have a file with 5 fields in each line just like mentioned below. Also the 4th field is time elapsed(hh:mm:ss) since the process is running xyz abc status 23:00:00 idle abc def status 24:00:00 idle def gji status 27:00:02 idle fgh gty status 00:00:00 idle Here I... (8 Replies)
Discussion started by: rahul2662
8 Replies

3. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

4. Shell Programming and Scripting

Print line number

how i need help again i have file input.txt like this 48:20111008_20111122:31231|123| 78:20111008_20111122:435453|321112| where 48 and 78 is line number 20111008_20111122 is a file and 31231|123| i want get in file, i am using manual awk for get that file like this awk 'NR==48... (6 Replies)
Discussion started by: zvtral
6 Replies

5. Shell Programming and Scripting

Closest Number from a Range of Numbers

out of a range of numbers, how can i pick out the number that is the closest to any arbitrary/random number that a user supplies? say the range of numbers are between 1 - 90000. but that doesn't mean each number exist between 1 - 90000. the range of numbers could be for example: 1, 3, 4, 6,... (6 Replies)
Discussion started by: SkySmart
6 Replies

6. Shell Programming and Scripting

Print the 2nd line everytime after defined pattern is found.

Hi, I have a text file similar to the example below and I want to print the second line every time after the "--------------------------" pattern is found. The pattern is a fixed length of - characters. Example of input; 1 -------------------------- 2 3 39184018234 4 ... (10 Replies)
Discussion started by: lewk
10 Replies

7. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

8. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

9. Shell Programming and Scripting

Print selection of line based on line number

Hi Unix gurus Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number. I am trying to use the following code. but it is not working. cat... (3 Replies)
Discussion started by: mohanm
3 Replies

10. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies
Login or Register to Ask a Question