Legends,
Please help me in , how do i subtract the variable values listed like below.
the first value of orig should be subtracted from first value of prev and so on.
san> echo $orig
346 316 340 239 410 107 291 139 128 230 167 147 159 159 172 116 110 260 177 0 177 169 168 186 165 366 195 131 7 143 60 276 149 114 120 110 103 5 0 87 79 104 101 142 184 13 278 2 153 160 0 5 101 127 151 14 196 138 90 177 195 166 135 122 136 117 169 518 244 125 116 127 117 131 129 121 120 164 218 105 111 122 127 123 117 168 536 192 177 133 130 160 78 126 678 277 294 297 842 78
san> echo $prev
346 316 340 239 410 107 291 139 128 230 166 147 159 159 172 116 110 260 177 0 177 167 168 186 165 366 195 131 7 143 60 276 145 114 120 110 103 5 0 87 79 101 99 140 184 13 278 2 153 160 0 5 99 125 150 14 196 136 87 177 195 162 135 121 136 113 169 518 244 125 116 127 117 131 129 119 117 164 218 101 111 122 127 123 116 168 536 192 177 133 130 160 78 126 678 277 294 297 842 78
Pls help
pamu
October 12, 2012, 8:48am
2
try something like this..
assuming both variables have same number of fields..
echo "" | awk -v FV="$orig" -v SV="$prev" '{ n=split(FV,F);split(SV,S)}END{
for(i=1;i<=n;i++){printf (S - F)" "}}'
I am not able to use just awk..
any guess?
i Tried the follwoing using array but no go
for (( i = 0 ; i < ${#orig[@]} ; i++ ))
do
for (( j = 0 ; j < ${#prev[@]} ; j++ ))
do
if [ $i == $j ];
then
(( DIFF = ${orig} - ${prev[j]} ))
echo $DIFF;
else
echo ${orig} ${prev[j]} > /dev/null 2>&1
fi
done
done
---------- Post updated at 04:51 AM ---------- Previous update was at 04:48 AM ----------
@pamu
below is the error i am gettng
> echo "" | awk -v FV="$orig" -v SV="$prev" '{ n=split(FV,F);split(SV,S)}END{for(i=1;i<=n;i++){printf (F - S)" "}}'
awk: syntax error near line 2
awk: bailing out near line 2
In ksh93/bash (assuming that there are equal number of elements in both variables):
orig_array=($orig)
prev_array=($prev)
diff=
for((i=0;i<${#orig_array[*]};i++))
do
diff="$diff $(( ${prev_array} - ${orig_array} ))"
done
echo $diff
producing
0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 -2 0 0 0 0 0 0 0 0 0 0 -4 0 0 0 0 0 0 0 0 -3 -2 -2 0 0 0 0 0 0 0 0 -2 -2 -1 0 0 -2 -3 0 0 -4 0 -1 0 -4 0 0 0 0 0 0 0 0 0 -2 -3 0 0 -4 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 Like
@elixir ,
main requirement is: i have two files File A and File B
using these two files, i want out with difference to third one (say file C) after calculating diff from 3rd column from file
using "bash" shell
file A
abc xxx 340
def yyy 239
ghi zzz 410
file B
abc xxx 330
def yyy 230
ghi zzz 310
OUTPUT SHOLUD be
File C
abc xxx 10 times diff
def yyy 9 times diff
so on...
There is a big difference between how you handle files and variables. You mention variables in one post (and in the thread title) and files in the other.
Please don't try to oversimplify the problem. This makes it difficult to provide a proper solution.
1 Like
initially i tried to get the values in variable , later realized, by "paste" command we can get the values in 3rd file. so tried that option.
pamu
October 12, 2012, 9:32am
9
try this..
awk 'FNR==NR{a[$1,$2]=$3;next}{if(a[$1,$2]){print $1,$2,(a[$1,$2]-$3)" times diff"}}' OFS="\t" file1 file2
if it doesn't work try using nawk
1 Like
pamu:
try this..
awk 'FNR==NR{a[$1,$2]=$3;next}{if(a[$1,$2]){print $1,$2,(a[$1,$2]-$3)" times diff"}}' OFS="\t" file1 file2
if it doesn't work try using nawk
pamu.
Thanks, it is working. just i need to take care of other characters like
"blank lines and "========= HOSTNAME ========" etc
pamu
October 12, 2012, 9:46am
11
sdosanjh:
pamu.
Thanks, it is working. just i need to take care of other characters like
"blank lines and "========= HOSTNAME ========" etc
Could you please provide your input files containing those..
Because i don't think it will print blank lines and '========= HOSTNAME ========"
the file contains like below both files have similar entries file A and file B
=================== DIFF Report on abc host at 10/12 12:21 ==================
abc xxx 340
def yyy 239
ghi zzz 410
=================== DIFF Report on def host at 10/12 12:21 ==================
jkl xxx 340
mno yyy 239
pqr zzz 410
=================== DIFF Report on def host at 10/12 12:21 ==================
...
...
...
pamu
October 12, 2012, 9:57am
13
awk 'FNR==NR{a[$1,$2]=$3;next}{if(a[$1,$2] && $1 !~ "==="){print $1,$2,(a[$1,$2]-$3)" times diff"}}' OFS="\t" file1 file2
1 Like
pamu:
awk 'FNR==NR{a[$1,$2]=$3;next}{if(a[$1,$2] && $1 !~ "==="){print $1,$2,(a[$1,$2]-$3)" times diff"}}' OFS="\t" file1 file2
This is exactly working as i wanted except i missed hostnames now.
anything can be done on not to eliminate hostnames
abc xxx 1 times diff
def yyy 1 times diff
ghi zzz 1 times diff
jkl xxx 1 times diff
mno yyy 1 times diff
pqr zzz 1 times diff
pamu
October 12, 2012, 10:53am
15
Where you want hostnames..? and where are the hostnames in the input.?
Please give your desired output...
1 Like
Actually i modified the filename to get the second column as hostname. so all done.
Thanks for your nawk. it made my day