openSUSE Forums > Programming/Scripting » help with recursive searching+editing

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
Page 2 of 2 1 2
 
LinkBack Thread Tools Display Modes
  #11 (permalink)  
Old 30-Oct-2009, 06:11
Puzzled Penguin
 
Join Date: Aug 2008
Posts: 19
alexzive hasn't been rated much yet
Default Re: help with recursive searching+editing

Many Thanks FM!

I am almost done with the following code (thanks also to another no-awk guy here

Code:
#!/bin/bash

c=/path/
cd $c

#crop bad elements between "small area" and "P R O B L E M"
awk 'BEGIN{RS="--"}/'"P R O B L E M   S I Z E"'/{for(i=1;i<=NF;i++)if($i == "NO"){n=i-1;print $n}}' DAT.dat > BAD.dat


while read line
do
  P="$line"
  echo $P
  (echo "/Element, type"; echo ":/[0-9]"; echo "/$P/d";echo "wq!")|ex -s INPUT.inp

done < BAD.dat

There is only one problem left, when the BAD elements appear as second third or fourth item in the line--> in this case the line should not be deleted:

Code:
*Element, type=DC2D3
432,477,478,479
433,439,481,482 --> line NOT to be deleted
434,483,484,440 --> line NOT to be deleted
435,486,485,484
436,487,488,489
437,490,491,492
438,493,488,487
439,494,488,493 --> line to be deleted
440,495,496,497 --> line to be deleted
441,491,498,496
442,496,498,497
443,493,499,494
444,500,487,501
445,500,493,487
446,493,500,499
447,499,500,498
448,502,503,504
449,505,506,507
450,508,507,509
451,506,509,507
452,510,511,512
453,510,506,505
454,510,513,506
455,514,505,515
*Elset, elset=C
Thanks!
Reply With Quote
  #12 (permalink)  
Old 30-Oct-2009, 08:50
FeatherMonkey's Avatar
Wise Penguin
 
Join Date: Mar 2008
Posts: 1,550
FeatherMonkey has a spectacular reputation aura aboutFeatherMonkey has a spectacular reputation aura aboutFeatherMonkey has a spectacular reputation aura aboutFeatherMonkey has a spectacular reputation aura about
Default Re: help with recursive searching+editing

As I'm enjoying the learning curve ...

You really are missing the beauty of awk (Bloody terrible syntax though)
There is no need for a while loop
Code:
awk 'BEGIN{FS=","}NR==FNR{a[$1];next} $1 in a{print $0}' check.txt dat1.txt
OK the above still doesn't do what you want but does show that there is no need to loop. The problem with the above is it will still bring back the early and later ones.

Now I just cant get my head around at combining the expressions hopefully one of the gurus will fix it. Even though I did manage to achieve what you first wanted without the 0 padding though
**(note)
Code:
for i in $(ls -l | egrep -v "^d|^l" | awk '{print $9}');
do awk 'BEGIN{str=01;RS="--"}/'"P R O B L E M   S I Z E"'/{{print "*Element"}
for(i=1;i<=NF;i++)
if($i == "NO"){n=i-1;print $n}
else if ($i=="YES"){n=i-1;print $n}{print "*Elset"}}' $i;
done
This is actually missing the count and the #, to get that.. the var is there and just needs printing it also doesn't presume just NO but also takes into account YES. If you change print $n to $n'#'str;str++ you'll have the format you wanted(But then it won't match for the delete), if you change "*Element" to FILENAME(Or even prior should work inside{}) it'll append the filename to the top of the block but... this uses *EL as the record sets.

As mentioned I've had to crudely hack it with pipes hopefully someone will come along and tidy it up and I'll learn something new.

Code:
awk 'BEGIN{FS=","}NR==FNR{a[$1];next} $1 in a{print $0}' check.txt dat1.txt | awk 'BEGIN{RS="\*El";FS="\n"}/'"type\="'/{print $0}' | awk '/^([[:digit:]])/{print $0}'
Now personally I would then pipe this to sed -i as this'll update them but carefully test and test again(Did I say test) using test data there will be no undo(OK maybe you'll have a backup looking at the man).

**
The for loop won't actually do as intended as you won't have paths if you use the recursive flag, it looks like this would be one for find. So the for loop is rather superfluous and could be written as for i in *.*
__________________
Man first, have a try at Info, have a look at Wiki, if all that fails Scroogle!!!!!
If I've helped click on the Rep button I don't know what it does but it sounds cool.
Reply With Quote
  #13 (permalink)  
Old 30-Oct-2009, 09:17
FeatherMonkey's Avatar
Wise Penguin
 
Join Date: Mar 2008
Posts: 1,550
FeatherMonkey has a spectacular reputation aura aboutFeatherMonkey has a spectacular reputation aura aboutFeatherMonkey has a spectacular reputation aura aboutFeatherMonkey has a spectacular reputation aura about
Default Re: help with recursive searching+editing

edit
If you change the print to..
Code:
{print "*Element" "\n" FILENAME}
You'll have the filename in each block as to how you'll use it I leave that one upto you.
__________________
Man first, have a try at Info, have a look at Wiki, if all that fails Scroogle!!!!!
If I've helped click on the Rep button I don't know what it does but it sounds cool.
Reply With Quote
  #14 (permalink)  
Old 04-Nov-2009, 06:51
Puzzled Penguin
 
Join Date: Aug 2008
Posts: 19
alexzive hasn't been rated much yet
Smile Re: help with recursive searching+editing

Hello,
thanks for suggestion but I still prefer the echo "/string" style with loop. I solved my problem above just adding ^ before $P.
This code does exactly what I wanted but for just a single file:

Code:
#!/bin/bash

c=/path/
cd $c

#crop bad elements between "small area" and "P R O B L E M"
awk 'BEGIN{RS="--"}/'"P R O B L E M   S I Z E"'/{for(i=1;i<=NF;i++)if($i == "NO"){n=i-1;print $n}}' DAT.dat > BAD.dat


while read line
do
  P="$line"
  echo $P
  (echo "/Element, type"; echo ":/[0-9]"; echo "/^$P/d";echo "wq!")|ex -s INPUT.inp

done < BAD.dat

making this code recursive on several files is the next challenge now (part II)
Thanks!
Reply With Quote
  #15 (permalink)  
Old 04-Nov-2009, 13:20
Explorer Penguin
 
Join Date: Sep 2008
Posts: 161
dmera hasn't been rated much yet
Default Re: help with recursive searching+editing

Sorry that I couldn't help earlier but sometimes I have to work at work.
Anyway is not what I would have like the script to be like (I was thinking of a awk complete solution, but time was not on my side.
Just to help get over with the last step here is the script you have and I added just a loop to do multiple files(assuming they are in the same directory.
#!/bin/bash

#c=/path/
#cd $c

#crop bad elements between "small area" and "P R O B L E M"
awk 'BEGIN{RS="--"}/'"P R O B L E M S I Z E"'/{for(i=1;i<=NF;i++)if($i == "NO"){n=i-1;print $n}}' A.dat > BAD.dat
all_files="/home/dan/progr_task/*.inp"
for the_file in $all_files; do
echo "$the_file is my file"
while read line
do
P="$line"
# echo $P
(echo "/Element, type"; echo ":/[0-9]"; echo "/^$P/d";echo "wq!")|ex -s $the_file

done < BAD.dat
done
Reply With Quote
Reply
Page 2 of 2 1 2

Bookmarks


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2