LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-28-2009, 07:20 AM   #1
alexzive
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Rep: Reputation: 0
Post help with recursive string-search in files


Hello there,

I 'd like to find out a command to search among all *.dat files in a certain path (including subdirectories) looking for the following text in them:

Code:
 --------------------------------------------------------------------------------
 Elements with small area
  
  Element  Adjusted nodes 
 --------- -------------- 
     16294       NO     
     17889       NO
and getting the list of elements with small area printed in a file "ErrorEl.txt". The output should have this form:

"/path/01/A.dat
bad-el#01
bad-el#02

/path/04/A.dat
bad-el#01
bad-el#02
bad-el#03
"

How to do it?

I know already how to find out the dat files containg a certain string

Code:
c=/path/

grep -R --include="*.dat" "Elements with small area" $c | cut -d: -f1>> ErrorEl.txt
but I don't know then how to get the element numbers(16294 and 17889 in the example above)

PS: also a bash script would be ok.
Many thanks for help. Alex

Last edited by alexzive; 10-28-2009 at 07:39 AM.
 
Old 10-28-2009, 07:31 AM   #2
alexzive
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Post second part

ah and once I get the list of "bad elements" per ".dat" file like this:

"/path/01/A.dat
13333
48631


/path/01/B.dat
20001

/path/04/A.dat
13333
"

I would like to edit the corresponding ".inp" files which look like this:

/path/01/A.inp
(Elements, node 1, node 2, node 3)

1, 1, 2, 3
2, 2, 3, 4
...
13333, 13330, 13331, 13332
48629, 48450,48449,48444
48630, 48458,48446,48448
48631, 48448,48455,48458
48643, 48467,48468,48469
48644, 48470,48469,48468
48653, 48446,48469,48476
48654, 48469,48470,48477
48655, 48478,48479,48480
48656, 48478,48480,48481

and eliminate each line whose first element correspond to the "bad" element found.

For instance, the line in orange should be deleted

Many thanks, Alex

Last edited by alexzive; 10-28-2009 at 07:35 AM.
 
Old 10-28-2009, 10:35 AM   #3
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
Part 1:

To delete everything in your dat file except the numbers and write those numbers to a new file use:

Code:
$ (echo ":g/^ *$/d"; echo ":g/[\-E]/d"; echo ":%s/\([0-9]\{5\} \).*/\1/";echo ":wq! OUTPUT_FILE") |ex -s INPUT_FILE
You can use "grep -lR PATTERN FILES" to list the file names which will become INPUT_FILE of the above command. You can write a short script or use Pipes to get the input files.

Come back if you face problem.
 
Old 10-28-2009, 10:58 AM   #4
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
Part 2:

Contents (Lines) of your "bad.dat" becomes search pattern now. Read the contents of your bad.dat files linewise and each line is now your search pattern. You can replace those lines with a 'd' (eg: g/PATTERN/d in Vi, or /PATTERN/d in SED), this will delete those line.
 
Old 10-29-2009, 04:49 AM   #5
alexzive
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
thanks for help!

I now got the BAD.dat file in the form:

16294
12333

but my knowledge in bash script is too low to do what you suggested in part2.

How to
1. Read the contents of BAD.dat linewise?
2. and send this [16294 12333] as input for a replace (->delete) in a different file "INPUT.inp"?

PS: in INPUT.inp I have to delete entire lines which begin with 16294 and 12333 respectively.

Many Thanks again!
 
Old 10-29-2009, 05:37 AM   #6
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
Quote:
Originally Posted by alexzive View Post
thanks for help!

1. Read the contents of BAD.dat linewise?
2. and send this [16294 12333] as input for a replace (->delete) in a different file "INPUT.inp"?

PS: in INPUT.inp I have to delete entire lines which begin with 16294 and 12333 respectively.
Try this:

Code:
#!/bin/bash

while read line
do
  P="$line"
  (echo "g/$P/d";echo "wq!") |ex -s INPUT.imp
done <BAD.dat
I havent tried it but it should work.
 
Old 10-29-2009, 06:07 AM   #7
alexzive
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
many thanks!

unfortunately there is a futher complication.

The INPUT.inp file looks like this:

*Node
475,0.132564,0.001137
477,0.134263,0.001069
478,0.134967,0.000611
479,0.135379,0.001142
480,0.137209,0.002212
...
120201,0.285240,0.016046
121764,0.302663,0.135871
*Element, type=DC2D3
432,477,478,479
433,480,481,482
434,483,484,485
...
180340,45645,113155,45651
180344,55174,121625,121764

*Elset, elset=C
484,490,514,515,545,547,554,555,558,760,764,765,772,774,780,789
792,794,807,810,812,813,814,816,817,819,903,904,907,922,923,925
121693,121699,121701,121703,121704,121713,121714,121718,121723,121727,121728,121732,121745,121748,12 1749,121750
121754,121755,121764,121764,121764,121764,121764,121764,12
1764,121764,121764,121764,121764,121764,121764,121764

but I have to delete lines (which corresponds to BAD.dat, i.e [16294 12333]) only within the orange selection!

PS: I need the entire INPUT.inp (not only the orange selection)

Thank you for help again!
 
Old 10-29-2009, 06:37 AM   #8
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
Quote:
Originally Posted by alexzive View Post
many thanks!

unfortunately there is a futher complication.

I have to delete lines (which corresponds to BAD.dat, i.e [16294 12333]) only within the orange selection!

PS: I need the entire INPUT.inp (not only the orange selection)

Thank you for help again!
what exactly do you want to do?
As I understood you wanted to delete lines from INPUT.imp whenever you encounter a number from your dat file.

I cannot understand your orange selection.

Do you want to search/delete in a range of lines i.e. your orange selection (and not in whole file)?

To do this you will need to have knowledge of that range. Is there a delimiter by which that range can be known? Are the line numbers fixed and known?

When you know the range you can improvise the commands to run within a range and not on whole file. You can add that range just before the 'g'.

Example:
Code:
"10,100g/$P/d"
This will remove that pattern lines from within line number 10 to 100. It will not touch the rest of the file.
 
Old 10-29-2009, 07:23 AM   #9
alexzive
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
thanks and sorry for unclear description.

the code you suggested:

Code:
"10,100g/$P/d"
is actually what I need, but instead of 10,100 my range-delimiters are to be found (referring to the text I posted) with the first orange-line "*Element, type=DC2D3" and the first non-orange-line "*Elset, elset=C".

thanks,
Alex

Last edited by alexzive; 10-29-2009 at 07:31 AM.
 
Old 10-29-2009, 07:37 AM   #10
alexzive
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
in the posted text (including the orange selection) "..." represents a large amount of lines in the same format of the previous ones, just with different numbers.
I cannot post the entire file content because it is too huge.
 
Old 10-29-2009, 09:34 AM   #11
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
Quote:
Originally Posted by alexzive View Post
thanks and sorry for unclear description.

the code you suggested:

Code:
"10,100g/$P/d"
is actually what I need, but instead of 10,100 my range-delimiters are to be found (referring to the text I posted) with the first orange-line "*Element, type=DC2D3" and the first non-orange-line "*Elset, elset=C".

thanks,
Alex
I take that your delimiters are "Element, type=DC2D3" and "Elset, elset=C". My commands are colour blind

Try:
Code:
echo "/Element, type=DC2D3"; echo "ma a"; echo "/Elset, elset=C"; echo "ma b"; echo "'a,'bg/$P/d"
Explanation:
1. first search for first occurrence of "Element..." pattern.
2. mark this line as 'a'
3. search for first occurrence of "Elset..."
4. mark this line as 'b'
5. within 'a to 'b do a g/$P/d

May be someone else can suggest a better or easier method. I think awk should handle this more cleanly, but I dont know awk.
 
Old 10-29-2009, 01:10 PM   #12
alexzive
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Hello,

almost done! but still some problem with the markers.

using:

Code:
while read line
do
  P="$line"
  (echo "/Element, type"; echo "ma a"; echo "/Elset,"; echo "ma b"; echo "'a,'b/^$P/d";echo "wq!") |ex 
-s INPUT.inp
done < BAD3.dat
~
[this time I have BAD3.dat as follows:
439
440
]


INPUT.inp (before):

*Node
439,0.132564,0.001137
440,0.134263,0.001069
478,0.134967,0.000611
479,0.135379,0.001142
480,0.137209,0.002212
481,0.136382,0.001698
482,0.137539,0.001231
483,0.136193,0.002135
484,0.135500,0.002459
485,0.135604,0.001725
486,0.134970,0.002100
487,0.141110,0.001470
488,0.141143,0.002361
489,0.139858,0.002298
490,0.143474,0.002265
491,0.142813,0.002073
492,0.143749,0.001748
493,0.141699,0.001913
494,0.141927,0.002361
495,0.142661,0.001041
496,0.142860,0.001350
497,0.142370,0.001045
498,0.142440,0.001492
*Element, type=DC2D3
432,477,478,479
433,480,481,482
434,483,484,485
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
484,490,514,515,545,547,554,555,558,760,764,765,772,774,780,789
792,794,807,810,812,813,814,816,817,819,903,904,907,922,923,925
439,440 --> put there intentionally for testing


After the code above

*Node
439,0.132564,0.001137
440,0.134263,0.001069
478,0.134967,0.000611
479,0.135379,0.001142
480,0.137209,0.002212
481,0.136382,0.001698
482,0.137539,0.001231
483,0.136193,0.002135
484,0.135500,0.002459
485,0.135604,0.001725
486,0.134970,0.002100
487,0.141110,0.001470
488,0.141143,0.002361
489,0.139858,0.002298
490,0.143474,0.002265
491,0.142813,0.002073
492,0.143749,0.001748
493,0.141699,0.001913
494,0.141927,0.002361
495,0.142661,0.001041
496,0.142860,0.001350
497,0.142370,0.001045
498,0.142440,0.001492
*Element, type=DC2D3


Thanks for help & cheers from Germany !
Alex

Last edited by alexzive; 10-29-2009 at 02:15 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to search all files for string - strings command? jmur Programming 4 03-28-2008 02:35 PM
Search and replace string in executable files aalex77 Programming 2 05-26-2006 06:32 PM
Is it possible to search for a string from all the files on the harddisk? Akhran Linux - Newbie 8 09-13-2005 06:09 AM
how do i search files for a particular string? darkpark Linux - Newbie 4 07-05-2005 05:46 PM
recursive search of a string in a directory ?! realos Linux - Software 4 11-27-2002 04:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration