Find the closest value in another csv file preceding it and following it?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the closest value in another csv file preceding it and following it?
# 1  
Old 02-13-2013
Find the closest value in another csv file preceding it and following it?

Hi,

Is this possible? I want to take a csv file and find the closest value in another csv file preceding it and following it.

For ex. In this csv file, I'll take the first line:

Code:
1309341156.800000000
1309341156.802500000
1309341156.805000000
1309341156.807500000

and find the closest value preceding and following in this csv file:

Code:
1,1309341156.793221222
1,1309341156.795721232
1,1309341156.798221262
1,1309341156.800721272
1,1309341156.803221282

Which would be:
Code:
1,1309341156.798221262
1,1309341156.800721272


Is the precision going to be a problem if I try using awk or sed?


Thanks!

Last edited by Scrutinizer; 02-13-2013 at 05:09 PM.. Reason: code tags
# 2  
Old 02-13-2013
Are the two files sorted like your example data implies?

If there are no proceeding/following value should it print a blank line in it's place or just 1 value instead?

---------- Post updated at 07:12 AM ---------- Previous update was at 06:47 AM ----------

Here is a solution if both files are sorted that prints blank when proceeding or following value not available:

Code:
awk -F, 'FNR==NR{V[NR]=$2; L[NR]=$0; next}
{ 
  while(((k+1) in V) && $1 > V[k+1]) k++
  if ($1<V[k]) printf "\n"
  else print L[k];
  if (!(k+1 in V)) printf "\n"
  else print L[k+1]
}' fileb filea

# 3  
Old 02-13-2013
try also:
Code:
awk -v value_record=1 '
BEGIN {last_value="first record"}
NR==FNR {if (NR==value_record) {val=$1 ; next}; next}
(!val) {print "No value found"; exit}
$2>=val {print last_value; print $0; f=1; exit}
{last_value=$0}
END {if (val) if (!f) {print last_value ; print "last record"}}
' values_file FS=, csv_file

update value record as needed or include as variable from parent script.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Script Closest Timestamp (LOG FILE)

Hi All, I need something where I could take the date from FILE1 and in this case the date is Thu Sep 10 13:48:42 EDT 2015 and I need it to match the closest PREVIOUS date in the log file FILE2 this means in this specific case it would be Thu Sep 10 2015 13:35:28. I only need the closest... (3 Replies)
Discussion started by: roberto999
3 Replies

2. Shell Programming and Scripting

awk find closest

Hi All I am sorry but I have to open a new thread about my issue. My input files are: file_1 ID_1 10 ID_2 15 ID_3 32 ID_4 45 ID_5 66 ID_6 79 ID_7 88 file_2 ID_3 ID_5My output file should be ID_3 ID_1(-22) ID_2(-17) ID_4(-13) ID_5(34) (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

3. Shell Programming and Scripting

Find Node and replace line(s) preceding in xml file

Hello, I have an xml file whose contacts are like below: <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Mango <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Bannana (3 Replies)
Discussion started by: umarsatti
3 Replies

4. Shell Programming and Scripting

Increment and find closest value until the end of the file is reached.

Hi all, I have a file which looks like: 0 1.1985506 1 1.2237930 2 1.2159038 3 1.2668828 4 1.2650216 5 1.2474344 6 1.2817688 7 1.2721698 8 1.2665005 9 1.2826315 10 1.2797879 11 1.3201736 12 1.3116595 13 1.3361583 14 1.3309238 (2 Replies)
Discussion started by: ezitoc
2 Replies

5. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

6. Shell Programming and Scripting

Find the position of a pattern on a line from a csv file

hello I'm doing a unix program and i'm using many file csv.in each csv file the colums are separated by ";" I would like to know the position of a pattern. For example for a line yyyy, bbbb, cccc; ddddd;eeee. I will like for example by finding the position of the pattern "cccc" and the response is... (6 Replies)
Discussion started by: papis
6 Replies

7. Shell Programming and Scripting

Log File - Getting Info about preceding Date of Pattern Found

Ok Suppose I have a log file like the below: 2010-07-15 00:00:01,410 DEBUG 2010-07-15 00:01:01,410 DEBUG 2010-07-15 00:01:02,410 DEBUG com.af ajfajfaf affafadfadfd dfa fdfadfdfadfadf fafafdfadfdafadfdaffdaffadf afdfdafdfdafafd error error failure afdfadfdfdfdf EBUDGG eafaferror failure... (6 Replies)
Discussion started by: SkySmart
6 Replies

8. Shell Programming and Scripting

find & replace comma in a .csv file.

HI, Please find the text below. I receive a .csv file on server. I need the comma(,) in the second column to be replaced by a semi-colon( ; ). How to do it. Please help. Sample text: "1","lastname1,firstname1","xxxxxx","19/10/2009","23/10/2009","0","N","Leave"... (2 Replies)
Discussion started by: libin4u2000
2 Replies

9. Shell Programming and Scripting

Finding word in file then print the preceding....

Hi, I am looking for a way to find a particular word in a file then print a line that precedes this line, as well as this line. Sometimes in a log file there is only one word per line and I need to print one of the lines leading up to the single worded line. Example - I can grep for ouch... (5 Replies)
Discussion started by: g_jumpin
5 Replies
Login or Register to Ask a Question