I advanced a lot in awk mainly thanks to this forum.
I am posting here my latest creation and the way I arrived to it. I am asking you gurus for alternative way to do this or for possible improvements, suggestions ....
Here is the example infile
Simple code to extract single line containing the #S regex
If I want to see the header line for block 5
So for arbitrarily number of input arguments my code evolves to.
My final goal is actually to be able to print not the header lines but the entire number part of a given block.
That is why I started the thread Another way to print between two regex and here is the code
So if I now run
However some times I need to extract range of blocks so I have the user input as
And here is the entire code that takes into account the range option.
Thank you feherke, PHV, Annihilannic, LKBrwnDBA and the rest of you for the help.
I am posting here my latest creation and the way I arrived to it. I am asking you gurus for alternative way to do this or for possible improvements, suggestions ....
Here is the example infile
Code:
$cat infile
#S 1 line one contents
#AAAA
11 13 100
12 14 100
13 23 111
end
#S 2 line two contents
# a n c
21 11 222
22 24 222
23 45 222
end
blah blah
#S 3 line three contents
x y z
31 42 333
32 26 333
33 56 333
end
#S 4 line four contents
more text
41 15 444
42 24 444
43 12 444
end
a z characters between the blocks
#S 5 line five contents
# some comments here
51 32 555
52 34 555
53 12 555
end block five
i j k
#S 6 line six contents
61 34 666
62 23 666
63 56 666
end block six
and so fourth another thousand of these
Simple code to extract single line containing the #S regex
Code:
$cat ARG.awk
BEGIN{
ARGC--
}
$0~("#S " ARGV[2] " "){ print $0}
Code:
$awk -f ARG.awk infile 5
#S 5 line five contents
Code:
BEGIN {
ARGCin=ARGC
while (ARGC >2) ARGC--
}
{
for (i = 2; i < ARGCin; i++) if($0~("#S " ARGV[i] " ")){print $0}
}
My final goal is actually to be able to print not the header lines but the entire number part of a given block.
That is why I started the thread Another way to print between two regex and here is the code
Code:
BEGIN {
ARGCin=ARGC
while (ARGC >2) ARGC--
}
function formprint()
{if($0!~/[f-zA-Z]|#|^$/) print $1," ",$2," ",$3}
{
for (i = 2; i < ARGCin; i++)
if ($0 ~ ("#S " ARGV[i] " ") ) { s=1; next; } if ($1 ~ /end/) s=0; if (s) formprint()
}
Code:
awk -f ARG.awk infile 2 3 5
21 11 222
22 24 222
23 45 222
31 42 333
32 26 333
33 56 333
51 32 555
52 34 555
53 12 555
However some times I need to extract range of blocks so I have the user input as
Code:
awk -f ARG.awk infile 93 - 104
And here is the entire code that takes into account the range option.
Code:
#!/bin/awk -f
BEGIN {
# print "in "ARGC
ARGCin=ARGC
while (ARGC >2) ARGC--
}
function formprint()
{if($0!~/[a-z]|#|^$/) printf("%1.5f %1.8f %1.5e \n",$1,$2,$3)}
ARGCin>2&&ARGV[3]~/-/{
if ($0 ~ ("#S " ARGV[2] " ") ) { s=1; next; } if ($0 ~ ("#S " ARGV[4] " ") ) s=0; if (s) formprint()
if ($0 ~ ("#S " ARGV[4] " ") ) { i=1; next; } if ($1 ~ /end/) i=0; if (i) formprint()
}
ARGV[3]!~/-/ {
for (i = 2; i < ARGCin; i++)
if ($0 ~ ("#S " ARGV[i] " ") ) { s=1; next; } if ($1 ~ /end/) s=0; if (s) formprint()
}