$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
$ /bin/awk -V | head -n1
bash: /bin/awk: No such file or directory
I have gotten a script that helps me to parse, read, and output the data in a specific format, over on (Unix and Linux) Ubuntu 16. Although I am not getting any errors but the script is NOT producing the result that I expect.
What the script does? I have a large input data file data.txt (see below); for every timestamp, I need to find the nearest time match 5 minutes later and the value on column 4 and then print both in two new columns.
The logic for the match is - Calculate the time 5 minutes later from the current time stamp, now get the value closes to this time within a 2 mins limit in either direction, for example lets say time now is 00:00:00.000 get the value no sooner that 00:03:00.000 and no later than 00:07:00.000, else print NA NA. Not to mention that the final matches will all show NAs.
for example for
Code:
2018-02-16 16:45:29.557 farads 0.0004300000
I need to find the best time available 5 minutes later (i. e. approximately)
Please note that there are no exact time matches here, that is why only the nearest match is found. Also the code should seamlessly work when the date changes from 2018-02-16 to 2018-02-17.
However, this script is not outputting things properly. The expected output should look like this
awk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to
uninitialized element `matchfor["1518802491.175"]'
gawk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to
uninitialized element `matchfor["1518804802.429"]'
gawk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to
uninitialized element `matchfor["1518801331.044"]'
gawk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to
uninitialized element `matchfor["1518799413.167"]'
Can the experts please help
Last edited by delbroooks; 03-21-2018 at 11:41 PM..
is printing only "NA NA" in the last two columns it's not matching anything
Code:
2018-02-16 17:02:19.786 farads 0.0006600000 NA NA
2018-02-16 17:03:44.343 farads 0.0003300000 NA NA
2018-02-16 17:04:24.996 farads 0.0002200000 NA NA
2018-02-16 17:05:48.512 farads 0.0004600000 NA NA
2018-02-16 17:06:29.248 farads 0.0003700000 NA NA
2018-02-16 17:20:22.002 farads 0.0000700000 NA NA
2018-02-16 17:07:50.392 farads 0.0005500000 NA NA
2018-02-16 17:21:43.477 farads 0.0002000000 NA NA
2018-02-16 17:08:32.397 farads 0.0002000000 NA NA
2018-02-16 17:22:27.400 farads 0.0003500000 NA NA
2018-02-16 17:09:57.688 farads 0.0003100000 NA NA
2018-02-16 17:23:52.907 farads 0.0001100000 NA NA
2018-02-16 17:24:40.392 farads 0.0001500000 NA NA
2018-02-16 17:25:23.026 farads 0.0001400000 NA NA
2018-02-16 17:26:45.192 farads 0.0001900000 NA NA
2018-02-16 17:40:33.633 farads 0.0001500000 NA NA
I request you run the script in your Ubuntu or UNIX machine in debug mode and see if you can replicate the results.
Last edited by delbroooks; 03-21-2018 at 06:28 PM..
Reason: tried the suggested changes
Is this ms precision to eliminate duplicate indices? ms wouldn't make sense if you're dealing with approximate 5 min?
Quote:
Code:
.
.
.
min=(0 + "INF") # whatever this means - it will yield min = 0
.
.
.
if (difference < 0) difference=-difference
if (difference < min) {
min=difference
matchfor[record]=records[others]
}
.
.
.
With min = 0 and difference ALWAYS positive, the if branch will NEVER be entered. So - no surprise matchfor is undefined / uninitialized:
Quote:
Code:
awk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to
uninitialized element `matchfor["1518802491.175"]'
.
.
.
Would you mind to take a step back and rephrase your requirements? Like "For any given record take its time stamp, add five minutes, and from there find the line in the data file with the smallest absolute delta time and print it as a suffix to the actual line"
@RudiC I have rephrased the question above. Please see the logic of the match. Basically there is 2 minutes time window on either side to find a match, if none found it prints NAs
To your question weather the code runs- the answer is absolutely yes the code runs. May I request to you copy and paste the code as well as the data.txt file in your ubuntu terminal and run it to see if the results are replicated.
You could try this script. I believe your OS should come with GNU awk by default so it's much more efficient to use the mktime() feature to convert these times.
Code:
#!/usr/bin/awk -f
{
day=$1
gsub(/-/, " ", day)
split($2, t, ".")
gsub(/:/, " ", t[1])
x=mktime(day " " t[1]) + t[2] / 1000
rectime[NR]=x
records[NR]=$0
}
END {
offset=5*60
max=2*60
for (rec in rectime) {
cur=rec + 1
target = rectime[rec] + offset
offsetmin = target - max
offsetmax = target + max
best = 9999999
found = 0
while(cur in rectime && rectime[cur] < offsetmax) {
if (rectime[cur] < target && rectime[cur] > offsetmin &&
best > target - rectime[cur]) {
best= target - rectime[cur]
found=cur
}
if (rectime[cur] >= target) {
if(best > rectime[cur] - target) {
best=rectime[cur] - target
found=cur
}
break
}
cur++
}
if (found)
print records[rec] " " records[found]
else
print records[rec] " NA NA"
}
}
Last edited by Chubler_XL; 03-22-2018 at 12:22 AM..
This User Gave Thanks to Chubler_XL For This Post:
Hi,
I have a script below that is running ssh <host> <command> on some servers.
Below is more or less the script. I have to modify it somehow to get rid of the 'confidential' hostnames
check_log.bash
#!/bin/bash
#
myPID=$$
parse_log ()
{
sub="parse_log"
host=${1}
... (2 Replies)
Hi All,
I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination.
# file_copy.sh
source_dir = ${source_dir}
target = ${target_dir}
cp source_dir target
and my parameter file is like below
#parameter_file.txt
source_dir =... (4 Replies)
Hi Experts,
Need your kind help with gsub awk.
Below is my pattern:"exec=1_host_cnt=100_dup=4_NameTag=targetSrv_500.csv","'20171122112948"," 100"," 1"," 1"," 4","400","","",
" aac sample exec ""hostname=XXXXX commandline='timeout 10 openssl speed -multi 2 ; exit 0'"" ","-1","-1","1","... (6 Replies)
Following one line of awk code removes first 3 characters from each line but when I run the same code on another linux platform it doesn't work and only prints blank lines for each record. Can anyone please explain why this doesn't work? (31 Replies)
Dear all,
I had script which used to work, but recently it is not working as expected.
I have command line in my shell script to choose the following format from the output_elog and perform some task afterwards on
As you see, I want all numbers in foramt following RED mark except for... (12 Replies)
Hi,
I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong.
"If else" part in below script is not working basically.
I am running it on HP-UX.
for i in slpd puma sfmdb
do
echo "******\t$i\t*******"
echo... (10 Replies)
Hi!
Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting:
servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')"
This will essentially pare down a line like this:
... (7 Replies)
I am facing some strange problem.
I know, there is only one record in a file 'test.txt' which starts with 'X'
I ensure that with following command,
awk /^X/ test.txt | wc -l
This gives me output = '1'.
Now I take out this record out of the file, as follows :
awk /^X/ test.txt >... (1 Reply)
Hello.
Consider the following magic words:
# ls `which adduser`
ls: /usr/sbin/adduser: No such file or directory
#
Hmmm...
Then:
# ls /usr/sbin/adduser
/usr/sbin/adduser
#
Now what?
Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)