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!

Stuck on script 2

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I have several files named block_obj_#. Example block_obj_1, block_obj_2, ect.
I will never know how many files I might have. I need to grep on RXOTX and RXORX
(exactly) and send the results from each file to a new file called rxmop_input# using
the same number as in the original file. That is the most important part.

Meaning:

block_obj_1 contents

RXOTS-211-10-0
RXOTS-211-10-1
RXOTS-211-10-2
RXOTS-211-10-3
RXOTS-211-10-4
RXOTS-211-10-5
RXOTS-211-10-6
RXOTS-211-10-7
RXORX-211-0
RXORX-211-1
RXORX-211-2
RXORX-211-4
RXORX-211-5
RXORX-211-6
RXORX-211-8
RXORX-211-10
RXOTX-211-0
RXOTX-211-1
RXOTX-211-2
RXOTX-211-4
RXOTX-211-5
RXOTX-211-6
RXOTX-211-8
RXOTX-211-10
RXOTRX-211-0
RXOTRX-211-1

rxmop_input_1 contents after grep

RXORX-211-0
RXORX-211-1
RXORX-211-2
RXORX-211-4
RXORX-211-5
RXORX-211-6
RXORX-211-8
RXORX-211-10
RXOTX-211-0
RXOTX-211-1
RXOTX-211-2
RXOTX-211-4
RXOTX-211-5
RXOTX-211-6
RXOTX-211-8
RXOTX-211-10

And so on, for as many files as there are. Thanks!
 
nawk -f thingy.awk block_obj*

#---------- thingy.awk
BEGIN {
outputFileROOT="rxmop_input"
unknownNUM="999999";
}

FNR == 1 {
if (match(FILENAME, "[0-9]"))
output=outputFileROOT substr(FILENAME, RSTART, RLENGTH);
else
output=outputFileROOT unknownNUM;
}

/(^RXOTX|^RXORX)/ { print >> output; }
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
for xx in $PWD/block_obj_*
do
num=`echo $xx | sed 's/[^0-9]//g'`
awk ' {
if ($0 ~ /RXOTX/ || $0 ~ /RXORX/) {
print $0
}
}' $xx > $PWD/rxmop_input$num
done
 
Awesome vlad, the thingy.awk worked perfectly as usual!

Thanks,
Beaster
 
marsd, yours worked fine as well thanks!
 
but, no need to use the slow awk:

for xx in $PWD/block_obj_*
do
num=`echo $xx | sed 's/[^0-9]//g'`
echo $xx | sed -ne '/TXO[TR]X/p' >$PWD/rxmop_input$num
done ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
sure, should be:


sed -ne '/TXO[TR]X/p' $xx >$PWD/rxmop_input$num ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Jamisar.
Prove it. Prove to me that the sed version makes a measurable difference and you are not just nitpicking.
Please post the benchmarks.
Here are mine:

!/bin/sh

function do_with_awk() {
for xx in $PWD/block_obj_*
do
num=`echo $xx | sed 's/[^0-9]//g'`
awk ' {
if ($0 ~ /RXOTX/ || $0 ~ /RXORX/) {
print $0
}
}' $xx > $PWD/rxmop_input$num
done 2> /dev/null
}

function do_with_sed() {
for xx in $PWD/block_obj_*
do
num=`echo $xx | sed 's/[^0-9]//g'`
echo $xx | sed -ne '/TXO[TR]X/p' >$PWD/rxmop_input$num
done 2> /dev/null
}

#do_with_awk 2>/dev/null
do_with_sed 2>/dev/null

Alternation results in (on average):
Sed version:
##############################
>stimer.exp sh scrap.sh
Current=1400644:-:Former=1400606.

Programs elapsed time was: 38
End sed version
###############################
awk version
###############################
>stimer.exp sh scrap.sh
Current=1497767:-:Former=1497728.

Programs elapsed time was: 39
End awk version
###############################
 
a better [more granular] 'stopwatch' would be 'time' - 'man time'.

marsd,
use '/(^RXOTX|^RXORX)/' construct with no 'action'. The 'missing' action is implicitly &quot;print $0&quot;. And there's only one operation when matching a desired pattern [and not two 'matches' ORed together].

vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks for the tips vlad.
For clarity and simplicity I believe the
syntactical sugar of $0 and a full if is
easier to understand.
For speed you are doubtless correct.
My point is that in a simple operation of this type I think jamisars comments are
nitpicky...and this IS an awk forum after all ;)
 

#!/bin/sh
#hi all, don't worry, this is my last visit.
#marsd: you are still running awk

trap &quot;rm -f [ax]file&quot; 0
man xterm >afile
cat afile afile afile afile afile afile afile >xfile
cat xfile xfile xfile xfile xfile xfile xfile >afile
wc afile
# 103488 441980 3850518 afile

/bin/time -p sed -ne '/xterm/p' afile >/dev/null
/bin/time -p nawk '/xterm/' afile >/dev/null

#real 0.39
#user 0.25
#sys 0.08

#real 0.67
#user 0.61
#sys 0.03

#echo &quot;scale=2;3900/67&quot;|bc
#58.20
#the nitpicking result: sed uses 58.2 % the time awk needs.

#uname -a
#SunOS op1d345swk 5.6 Generic_105181-21 sun4u sparc SUNW,Ultra-5_10
#psrinfo -v
#Status of processor 0 as of: 09/12/02 13:08:34
# Processor has been on-line since 09/03/02 10:31:40.
# The sparc processor operates at 333 MHz,
# and has a sparc floating point processor.
------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Gawk 3.1.0
Please don't tell me which utility I am using.
My test was performed on an amd tbird 700mhz
processor with 256 mb RAM, linux kernel 2.4.14.

I suggest your results are partly an artifact
of your environment and nawk.
 
... I'd bet I could do it in assembler and it'd be faster! ;)

This hourse has been beaten to death long time ago - RIP!

The way I look it is the smaller 'footprint' of your favorite is the better off you are [in most cases]. The number of hoops you'd have to jump through to solve the problem is a matter of personal perseverance and determination level.

I think it would be much more interesting to compare performance metrics of programs implemented with the same set of tools, rather then comparing apples and oranges.

I hope jamisar didn't take everythig said on this thread [and in this forum] too personally and would come back when (s)he sees it fit.
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
jamisar is still there, as 'visitor'
he uses standard /bin/[sed|nawk] shipped with solaris.
the main title of this forum is : 'tecnical forum for computer professionals', and he expects flexible-professional-people.

i do not believe it's professional to write xx[x]*-lines code
in what-ever-you-want-tool to solve a job when an other
tool will (faster) make in an one-line.

thats all.

choice the tool you will use according to the job you have to do. don't try 'xxxx' only why you just known 'xxxx'
awk is a FANTASTIC-tool, he learned me 'C'
i always make a choice of the tool to use for the job i have to solve!
(NOTA: i am not able to print a backquote on this 'scheiss' pc)

the stupid code:
aaa='cat sssss|grep aaa|wc -l|awk '{print $1}''
bbb='cat sssss|grep bbb|wc -l|awk '{print $1}''
works !! this shows your poor unix-knowlegde !!!
aaa='grep -c aaa sssss'
bbb='grep -c bbb sssss'
make the same! but i read: you undestand grep, also unix.

if (and this is reality) i find x >100 times souch statement in
a sh-script.. i just think: the guy who wrote it, has no ideas about unix-shell-scriping.
he is just limitet to his 'xxxx' cmd.
and spends a lot, lot, lot of time doing his 'cat', suppose sssss is a big file !!!
he uses the wrong tool to do the job.
'perl' or 'c' will do it in ONE loop.

NOTA: (IRONIE: on my sunbox) 'grep -c' IS SLOWER :(

my message: don't limit yourself.
in unix, you can do 'the job' in several differents ways
look for the rigth one!!!
if you have not to aritmetically compute somethig, the (FANTASTIC)AWK is offten not the tool you shoud use.



------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
I am not quite sure what with your tortured english you are trying to impart here.

My guess is:

a) That you believe I or other persons here are not &quot;professional&quot; in that we don't use invective and personal
attacks as regularly as you do.

b) That you have a GIGANTIC chip on your shoulder.

c) All of your other commentary and guesses about
specific knowledge of this or that are symptomatic of your particular prejudices and bruised ego.







 
jamisar,

You're preaching to the choir!

If I drive a stick and you drive an automatic - it's a matter of a personal choice. It says nothing [well, almost nothing - I might be a bit biased here ;) ] about your driving skills.

The beauty of it all is &quot;there's no limit to human perfection&quot;.

Beaster,
do you what you have started? ;)
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top