Help with A simple awk script. 
Author Message
 Help with A simple awk script.

Hello,

I have an ascii file that looks something this:

Garbage~~~~~~~~~~~~~~~~~~~~
Garbage~~~~~~~~~~~~~~~~~~~~~
.
.
.
mystring=(string2(string3('m1:n1:K1', 'n23', 'm2:n2:K2',
...............,'mi:ni:ki',..........................
..........................mn:nn:kn)))
moregarabge~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
moregarbage~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.
.
Now, I want to extract the numbers within the inside parenthesis and
print them into a file as :

 m1:n1:k1
 n23
 m2:n2:k2
 m3:n3:k3
 .
 .
 mi:ni:ki
 .
 nij
 .
 mn:nn:kn

Now, I have idea of how to do this in bits and pieces but i don't know
the exact syntax for the script. Of course, I can search for mystring
and then
print the first field after the string3 and keep printing till I find
the first
')' But I don't know enough awk syntax to do this.

Help Please!
azac.



Fri, 13 Aug 2004 11:32:18 GMT  
 Help with A simple awk script.

Quote:

>Hello,

>I have an ascii file that looks something this:

>Garbage~~~~~~~~~~~~~~~~~~~~
>Garbage~~~~~~~~~~~~~~~~~~~~~
>.
>.
>.
>mystring=(string2(string3('m1:n1:K1', 'n23', 'm2:n2:K2',
>...............,'mi:ni:ki',..........................
>..........................mn:nn:kn)))
>moregarabge~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>moregarbage~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>.
>.
>Now, I want to extract the numbers within the inside parenthesis and
>print them into a file as :

> m1:n1:k1
> n23
> m2:n2:k2
> m3:n3:k3
> .
> .
> mi:ni:ki
> .
> nij
> .
> mn:nn:kn

>Now, I have idea of how to do this in bits and pieces but i don't know
>the exact syntax for the script. Of course, I can search for mystring
>and then
>print the first field after the string3 and keep printing till I find
>the first
>')' But I don't know enough awk syntax to do this.

>Help Please!
>azac.

Start with:

awk '/mystring/ {print}' infile

then modify that to change characters to spaces:

awk '/mystring/ {gsub(/[',()/," "), print}' infile

Then print the fields you wanted:

awk '/mystring/ {gsub(/[',()/," ")
                 $1=$1
                 for(i=4;i<=NF;i++) {print $i}}' infile

That should do the job, but I didn't test it.

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 13 Aug 2004 15:50:40 GMT  
 Help with A simple awk script.

Quote:


> >Hello,

> >I have an ascii file that looks something this:

> >Garbage~~~~~~~~~~~~~~~~~~~~
> >Garbage~~~~~~~~~~~~~~~~~~~~~
> >.
> >.
> >.
> >mystring=(string2(string3('m1:n1:K1', 'n23', 'm2:n2:K2',
> >...............,'mi:ni:ki',..........................
> >..........................mn:nn:kn)))
> >moregarabge~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >moregarbage~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >.
> >.
> >Now, I want to extract the numbers within the inside parenthesis and
> >print them into a file as :

> > m1:n1:k1
> > n23
> > m2:n2:k2
> > m3:n3:k3
> > .
> > .
> > mi:ni:ki
> > .
> > nij
> > .
> > mn:nn:kn

> >Now, I have idea of how to do this in bits and pieces but i don't know
> >the exact syntax for the script. Of course, I can search for mystring
> >and then
> >print the first field after the string3 and keep printing till I find
> >the first
> >')' But I don't know enough awk syntax to do this.

> >Help Please!
> >azac.

> Start with:

> awk '/mystring/ {print}' infile

> then modify that to change characters to spaces:

> awk '/mystring/ {gsub(/[',()/," "), print}' infile

> Then print the fields you wanted:

> awk '/mystring/ {gsub(/[',()/," ")
>             $1=$1
>             for(i=4;i<=NF;i++) {print $i}}' infile

> That should do the job, but I didn't test it.

> Chuck Demas

Chuck,
Thank you. Actually your code prints from 4th field onward for every
line-which i do not need.I actually was able to do two separate
scripts to get to do what I need to do:
The first one script1 is as follows:

  flag==1 &&/^session/{exit}
  flag==1{print}
  /^leaf/{for(i=4;i<=NF;i++){print$i};flag=1}

where I isolate the part of the input file between strings 'leaf' and
'session'.
Then I use your code as script2 as follows:

{gsub(/[',()]/," ")
         $1=$1
         for(i=1;i<=NF;i++)
            {print$i}i=1}

But can you tell me how I could put these two scripts together so that
i do not have to do awk -f script1 infile|awk -f script2 ?

Regards
Azac



Fri, 13 Aug 2004 23:59:40 GMT  
 Help with A simple awk script.

<snip>

Quote:

> Chuck,
> Thank you. Actually your code prints from 4th field onward for every
> line-which i do not need.I actually was able to do two separate
> scripts to get to do what I need to do:
> The first one script1 is as follows:

>   flag==1 &&/^session/{exit}
>   flag==1{print}
>   /^leaf/{for(i=4;i<=NF;i++){print$i};flag=1}

> where I isolate the part of the input file between strings 'leaf' and
> 'session'.
> Then I use your code as script2 as follows:

> {gsub(/[',()]/," ")
>          $1=$1
>          for(i=1;i<=NF;i++)
>             {print$i}i=1}

> But can you tell me how I could put these two scripts together so that
> i do not have to do awk -f script1 infile|awk -f script2 ?

> Regards
> Azac

Does this do what you want?

# alevuru.awk
/mystring/,/\)\)\)/ {
    sub(/.*\(/,"")
    sub(/\)\)\)/,"")
    a[i++]=$0

Quote:
}

END {
    for(j=0;j<i;j++){
        gsub(/,/,"\n",a[j])
        gsub(/'/,"",a[j])
        print a[j]
    }

Quote:
}

HTH
--
Peter S Tillier
"Who needs perl when you can write dc in sed?"
peter dot tillier at btinternet dot com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.


Sat, 14 Aug 2004 05:00:21 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Need help to write a simple awk script

2. Need help with simple awk script

3. A simple question regarding shell scripts and awk

4. Problem with simple AWK script.

5. Simple awk command within a perl script

6. Simple awk script does not work?

7. 'Simple Awk Script ?'

8. Very simple awk for a simple user

9. awk:Simpler question: But critical ; please help

10. Simple help with awk

11. Help optimising AWK script

12. Need help with awk script

 

 
Powered by phpBB® Forum Software