Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk matching script not working as expected

Tags
awk bash perl ubuntu gnu, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 8  
Old 03-22-2018
How about this:

Code:
#!/usr/bin/awk -f
FNR==1 {file++}
{
  day=$1
  gsub(/-/, " ", day)
  split($2, t, ".")
  gsub(/:/, " ", t[1])
  x=mktime(day " " t[1]) + t[2] / 1000
  if(file==1) srctime[FNR]=x
  else desttime[FNR]=x
  records[file, FNR]=$0
}

END {
   offset=5*60
   max=2*60
   cur=1
   for (rec in srctime) {
       target = srctime[rec] + offset
       offsetmin = target - max
       offsetmax = target + max
       best = 9999999
       found = 0
       while(cur in desttime && desttime[cur] < offsetmax) {
           if (desttime[cur] < target && desttime[cur] > offsetmin &&
               best > target - desttime[cur]) {
                  best= target - desttime[cur]
                  found=cur
           }
           if (desttime[cur] >= target) {
              if(best > desttime[cur] - target) {
                  best=desttime[cur] - target
                  found=cur
               }
               break
           }
           cur++
        }

        if (found)
           print records[1, rec] " " records[2, found]
        else
           print records[1, rec] " NA NA"
    }
}


Result:
Code:
2018-02-16 16:45:29.557 farads 0.0004300000 2018-02-16 16:50:40.486 reactance 0.0002400000
2018-02-16 16:46:09.300 farads 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 farads 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 farads 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 farads 0.0006500000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:48:34.077 farads 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 farads 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 farads 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 farads 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 farads 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 farads 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 farads 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 farads 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 farads 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000



Edit: previous solution could miss closer records that are before previous target this should be more accurate:

Code:
#!/usr/bin/awk -f
FNR==1 {file++}
{
  day=$1
  gsub(/-/, " ", day)
  split($2, t, ".")
  gsub(/:/, " ", t[1])
  x=mktime(day " " t[1]) + t[2] / 1000
  if(file==1) srctime[FNR]=x
  else desttime[FNR]=x
  records[file, FNR]=$0
}

END {
   offset=5*60
   max=2*60
   deststart=0
   for (rec in srctime) {
       target = srctime[rec] + offset
       offsetmin = target - max
       offsetmax = target + max
       best = 9999999
       found = 0
       cur=deststart+1
       while(cur in desttime && desttime[cur] < offsetmax) {
           if (desttime[cur] < target && desttime[cur] > offsetmin &&
               best > target - desttime[cur]) {
                  if( best = 9999999) deststart = cur
                  best= target - desttime[cur]
                  found=cur
           }
           if (desttime[cur] >= target) {
              if(best > desttime[cur] - target) {
                  best=desttime[cur] - target
                  found=cur
               }
               break
           }
           cur++
        }

        if (found)
           print records[1, rec] " " records[2, found]
        else
           print records[1, rec] " NA NA"
    }
}


Last edited by Chubler_XL; 03-22-2018 at 02:02 AM..
The Following User Says Thank You to Chubler_XL For This Useful Post:
delbroooks (03-23-2018)
# 9  
Old 03-22-2018
What command did you use at the command line to run the code?
Code:
gawk --lint -f awkscript4  < file1.txt file2.txt | less

prints the following:

Code:
2018-02-16 16:46:09.300 reactance 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 reactance 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 reactance 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 reactance 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:48:34.077 reactance 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 reactance 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 reactance 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 reactance 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 reactance 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 reactance 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 reactance 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 reactance 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 reactance 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000
2018-02-16 16:54:44.223 reactance 0.0003800000 2018-02-16 16:59:39.217 reactance 0.0001900000
2018-02-16 16:55:24.769 reactance 0.0003200000 2018-02-16 17:00:19.129 reactance 0.0005800000
2018-02-16 16:56:10.028 reactance 0.0002700000 2018-02-16 17:00:59.328 reactance 0.0001500000
2018-02-16 16:56:57.624 reactance 0.0000900000 2018-02-16 17:01:39.138 reactance 0.0005400000
2018-02-16 16:57:37.387 reactance 0.0003000000 2018-02-16 17:02:19.786 reactance 0.0006600000
2018-02-16 16:58:16.929 reactance 0.0005800000 2018-02-16 17:03:00.236 reactance 0.0004700000
2018-02-16 16:58:56.961 reactance 0.0003000000 2018-02-16 17:03:44.343 reactance 0.0003300000
2018-02-16 16:59:39.217 reactance 0.0001900000 2018-02-16 17:04:24.996 reactance 0.0002200000
2018-02-16 17:00:19.129 reactance 0.0005800000 2018-02-16 17:05:05.754 reactance 0.0003200000
2018-02-16 17:00:59.328 reactance 0.0001500000 2018-02-16 17:05:48.512 reactance 0.0004600000
2018-02-16 17:01:39.138 reactance 0.0005400000 2018-02-16 17:06:29.248 reactance 0.0003700000
2018-02-16 17:02:19.786 reactance 0.0006600000 2018-02-16 17:07:09.819 reactance 0.0001300000
2018-02-16 17:03:00.236 reactance 0.0004700000 2018-02-16 17:07:50.392 reactance 0.0005500000
2018-02-16 17:03:44.343 reactance 0.0003300000 2018-02-16 17:08:32.397 reactance 0.0002000000
2018-02-16 17:04:24.996 reactance 0.0002200000 2018-02-16 17:09:14.778 reactance 0.0003000000
2018-02-16 17:05:05.754 reactance 0.0003200000 2018-02-16 17:09:57.688 reactance 0.0003100000


Last edited by delbroooks; 03-22-2018 at 12:55 PM..
# 10  
Old 03-22-2018
Try:

Code:
gawk --lint -f awkscript4  file1.txt file2.txt

or if you have the correct hash bang at the top of your script (something like #!/usr/bin/gawk -f) you can do:
Code:
$ chmod 755 awkscript4
$ ./awkscript4 file1.txt file2.txt

# 11  
Old 03-22-2018
There is a bug in the code. I tried your suggestion, What I see is happening is file1 (containing farads) is matching with file1 itself. file 2 (containing reactance) is matching with file 2 itself and both are merging. I want file1 to match with file2 and print the matched values.

This is what is happening with
Code:
gawk --lint -f awkscript4  file1.txt file2.txt

Code:
 2018-02-17 00:05:40.967 farads 0.0001400000  2018-02-17 00:12:00.863 farads 0.0001600000
 2018-02-17 00:06:24.584 farads 0.0001000000  2018-02-17 00:12:00.863 farads 0.0001600000
 2018-02-17 00:07:04.742 farads 0.0002500000  2018-02-17 00:12:00.863 farads 0.0001600000
 2018-02-17 00:12:00.863 farads 0.0001600000  2018-02-17 00:16:56.912 farads 0.0002100000
 2018-02-17 00:12:41.023 farads 0.0002400000  2018-02-17 00:17:37.895 farads 0.0001800000
 2018-02-17 00:13:22.429 farads 0.0001500000  2018-02-17 00:18:18.354 farads 0.0003700000
 2018-02-17 00:14:04.826 farads 0.0004100000  2018-02-17 00:18:58.071 farads 0.0004700000
 2018-02-17 00:14:51.079 farads 0.0001600000  2018-02-17 00:18:58.071 farads 0.0004700000
 2018-02-17 00:15:31.247 farads 0.0003500000  2018-02-17 00:18:58.071 farads 0.0004700000
 2018-02-17 00:16:17.396 farads 0.0001900000 NA NA
 2018-02-17 00:16:56.912 farads 0.0002100000 NA NA
 2018-02-17 00:17:37.895 farads 0.0001800000 NA NA
 2018-02-17 00:18:18.354 farads 0.0003700000 NA NA
 2018-02-17 00:18:58.071 farads 0.0004700000 NA NA
 2018-02-17 18:19:38.135 farads 0.0002000000  2018-02-17 18:24:27.966 farads 0.0001800000
 2018-02-17 18:20:22.373 farads 0.0002600000  2018-02-17 18:25:11.832 farads 0.0002800000
 2018-02-17 18:21:02.161 farads 0.0003000000  2018-02-17 18:25:52.344 farads 0.0003000000
 2018-02-17 18:21:43.806 farads 0.0002700000  2018-02-17 18:26:33.672 farads 0.0002600000
 2018-02-17 18:22:25.394 farads 0.0002500000  2018-02-17 18:27:15.499 farads 0.0004300000
 2018-02-17 18:23:06.549 farads 0.0003100000  2018-02-17 18:27:55.288 farads 0.0004800000
 2018-02-17 18:23:46.638 farads 0.0002100000  2018-02-17 18:28:56.699 farads 0.0004200000
 2018-02-17 18:24:27.966 farads 0.0001800000  2018-02-17 18:29:40.909 farads 0.0002100000
 2018-02-17 18:25:11.832 farads 0.0002800000  2018-02-17 18:30:20.942 farads 0.0003400000
 2018-02-17 18:25:52.344 farads 0.0003000000  2018-02-17 18:31:03.937 farads 0.0003500000
 2018-02-17 18:26:33.672 farads 0.0002600000  2018-02-17 18:31:51.329 farads 0.0002500000
 2018-02-17 18:27:15.499 farads 0.0004300000  2018-02-17 18:32:32.608 farads 0.0005000000
 2018-02-17 18:27:55.288 farads 0.0004800000  2018-02-17 18:33:12.869 farads 0.0004900000
 2018-02-17 18:28:56.699 farads 0.0004200000  2018-02-17 18:33:52.725 farads 0.0002300000
 2018-02-17 18:29:40.909 farads 0.0002100000  2018-02-17 18:34:39.022 farads 0.0001300000
 2018-02-17 18:30:20.942 farads 0.0003400000  2018-02-17 18:35:20.579 farads 0.0002800000
 2018-02-17 18:31:03.937 farads 0.0003500000  2018-02-17 18:36:00.487 farads 0.0002400000
 2018-02-17 18:31:51.329 farads 0.0002500000  2018-02-17 18:36:51.908 farads 0.0004500000
 2018-02-17 18:32:32.608 farads 0.0005000000  2018-02-17 18:37:33.667 farads 0.0002500000
 2018-02-17 18:33:12.869 farads 0.0004900000  2018-02-17 18:38:13.989 farads 0.0004700000
 2018-02-17 18:33:52.725 farads 0.0002300000  2018-02-17 18:38:53.753 farads 0.0003500000
 2018-02-17 18:34:39.022 farads 0.0001300000  2018-02-17 18:39:34.052 farads 0.0004100000
 2018-02-17 18:35:20.579 farads 0.0002800000  2018-02-17 18:39:34.052 farads 0.0004100000
 2018-02-17 18:36:00.487 farads 0.0002400000  2018-02-17 18:39:34.052 farads 0.0004100000
 2018-02-17 18:36:51.908 farads 0.0004500000 NA NA
 2018-02-17 18:37:33.667 farads 0.0002500000 NA NA
 2018-02-17 18:38:13.989 farads 0.0004700000 NA NA
 2018-02-17 18:38:53.753 farads 0.0003500000 NA NA
 2018-02-17 18:39:34.052 farads 0.0004100000 NA NA
 NA NA
2018-02-16 16:46:09.300 reactance 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 reactance 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 reactance 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 reactance 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:48:34.077 reactance 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 reactance 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 reactance 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 reactance 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 reactance 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 reactance 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 reactance 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 reactance 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 reactance 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000

Actually the match should be like this where lines that require match are the first four columns that come from file1 and the matched values are the last four columns that come from file2
Code:
2018-02-16 16:45:29.557 farads 0.0004300000 2018-02-16 16:50:40.486 reactance 0.0002400000
2018-02-16 16:46:09.300 farads 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 farads 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 farads 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 farads 0.0006500000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:48:34.077 farads 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 farads 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 farads 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 farads 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 farads 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 farads 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 farads 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 farads 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 farads 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000


Last edited by delbroooks; 03-22-2018 at 06:41 PM..
# 12  
Old 03-23-2018
For your first problem, try - based on the assumption that there's only few time stamps with duplicate seconds and norrmally large gaps in between - this simpler approach, which eliminates the need for a system call to date by adding the epoch time to every line upfront:

Code:
paste <(date +"%s" -f<(cut -d" " -f1,2 data.txt)) data.txt | awk '
$1 in LN        {$1++
                }
                {TM[NR] = $1
                 sub ($1 ".", _)
                 LN[TM[NR]] = $0
                }
END             {for (n=1; n<=NR; n++)  {TMP = TM[n] + 300
                                         DT  = 0
                                         for (SEC=0; SEC<120; SEC++)    {if ((TMP + SEC) in LN) DT = +SEC
                                                                         if ((TMP - SEC) in LN) DT = -SEC
                                                                         if (DT) break
                                                                        }
                                         OUT = LN[TMP+DT]
                                         sub  (/farads./, _, OUT)
                                         $0 = LN[TM[n]] OFS (OUT?OUT:"NA NA")
                                         print
                                        }
                }
'

# 13  
Old 03-23-2018
For your other problem, try
Code:
paste <(date +"%s" -f<(cut -d" " -f1,2 file2)) file2 > TMP2
paste <(date +"%s" -f<(cut -d" " -f1,2 file1)) file1 > TMP1
awk '

FNR == NR       {if ($1 in LN)  $1++
                 TM[NR] = $1
                 sub ($1 ".", _)
                 LN[TM[NR]] = $0
                 next
                }

                {TMP = $1 + 300
                 DT  = 0
                 for (SEC=0; SEC<120; SEC++)    {if ((TMP + SEC) in LN) DT = +SEC
                                                 if ((TMP - SEC) in LN) DT = -SEC
                                                 if (DT) break
                                                }
                 OUT = LN[TMP+DT]
                 sub ($1 ".", _)
                 print $0  OFS (OUT?OUT:"NA NA")
                }
' TMP2 TMP1

# 14  
Old 03-23-2018
Quote:
Originally Posted by Chubler_XL
Edit: previous solution could miss closer records that are before previous target this should be more accurate:

Code:
#!/usr/bin/awk -f
FNR==1 {file++}
{
  day=$1
  gsub(/-/, " ", day)
  split($2, t, ".")
  gsub(/:/, " ", t[1])
  x=mktime(day " " t[1]) + t[2] / 1000
  if(file==1) srctime[FNR]=x
  else desttime[FNR]=x
  records[file, FNR]=$0
}

END {
   offset=5*60
   max=2*60
   deststart=0
   for (rec in srctime) {
       target = srctime[rec] + offset
       offsetmin = target - max
       offsetmax = target + max
       best = 9999999
       found = 0
       cur=deststart+1
       while(cur in desttime && desttime[cur] < offsetmax) {
           if (desttime[cur] < target && desttime[cur] > offsetmin &&
               best > target - desttime[cur]) {
                  if( best = 9999999) deststart = cur
                  best= target - desttime[cur]
                  found=cur
           }
           if (desttime[cur] >= target) {
              if(best > desttime[cur] - target) {
                  best=desttime[cur] - target
                  found=cur
               }
               break
           }
           cur++
        }

        if (found)
           print records[1, rec] " " records[2, found]
        else
           print records[1, rec] " NA NA"
    }
}


This is matching the two files well. I am getting a warning that says
assignment used in conditional context

Code:
awk: awkscript5:30: (FILENAME=file2.txt FNR=175) warning: assignment used in conditional context


Last edited by Chubler_XL; 03-25-2018 at 06:01 PM.. Reason: Fix quotation start missing
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk not working as expected in script emily Shell Programming and Scripting 12 10-25-2014 05:04 PM
Script not working as expected sv0081493 Shell Programming and Scripting 10 02-19-2014 09:05 AM
Nohup not working as expected sebcou UNIX for Dummies Questions & Answers 6 07-31-2013 05:09 PM
sed not working as I expected. What can be the possible reason? Ugenther UNIX for Advanced & Expert Users 5 04-27-2012 04:54 PM
Why this is not working in expected way? meharo Shell Programming and Scripting 4 12-07-2009 08:47 AM
FTP on Linux is not working as expected vidyak Red Hat 1 07-12-2009 08:20 AM
Find cmd not working as expected Vishal123 Shell Programming and Scripting 6 08-29-2007 02:11 AM
#/usr/bin/csh -f not working as expected? effigy Shell Programming and Scripting 4 06-05-2006 05:00 PM
ls not working as expected within ksh GNMIKE Shell Programming and Scripting 10 07-07-2005 07:53 AM
which not working as expected osee Shell Programming and Scripting 2 09-07-2004 11:37 AM


All times are GMT -4. The time now is 03:05 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password