Search for particular tag and arrange as coordinates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for particular tag and arrange as coordinates
# 1  
Old 10-23-2010
Search for particular tag and arrange as coordinates

Hi

I have a file whose sample contents are shown here,

Code:
1.2.3.4->2.4.2.4 a(10) b(20) c(30)
1.2.3.4->2.9.2.4 a(10) c(20)
2.3.4.3->3.6.3.2 b(40) d(50) c(20)
2.3.4.3->3.9.0.2 a(40) e(50) c(20)
1.2.3.4->3.4.2.4 a(10) c(30)
6.2.3.4->2.4.2.5 c(10)
.
.
.
.


Here I need to search for c in every line and then get content of c and then need to arrange in form of (x,y) coordinate as (c,proportion of cumulative c) till end of file. This means (as shown in above sample) value of c is 30 and occurs for 2 times, 20 for 3 times and 10 for 1 time, so the co-ordinates would be in increasing order as : (10, 10*1/(10*1 +20*3 + 30*2)), (20, 20*3/(10*1 +20*3 + 30*2)) , (30, 30*2/(10*1 +20*3 + 30*2)) , .... till eof

Yours help is highly appreciated.
# 2  
Old 10-23-2010
Code:
awk '{for (i=2;i<=NF;i++) {if ($i~/^c/) {t=gensub(/^c\((.*)\)$/,"\\1","g",$i);a[t]++;s=s+t}}}
END {for ( i in a) printf "(%s, %.4f)\n",i,i*a[i]/s |"sort -n"}' infile

(10, 0.0769 )
(20, 0.4615 )
(30, 0.4615 )

# 3  
Old 10-23-2010
As I posted the o/p is in the form of (value of c, proportionate cumulative value of c)
means. (10, 10*1/(10*1 +20*3 + 30*2)), (20, 20*3/(10*1 +20*3 + 30*2)) , (30, 30*2/(10*1 +20*3 + 30*2))....
=> (10, 10/130), (20, 60/130) , (30, 60/130)

i.e Final O/P

Code:
(10, 0.0769), (20, 4615) , (30, 0.4615) ,... till end of file

# 4  
Old 10-23-2010
Ok, if you need print in one line:

Code:
 awk '{for (i=2;i<=NF;i++) {if ($i~/^c/) {t=gensub(/^c\((.*)\)$/,"\\1","g",$i);a[t]++;s=s+t}}}
END {for ( i in a) printf "(%s, %.4f)\n",i,i*a[i]/s |"sort -n"}' infile |tr "\n" ", "

(10, 0.0769),(20, 0.4615),(30, 0.4615),



---------- Post updated at 11:46 PM ---------- Previous update was at 11:42 PM ----------




Forget to say, you need gawk (gensub only supported in gawk)
This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 10-25-2010
perl

Code:
while(<DATA>){
  if(/c\((\d+)\)/){
   $hash{$1}+=1;
   $cnt+=$1;
  }
}
foreach my $key (keys %hash){
  print "($key,",($key*$hash{$key})/$cnt,")\n";
}
__DATA__
1.2.3.4->2.4.2.4 a(10) b(20) c(30)
1.2.3.4->2.9.2.4 a(10) c(20)
2.3.4.3->3.6.3.2 b(40) d(50) c(20)
2.3.4.3->3.9.0.2 a(40) e(50) c(20)
1.2.3.4->3.4.2.4 a(10) c(30)
6.2.3.4->2.4.2.5 c(10)

# 6  
Old 10-25-2010
Code:
while(<DATA>){
  if(/c\((\d+)\)/){
   $hash{$1}+=1;
   $cnt+=$1;
  }
}
foreach my $key (keys %hash){
  printf "(%s, %.4f), ", $key,($key*$hash{$key})/$cnt;
}
__DATA__
1.2.3.4->2.4.2.4 a(10) b(20) c(30)
1.2.3.4->2.9.2.4 a(10) c(20)
2.3.4.3->3.6.3.2 b(40) d(50) c(20)
2.3.4.3->3.9.0.2 a(40) e(50) c(20)
1.2.3.4->3.4.2.4 a(10) c(30)
6.2.3.4->2.4.2.5 c(10)

result is:

Code:
(30, 0.4615), (10, 0.0769), (20, 0.4615),

But how to sort $key, before print it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed search and replace after xml tag

Hi All, I'm new to sed. In following XML file <interface type='direct'> <mac address='52:54:00:86:ce:f6'/> <source dev='eno1' mode='bridge'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> ... (8 Replies)
Discussion started by: varunrapelly
8 Replies

2. Shell Programming and Scripting

Search for a tag and display a message if not found.

Hi All, I am working with a XML file. Below is part for the file. <Emp:Profile> <Emp:Description>Admin</Emp:Description> <Emp:Id>12347</Emp:Id> </Emp:Profile> <Emp:Profile> ... (7 Replies)
Discussion started by: Girish19
7 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

Search for a html tag and print the entire tag

I want to print from <fruits> to </fruits> tag which have <fruit> as mango. Also i want both <fruits> and </fruits> in output. Please help eg. <fruits> <fruit id="111">mango<fruit> . another 20 lines . </fruits> (3 Replies)
Discussion started by: Ashik409
3 Replies

5. Shell Programming and Scripting

How to retrieve the value from XML tag whose end tag is in next line

Hi All, Find the following code: <Universal>D38x82j1JJ </Universal> I want to retrieve the value of <Universal> tag as below: Please help me. (3 Replies)
Discussion started by: mjavalkar
3 Replies

6. Shell Programming and Scripting

Need an efficient way to search for a tag in an xml file having millions of rows

Hi, I have an XML file with around 1 billion rows in it and i am trying to find the number of times a particular tag occurs in it. The solution i am using works but takes a lot of time (~1 hr) .Please help me with an efficient way to do this. Lets say the input file is <Root> ... (13 Replies)
Discussion started by: Sheel
13 Replies

7. Shell Programming and Scripting

awk to search similar strings and arrange in a specified pattern

Hi, I'm running a DB query which returns names of people and writes it in a text file as shown below: Carey, Jim; Cena, John Cena, John Sen, Tim; Burt, Terrence Lock, Jessey; Carey, Jim Norris, Chuck; Lee, Bruce Rock, Dwayne; Lee, Bruce I want to use awk and get all the names... (9 Replies)
Discussion started by: prashu_g
9 Replies

8. Shell Programming and Scripting

Multiple Tag Search and Printing

Scenario: The following text belongs to a .doc file File: check1.asmFunction: MonksTag: NoTag: 001Tag: YesTag: 002File: check2.asmFunction: Perl MonksTag: YesTag: 003Tag: NoTag: 004File: check3.asmFunction: ExpertsTag: NoTag: 005Tag: NoTag: 006Function: Perl ExpertsTag: NoTag: 007Tag: YesTag: 008... (1 Reply)
Discussion started by: rajkrishna89
1 Replies

9. Shell Programming and Scripting

search pattern and mark/tag

Hi All, I have to search for patterns from a pattern file in a file and mark the matching lines. Input File: Student1 60 30 Student2 71 91 Student3 88 98 Pattern file: Student1 Fail Student2 Pass Student2 Pass Desired output: Student1 60 30 Fail Student2 71 91 Pass (5 Replies)
Discussion started by: saint2006
5 Replies

10. Shell Programming and Scripting

Search and filter by TAG

Hello all, searching on a text file (log file) is quite simple: grep -i texttosearch filename | grep somethingWhat I'm trying to do is filter the result by TAG and remove the double entries. Like if the log file contains the following text (fields are separated by commas): ... (18 Replies)
Discussion started by: Lord Spectre
18 Replies
Login or Register to Ask a Question