AWK script help required


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK script help required
# 1  
Old 06-27-2008
CPU & Memory AWK script help required

Hello every one
i have a very long file 'file1' like this


HTML Code:
<K>1</K>
</Condition>
<Tariff>Rate
<Price>1.27</Price>
<Interval>30</Interval>
</Tariff>
</Node>
<Node>NonFaF
<Tariff>Rate
<Price>1.9</Price>
<Interval>30</Interval>
</Tariff>
</Node>
</Node>
</Node>
<Node>FaF
<Condition>FriendsAndFamily
<K>1</K>
</Condition>
<Tariff>Rate
<Price>1.27</Price>
<Interval>30</Interval>
</Tariff>
</Node>
<Node>NonFaF
<Tariff>Rate
<Price>1.9</Price>
<Interval>30</Interval>
</Tariff>
</Node>
</Node>
<Node>International
<Node>LCR
<Node>group0_LCR
<Condition>NumberList
<Type>0</Type>
and i have file2 which is like this

HTML Code:
1.27      1.56
3.45      4.25
1.90      2.10
Now i want script to traverse through file1 and stops whereever he finds '<Price>1.9</Price> ' , compare the numercial value (1.9 in this case ) with all values of column 1 of file2 and where he finds match, then replaces it with <Price>value in file2 of second column</Price>
I am also working on it and would appreciate your help a lot

Regards
# 2  
Old 06-27-2008
Ok i have extracted out the price with this code , now all i need is comparison and replacement

HTML Code:
#!/bin/sh
nawk -v Node '
   /^<Price>/ {
      split($0, a, ">");
      split(a[2], b, "<");
      source=b[1];

      { print(source);}                    
   }
   ' test1`
and i want prices to be replaced in file1 Smilie

Regards
# 3  
Old 06-27-2008
ok now , since column2 value is some fraction multiplied by column1 value
so i have calculated it
now i all i want is to replace <Price> , which newly calculated price , code is given below

HTML Code:
#!/bin/sh
nawk -v Node '
   /^<Price>/ {
      split($0, a, ">");
      split(a[2], b, "<");
      source=b[1];
     source1 = source/100
      source2 = source - ( source1*26)
      source3 = source2 + ( source1*31)
      [B][I]b[1] = source3[/I][/B]
      { print(source3);}
   }
   ' test1`
I have tried replacing it in bold/italic line , but its not working Smilie
Regards
# 4  
Old 06-27-2008
Code:
nawk > file1_new 'NR == FNR { _[$1] = $2; next }
(NF == 3 && $2 in _ && $0 = "<Price>"_[$2]"</Price>")||
1' file2 FS='</*Price>' file1

If file_new contains what you want:

Code:
nawk > file1_new 'NR == FNR { _[$1] = $2; next }
(NF == 3 && $2 in _ && $0 = "<Price>"_[$2]"</Price>")||
1' file2 FS='</*Price>' file1 && mv file1_new file1

It seams a bit dangerous to change prices like this Smilie
# 5  
Old 06-27-2008
Thanks a lot for your reply but i have confirmed that i have to calculate every price , given below code is working fine , its giving me new prices with correct values
i just want these values to be there in original file inplace of old prices
HTML Code:
#!/bin/sh
nawk -v Node '
   /^<Price>/ {
      split($0, a, ">");
      split(a[2], b, "<");
      source=b[1];
      source1 = source/100
      source2 = source - ( source1*26)
      source3 = source2 + ( source1*31)
      $b[1] == source3

      { print(source);}
   }
   ' test1`
test1 is the input file
# 6  
Old 06-27-2008
I cannot understand the code above (what is Node?).
You already posted a sample of the input, could you post an example of the output?
# 7  
Old 06-27-2008
<Node> is just a pivoting point in text , it isnt that important
well lemme explain it again Smilie
i have this line in my input file

<Price>12.65</Price>

I want to 12.65 of this multiplied by 6% Smilie
thts all
like output file should have this line as

<Price>13.05</Price>

in my code , i have extracted 12.65 and multiplied it with my multiplication factor and output is in variable source3
now all i want is this new price to be there in original file Smilie

Regards
and Waiting
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk help required

Hello, I am trying to get an output from awk and have been unsuccessful so far. There is an input file which contains certain segments, that needs to be read and append with another data after matching the segment... Input file is as shown below ISA*00* *00* *01*781495650... (2 Replies)
Discussion started by: rakeshv
2 Replies

2. Shell Programming and Scripting

awk script required to compare two files

HI friends, I have two files. File2 2nd column need to be compared with file1 3rd column.If ID match found, then save in different file as file1 data followed by file2. file1: No;age;id;name 1;24;37h;name1 2;22;67e;name2 4;48;9r;name3 6;67;9i8;name4 file2: exp;id;pos... (9 Replies)
Discussion started by: vasanth.vadalur
9 Replies

3. Shell Programming and Scripting

awk script required

File 1 ################### TRAIU DSKDL; SLLA ;LAKJA KJAJA NSAJAN JANAL AJKJA JAJALA KAKAK JA AKA AKA AJ A A PPIN TRY1 SANT1 PPIN TRY2 SANT2 PPIN TRY3 SANT3 PPIN TRY4 SANT4 PPIN TRY5 SANT5 AJJA NA ANA ANHDJLD ALJALJA AJLJAJD LALJAL ALJALJA ALJALJA (4 Replies)
Discussion started by: jaita
4 Replies

4. UNIX for Advanced & Expert Users

Solution required for awk script

Hi Jim, The following script is in working state. But i m having one more problem with awk cmd. Could you tell me how to use any variable inside awk or how to take any variable value outside awk. My problem is i want to maintain one property file in which i am declaring variable value into that... (1 Reply)
Discussion started by: Ganesh Khandare
1 Replies

5. Shell Programming and Scripting

Awk help required

Hi All, I have 2 .csv files as below File1.csv name1,20,30,date1 name2,30,40,date1 File2.csv name1,25,35,date1 name2,45,55,date1 Can any one help me to add the 2nd and 3rd column of file1 and file2 and write the output into a new file as below name1,45,65,date1... (9 Replies)
Discussion started by: ajay547
9 Replies

6. Shell Programming and Scripting

Help required with awk/sed

Hi I have a file, with format like: column1|coulumn2|column3|column4 A|X|K|18 L|O|R|31,42,25 G|H|I|55,66 L|E|Q|25,31,94 output required: column1|coulumn2|column3|column4 A|X|K|18 L|O|R|31,25 L|E|Q|25,31 Input File Format: All columns are seperated using |, last column... (8 Replies)
Discussion started by: New to awk
8 Replies

7. Shell Programming and Scripting

Help required in sed or awk.

Hi All, I need to pick up data on both sides of "=" sign. For eg, following is the context that I have. 125.156.125.147=machine1 147.125.185.156=machine2 147.125.185.159=machine3 Can I have the ip address in one variable and machine name in another variable using sed or awk. ... (1 Reply)
Discussion started by: nua7
1 Replies

8. Shell Programming and Scripting

awk script required for finding records in 1 file with corresponding another file.

Hi, I have a .txt file (uniqfields.txt) with 3 fields separated by " | " (pipe symbol). This file contains unique values with respect to all these 3 fields taken together. There are about 40,000 SORTED records (rows) in this file. Sample records are given below. 1TVAO|OVEPT|VO... (2 Replies)
Discussion started by: RRVARMA
2 Replies

9. Shell Programming and Scripting

Help required on awk

Hi, I have a fixed with file, which have almost 10 records Sample File: ------------ AR 100 TTT BC 200 SSS DA 199 YYY AR 500 RRR PO 300 QQQ PB 800 PPP PC 150 OOO AR 111 CCC AD 321 LLL I Have to check for first two charactors and if its 'AR' i have to save those records to one... (3 Replies)
Discussion started by: meetavin
3 Replies

10. UNIX for Dummies Questions & Answers

Help required on AWK command

I have two questions: (Q1) I am using a loop and want to print a particular field of a table using the AWK command by accessing one row at a time by validating NR with the loop counter. I am using a command like this but it isn't working! count=1 NAWK -F"|" -v var=$count 'NR == var { printf... (13 Replies)
Discussion started by: udiptya
13 Replies
Login or Register to Ask a Question