this is my file
air1.txt
fc:75:16:d2:91:a3 -90 targol
78:54:2e:7f:e8:9e -88 DLink
fc:75:16:d2:91:a3 -89 targol
78:54:2e:7f:e8:9e -89 DLink
78:54:2e:7f:e8:9e -88 DLink
78:54:2e:7f:e8:9e -87 DLink
fc:75:16:d2:91:a3 -90 targol
I want to calculate the average of second column for each name in the third column! Here is my scrip!
RSSI=$(awk '{print $3}' air1.txt | sort -u | while read line; do awk < air1.txt '{print $2,$3}' | grep $line | ./rssiMean.sh |cut -d'.' -f1 |awk '{print $line,$1}' ;done)
echo $RSSI
but the result is
-88 -88 -89 -89
Why I can't get $line?!
BTW ./rssiMean.sh calculate the average!
This should do:
awk '{a[$3]+=$2;b[$3]++} END {for (i in a) print i,a[i]/b[i]}' air1.txt
DLink -88
targol -89.6667
It sum up number for every data in column #3 and divide it by number of hits.
You cannot use bash variables as such in awk script.
Rather use a awk variable and assign the value using the -v paramter
eg:
$var=123
$awk -v awkvar=$var '{print awkvar}'
here awkvar is an awk varible created and passed with value of $var
to make this change in your script
RSSI=$(awk '{print $3}' air1.txt | sort -u | while read line; do awk < air1.txt '{print $2,$3}' | grep $line | ./rssiMean.sh |cut -d'.' -f1 |awk -v line=$line '{print line,$1}' ;done)
echo $RSSI
The change made is
awk -v line=$line '{print line,$1}
awk varible line is assigned with value of bash varibale $line
Related
AWK code:
top -b|head -20|awk '/PID/,EOF {print $0}'|grep -v PID|while read line; do awk -v MYHOST=$(hostname) '{print "topstat,host="MYHOST",PID="$1" USER="$2",PR="$3",NI="$4",VIRT="$5",RES="$6",SHR="$7",STATE="$8",%CPU="$9",%MEM="$10",TIME="$11,COMMAND="$12}';done
The o/p line(1 line for e.g) is
topstat,host=host1.abc.com,PID=14 USER=root,PR=rt,NI=0,VIRT=0,RES=0,SHR=0,STATE=S,%CPU=0.0,%MEM=0.0,TIME=0:00.36,COMMAND=migration/1
I need the O/p
topstat,host=host1.abc.com,PID=14 USER=root,PR=rt,NI=0,VIRT=0,RES=0,SHR=0,STATE="S",%CPU=0.0,%MEM=0.0,TIME=0:00.36,COMMAND="migration/1"
Any help?
You need to use \ escape character
top -b|head -20|tail -n +6 | awk -v MYHOST=$(hostname) '{print "topstat,host="MYHOST",PID="$1" USER="$2",PR="$3",NI="$4",VIRT="$5",RES="$6",SHR="$7",STATE=\""$8"\",%CPU="$9",%MEM="$10",TIME="$11,COMMAND="$12}'
I am trying to split a variable as follows. is there any efficient way to do this preferably using awk.
echo 262146*10,69636*32 |awk -F, 'split($1, DCAP,"\\*") {print DCAP[1]}; split($2, DCAP,"\\*"){print DCAP[1]}'
echo '262146*10,69636*32' | awk -F '[,*]' '{print $1; print $3}'
or
echo '262146*10,69636*32' | awk -F '[,*]' '{printf("%d\n%d\n",$1,$3)}'
Output:
262146
69636
If you have a longer sequence you could try:
echo 262146*10,69636*32,10*3 | awk 'BEGIN {FS="*"; RS=","} {print $1}'
can you help to improve this code to be faster .. with 50000 lines in my file this take a lot time.
I appreciate your help
input
17/11/27 03:13:50:480000
17/11/27 03:12:54:380000
17/11/27 03:14:39:980000
output
1195787648480000
1195787592380000
1195787697980000
my code
ts=$(date -d'01/06/1980 00:00:00' +%s)
lap=18
cat file |
while read tt
do
dt=`echo $tt | awk '{print $1}' | awk -F"/" '{print $2"/"$3"/"$1}'`
tm=`echo $tt | awk '{print substr($2,1,8)}'`
ms=`echo $tt | awk '{print $2}' | awk -F":" '{print $NF}'`
line=`echo $dt" " $tm`
echo $line\ $(date -d "${line/// }" "+%s") |
awk '{print (($3 - '$ts') + '$lap')'$ms'}'
done
Please, help me to improve my code to get results faster.
Many thanks.
With single GNU awk process:
awk -F'[[:space:]]*|/|:' -v ts=$(date -d'01/06/1980 00:00:00' +%s) -v lap=18 '{
print (mktime(sprintf("20%d %d %d %d %d %d",$1,$2,$3,$4,$5,$6)) - ts)+lap $NF
}' file
The output:
1195791248480000
1195791192380000
1195791297980000
Enjoy )
similar with gawk
$ awk -F'[/: ]' -v ts=$(date -d'01/06/1980' +%s) \
-v lap=18 '{ms=$NF; $NF=""; d=sprintf(20$0);
print mktime(d)+lap-ts ms}' file
1195787648480000
1195787592380000
1195787697980000
I have a problem using an awk variable to insert into a field 2 of a file. This is my data:
data='AAA||CCC|DDD|EEE|FFF'
ds='BBB'
I want to insert the value of variable ds in field 2 of my file. So I written some test code to view the behavior in awk:
$ echo "$data" | awk -F'|' -v tmp="$ds" '{print $1}'
AAA <== that works ... now I try to print my variable
$ echo "$data" | awk -F'|' -v tmp="$ds" '{print $1 $tmp}'
AAAAAA||CCC|DDD|EEE|FFF <== output but I was expecting AAA|BBB
I also tried this:
$ echo "$data" | awk -F'|' -v tmp="$ds" '{print $tmp}'
AAA||CCC|DDD|EEE|FFF <== output but I was expecting BBB
What am I doing wrong??
In awk, you don't put $ in front of a variable to get its value like you do in bash. Just put the variable name:
$ echo "$data" | awk -F'|' -v tmp="$ds" '{print tmp}'
BBB
The $ is only used to get the value of a field (as in $1, $2, etc.). When putting $var, the field is based on the numeric value of var. If the value of var is 7, e.g., $var is the value of the 7th field. If var is 0 or some non-numeric value, $var is the same as $0, which is the whole line as you saw in your attempts.
I'm trying to process an input which has two field seperators ; and space. I'm able to parse the input with one separator using:
echo "10.23;7.15;6.23" | awk -v OFMF="%0.2f" 'BEGIN{FS=OFS=";"} {print $1,$2,$3}'
10.23;7.15;6.23
For an input with two seperators, I tried this and it doesn't parse both the seperators:
echo "10.23;7.15 6.23" | awk -v OFMF="%0.2f" 'BEGIN{FS=OFS=";" || " "} {print $1,$2,$3}'
You want to set FS to a character list:
awk -F'[; ]' 'script' file
and the other builtin variable you're trying to set is named OFMT, not OFMF:
$ echo "10.23;7.15 6.23" | awk -F'[; ]' -v OFMT="%0.2f" '{print $1,$2,$3}'
10.23 7.15 6.23
$ echo "10.23;7.15 6.23" | awk 'BEGIN{FS="[; ]"; OFS=";"; OFMT="%0.2f"} {print $1,$2,$3}'
10.23;7.15;6.23