Subtract field values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtract field values
# 1  
Old 11-19-2009
Subtract field values

I've got a long logfile of the form

Code:
network1:123:45:6789:01:234:56
network2:12:34:556:778:900:12
network3:...

I've got a similar logfile from a week later with different values for each of the fields eg

Code:
network1:130:50:6800:10:334:66
network2:18:40:600:800:999:20
network3:...

How do I script it so that for each network, I take the fields away from each other so that I get the output

Code:
network1:7:5:11:09:100:10
network2:6:6:44:22:99:8
network3:...

I'm guessing to use Perl and use ":" as the split, but I'm not sure how I'd check the network name against each other in the two log files, and how I'd get it to loop to do all the networks listed in the logfile
# 2  
Old 11-19-2009
Try this:

Code:
awk '{
  getline s < "file1"; split(s,a,":")
  n=split($0,b,":")
  out=b[1]
  for(i=2;i<=n;i++) {
    out=out ":" b[i] - a[i]
  }
  print out
}' file2

# 3  
Old 11-19-2009
Seems to work... it gets a bit confused when the network names are not in the same order though. Must check why that is in the logfiles. Should be possible to do a sort on the network name.
# 4  
Old 11-19-2009
Another solution that doesn't care about networks order :
Code:
awk '
BEGIN { FS = OFS = ":" }
NR==FNR {
   for (i=2; i<=NF; i++) old[$1,i] = $i;
   next;
}
{
   out = $1;
   for (i=2; i<=NF; i++) out = out OFS ($i-old[$1,i]);
   print out;
}
' net_old.dat net_new.dat

Jean-Pierre.
# 5  
Old 11-20-2009
Code:
cat f1 f2 | sort | paste -d: - - | awk -F":" '{
cnt=NF/2
printf $1
for(i=2;i<=cnt;i++){
 printf ":"$(i+cnt)-$i
}
print ""
}'

# 6  
Old 11-20-2009
Another one:

Code:
paste <(sort file1) <(sort file2)| awk -F"[: ]" '{ print $1" "($8-$2)" "($9-$3)" "($10-$4)" "($11-$5)" "($12-$6) }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Subtract values based on records

Hi Guys, I am having below tables in oracle T1 ID F_TYPE F_AMT DATE_COL 1 F 6 11-Feb-16 1 D 2 11-Feb-16 1 D 2 11-Feb-16 1 F 6 11-Feb-16 1 F 2 12-Mar-16 1 D 3 12-Mar-16 1 F 4 10-Apr-16 1 F 4 11-Apr-16 1 D 1 11-Apr-16 T2 ID START_DATE END_DATE F_ID FLAG... (0 Replies)
Discussion started by: rohit_shinez
0 Replies

2. Shell Programming and Scripting

awk to subtract from values in file

data.txt: 0,mq_conn_open_error,1444665949,734,/PROD/G/cicsitlp/sys/unikixmain.log,64K,mq_conn_open_error,62022,0,733--734 0,mq_conn_open_error,1444666249,734,/PROD/G/cicsitlp/sys/unikixmain.log,64K,mq_conn_open_error,62022,0,734--734... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Count the field values in a file

Hi I have a file with contents like : 101,6789556897,0000795369 - seq - fmt_recs187] - avg_recs 101,4678354769,0000835783 - seq - fmt_recs98] - avg_recs 221,5679787008,0001344589 - seq - fmt_recs1283] - avg_recs I need to find the sum of the all the values (which are in bold). here... (6 Replies)
Discussion started by: rkrish
6 Replies

4. Shell Programming and Scripting

Add values in 2 columns and subtract from third

Hi All, I have a file with thousands of lines in the following format, where Field1=First 8 characters Field2-9-16 characters Field3=17-26 characters I need to add Field 1 and Field2 and subtract the result from Field 3. Field3=Field3 - (Field1 + Field2) 0012.00 0010.00 0001576.53... (4 Replies)
Discussion started by: nua7
4 Replies

5. Shell Programming and Scripting

Concatenate last field values for all occurences

Hello all, Maybe you can help me with an awk script to get what I need. I have the input file with format below: REQUEST|79023787741690|738227864597|985 REQUEST|79024002151717|738229423534|985 REQUEST|79024002151717|738229423534|*985 NDS-REQUEST|79024002151717|738229423534 ... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

6. Shell Programming and Scripting

Quoting the values in second field

Hi, I have got a file comp_data containing the below data : 38232836|9302392|49 39203827|8203203,3933203|52 72832788|567,3245,2434324|100 This file can have many rows like shown above. I want the values separated by "," in second column(taking "|" as delimiter) to be in quotes. These... (2 Replies)
Discussion started by: msabhi
2 Replies

7. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

8. Shell Programming and Scripting

Find top N values for field X based on field Y's value

I want to find the top N entries for a certain field based on the values of another field. For example if N=3, we want the 3 best values for each entry: Entry1 ||| 100 Entry1 ||| 95 Entry1 ||| 30 Entry1 ||| 80 Entry1 ||| 50 Entry2 ||| 40 Entry2 ||| 20 Entry2 ||| 10 Entry2 ||| 50... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

9. Shell Programming and Scripting

Need help with switching field/column values

Hi all, I need some help on switching field/column values. For example I have a file name data.txt which contains: a b a b a b and I want to switch a and b and save it to the same file. the file data.txt then will have: b a b a b a The problem is, well, I know how to... (7 Replies)
Discussion started by: sonyd8
7 Replies

10. Shell Programming and Scripting

Subtract 100 from first field in long list? Simple manipulation?

It sounds so easy to do. I have a file thats laid out like this.. number text text text text (etc about 15 times with various text fields) I want to take the first field, "number", subtract 100 from it, and then put it back in the file. a simple little manipulation of the first field in... (4 Replies)
Discussion started by: LordJezo
4 Replies
Login or Register to Ask a Question

Featured Tech Videos