Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk gsub between the first two regular expressions in multiple dirs. 1

Status
Not open for further replies.

FedoEx

Technical User
Oct 7, 2008
49
US
Awk gsub between the first two regular expressions in multiple dirs.
Hello.
I create 10 directories at once with
Code:
$for (( i=0; i<10; i++)) ;do mkdir $i; done
In all of these 10 directories I copy the same file [tt]parsec.in[/tt].
I know how to print between the two regexp
Code:
 $awk ' /begin Atom/,/end Atom/ ' parsec.in   
begin Atom_Coord
 1.64545 1.64545 0.0
-1.78401 1.78401 0.0
 end Atom_Coord
 begin Atom_Coord
0.0             0.0            0.0
 end Atom_Coord
Here is what I need.
I keep the zeros between the second set of [tt]begin Atom end Atom[/tt].
For directory 1,2,3....9 I need to replace the numbers between the first set of [tt]begin Atom end Atom[/tt] with let say for directory [tt]1[/tt]
Code:
   sin(1)  sin(1)
 -cos(1) cos(1)
for directory [tt]2[/tt]
Code:
   sin(2)  sin(2)
 -cos(2) cos(2)

and so fort.
I can generate the sine/cos numbers above with
Code:
   awk 'BEGIN { for(i=1;i<=10;i++) printf("%2.8f %1s %2.8f \n %2.8f %1s %2.8f  \n \n",  sin(i),"",sin(i), -cos(i),"", cos(i) )}'
And then manually copy the pairs to the respective files.
The rest of the [tt]parsec.in[/tt] file must not change.
So is there a way to to that in awk.
Any help is greatly appreciated.
Thanks
 
I have a solution using different approach .
I can split the parsec.in file into two files let say
part1.in which is the part of the file from the beginning of the file to the first regex “begin Atom”
part2.in which is the part of the file from the second regex “end Atom” to the end of the file.
Then I just loop over all indexes.
So something like

Code:
 for((i=0;i<10;i+=1)); do cat part1.in>$i/parsec.in; awk -v vv="$i" 'BEGIN{print sin(vv),””, cos(vv)}'>>$i/parsec.in;cat part2.in >>$i/parsec.in; done

The code above is exatcly what I need.
However I would like to know a way to do that entirely in awk.
Thanks.
 
Typed, untested:
Code:
awk '
NR==FNR{p1=p1""$0"\n";next}
{p2=p2""$0"\n"}
END{for(i=0;i<10;++i)
printf "%s%2.8f   %2.8f\n%2.8f  %2.8f\n(%d)\n%s",p1,sin(i),sin(i),-cos(i),cos(i),i,p2>(i"/parsec.in")
}' part1.in part2.in

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you.It works great.
That is exactly what I need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top