Why does mktime return -1 in Awk? - shell

I have file with below data
# date format in file is (YYYY MM DD HH MM SS)
file_create_date file_execute_date file_name
"2017 10 10 12 24 30" , "2017 10 11 09 23 00" , LTE--cNum--T201710110923--W101114450--CHGLSMR01LEMS1--sam--4.0.csv.gz
i want output like this
file_create_date file_execute_date difference file_name
"2017 10 10 12 20 30" , "2017 10 11 13 31 59" , "1 Day 01:11:29, "LTE--cNum--T201710110923--W101114450--CHGLSMR01LEMS1--sam--4.0.csv.gz"
my script is :
awk -F',' '{print $1 "," , $2 , (mktime($2)-mktime($1))/86400 " Day",$3}' Filename
But mktime() returns -1.

You're passing mktime a string containing double quotes and blank chars at the start and end. Remove those.
$ echo '"2017 10 10 12 24 30" , "2017 10 11 09 23 00"'
"2017 10 10 12 24 30" , "2017 10 11 09 23 00"
$ echo '"2017 10 10 12 24 30" , "2017 10 11 09 23 00"' | awk -F',' '{print mktime($1)}'
-1
$ echo '"2017 10 10 12 24 30" , "2017 10 11 09 23 00"' | awk -F',' '{gsub(/\s*"\s*/,""); print mktime($1)}'
1507656270

Related

Count occurrences in a text line

Is there any way to count how often a value occurs in a line?. My input is a tab delimited .txt file. It looks something like this (but with thousands of lines):
#N/A 14 13 #N/A 15 13 #N/A 14 13 13 15 14 13 15 14 14 15
24 26 #N/A 24 22 #N/A 24 26 #N/A 24 26 24 22 24 22 24 26
45 43 45 43 #N/A #N/A #N/A 43 45 45 43 #N/A 47 45 45 43
I would like an output like this or similar.
#N/A(3) 14 13(3) 15 13(1) 13 15(1) 15 14(1) 14 15 (1)
24 26(4) #N/A(3) 24 22(3)
45 45(4) #N/A(4) 43 45(1) 47 45(1)
Perl solution:
perl -laF'/\t/' -ne '
chomp; my %h;
$h{$_}++ for #F;
print join "\t", map "$_ ($h{$_})", keys %h
' < input
-a splits each line on -F (\t means tab) into the #F array
-l adds newlines to prints
-n reads the input line by line
chomp removes the final newline
%h is a hash table, the keys are the members of #F, the values are the counts
awk to the rescue!
$ awk -F' +' -v OFS=' ' '{for(i=1;i<=NF;i++) if($i!="")a[$i]++;
for(k in a) printf "%s", k"("a[k]")" OFS; delete a; print ""}' file
#N/A(3) 14 13(3) 13 15(1) 15 13(1) 14 15(1) 15 14(1)
#N/A(3) 24 22(3) 24 26(4)
#N/A(4) 43 45(1) 45 43(4) 47 45(1)

Get the average of the selected cells line by line in a file?

I have a single file with the multiple columns. I want to select few and get average for selected cell in a line and output the entire average as column.
For example:
Month Low.temp Max.temp Pressure Wind Rain
JAN 17 36 120 5 0
FEB 10 34 110 15 3
MAR 13 30 115 25 5
APR 14 33 105 10 4
.......
How to get average temperature (Avg.temp) and Humidity (Hum)as column?
Avg.temp = (Low.temp+Max.temp)/2
Hum = Wind * Rain
To get the Avg.temp
Month Low.temp Max.temp Pressure Wind Rain Avg.temp Hum
JAN 17 36 120 5 0 26.5 0
FEB 10 34 110 15 3 22 45
MAR 13 30 115 25 5 21.5 125
APR 14 33 105 10 4 23.5 40
.......
I don't want to do it in excel. Is there any simple shell command to do this?
I would use awk like this:
awk 'NR==1 {print $0, "Avg.temp", "Hum"; next} {$(NF+1)=($2+$3)/2; $(NF+1)=$5*$6}1' file
or:
awk 'NR==1 {print $0, "Avg.temp", "Hum"; next} {print $0, ($2+$3)/2, $5*$6}' file
This consists in doing the calculations and appending them to the original values.
Let's see it in action, piping to column -t for a nice output:
$ awk 'NR==1 {print $0, "Avg.temp", "Hum"; next} {$(NF+1)=($2+$3)/2; $(NF+1)=$5*$6}1' file | column -t
Month Low.temp Max.temp Pressure Wind Rain Avg.temp Hum
JAN 17 36 120 5 0 26.5 0
FEB 10 34 110 15 3 22 45
MAR 13 30 115 25 5 21.5 125
APR 14 33 105 10 4 23.5 40

How do i compare the value located in one file with the other file

File Mem.txt
[root#mavenir Sudhakar]# cat MEM.txt | awk '{print $4,$5}'
Output:
CARD_0-1 12
CARD_0-10 13
CARD_0-11 13
CARD_0-12 28
CARD_0-13 2
CARD_0-14 2
CARD_0-2 30
CARD_0-3 13
CARD_0-4 29
CARD_0-9 24
CARD_1-1 13
CARD_1-10 28
CARD_1-11 13
CARD_1-12 28
CARD_1-13 29
CARD_1-14 13
CARD_1-2 30
CARD_1-3 13
CARD_1-4 28
CARD_1-5 10
CARD_1-6 28
CARD_1-9 13
[root#mavenir Sudhakar]# cat cardnum.txt
0-1
0-3
0-11
1-1
1-3
1-5
1-9
1-11
1-13
these are the two file where i need to select the value of the 2nd value from the MEM.txt file is the card num exist in cardnum.txt file.
the output should be like this
0-1 12
0-3 13
0-11 13
1-1 13
1-3 13
1-5 10
1-9 13
1-11 13
1-13 29
Here, now this should work.
#!/bin/bash
while lead line; do
val=$(echo "$line" | awk -F _ '{print $2}');
echo "$val";
done < mem.txt

Get next month in terminal

In OSX on the terminal, how can cal be used to obtain the calendar of the next month?
i.e. the following
command: "cal -m && date +%m +1"
should equate to
command: "cal -m 7"
and produce:
July 2014
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Use this:
m=$(date +'%m'); ((m++)) ; cal -m $m
or this:
cal $(date -v+1m +'%m %y')

UNIX SHELL Script Replace text with Sed,i have the program working,but for a simple particular case it is not working,how can i correct it?

Problem statement is
If todays date is single digit,then replace that date with single *
if the today date is double digit,then replace it with double *
my code works for single digit date.
but my sed code is unable to substitute double digit with double **.
Here is my code:
#!/bin/sh
set `date`
if [ $3 -le 9 ]
then
n=`cal | tail -n +3 | grep -n "$3"| cut -d ":" -f1 | head -n 1`
n=`expr $n + 2`
cal | sed "$n s/$3/*/"
else
cal | sed "s/$3/**/"
fi
whats the error in this line
cal | sed "s/$3/**/"
assume $3 contains value 19.
here's the output of cal | cat -vte
abhijith#abhijith-compaq-420:~/Desktop$ cal | cat -vte
December 2013 $
Su Mo Tu We Th Fr Sa $
1 2 3 4 5 6 7 $
8 9 10 _^H1_^H1 12 13 14 $
15 16 17 18 19 20 21 $
22 23 24 25 26 27 28 $
29 30 31 $
$
cal | sed "s/$3/**/" works fine for me. But I recommend to rewrite your script this way:
#!/bin/sh
day=`date +%d`
case $day in
?) cal | sed "s/ $day/ */" ;;
??) cal | sed "s/$day/**/" ;;
esac
UPDATE
Since you say it's not working for you, let's get back to the basics. What will be the output of this script:
#!/bin/sh
day=9; cal | sed "s/ $day/ */"
day=19; cal | sed "s/$day/**/"
I get:
December 2013
S M Tu W Th F S
1 2 3 4 5 6 7
8 * 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
December 2013
S M Tu W Th F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 ** 20 21
22 23 24 25 26 27 28
29 30 31

Resources