Search for a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a pattern
# 8  
Old 02-22-2008
Sorry, I forgot to say that the script have to find the pattern not only in the main directory but also in the subdirectories
# 9  
Old 02-22-2008
Tools Get this to work for you within one directory

Work with the sample code above for one directory. Once that is done, all you need to do is make a bigger 'loop' around the existing code. This would look thru the sub-directories.

So, make sure this is doing what you want within one directory first - before making the process more complicated!
# 10  
Old 02-22-2008
Question

Quote:
Originally Posted by joeyg
I am not sure if I have the sed command correct, but I think you get the idea. I * the changes. Because the file will now only have one keyword on a line, the wc -l command would give a truer count.

Good luck! Smilie


#! /bin/bash
pattern="good"
rm sctfile.raw 2>/dev/null
for zf in *.dat; do

* sed 's/$pattern/$pattern \n/g' $zf >tempf
# numpat=$(cat $zf | grep "$pattern" | wc -l)

* numpat=$(cat tempf | grep "$pattern" | wc -l)


echo $numpat","$zf >>sctfile.raw
done
sort -nr sctfile.raw >sctfile.srt
Hi,

I don't understand why the NEW code you posted does't work:
Code:
* sed 's/$pattern_da_cercare/$pattern_da_cercare \n/g' $zf >tempf
# numpat=$(cat $zf | grep "$pattern_da_cercare" | wc -l)
* numpat=$(cat tempf | grep "$pattern_da_cercare" | wc -l)

The shell return these error messages:
./conta_occorrenze: line 158: Desktop: command not found
./conta_occorrenze: line 160: Desktop: command not found
# 11  
Old 02-22-2008
Tools Eliminate the *

I inserted the * characters to help highlight two lines of code. Delete that character from your script and try again.

Code:
sed 's/$pattern_da_cercare/$pattern_da_cercare \n/g' $zf >tempf
# numpat=$(cat $zf | grep "$pattern_da_cercare" | wc -l)
numpat=$(cat tempf | grep "$pattern_da_cercare" | wc -l)

You should be able to after script run look at the tempf and see only one pattern on each line. If not, then something is wrong with the sed command and the \n. I ahve seen some quirky behavior from the insert of \n or newline character on some systems and shells.
# 12  
Old 02-22-2008
This should be faster and you can search for files that contain a pattern for a number of times between MIN and MAX:

Code:
#!/bin/sh

MIN=2
MAX=7
PATTERN="XXX"

for file in *.dat
do
  awk -v min=$MIN -v max=$MAX -v patt=$PATTERN '
  BEGIN {RS=" |\n"}
  $0 == patt {n++}
  END {
  if(n >= min && n <= max) {print FILENAME " : " n}
  }
  ' "$file"
done

Use nawk or /usr/xpg4/bin/awk on Solaris

Regards
# 13  
Old 02-22-2008
The two scripts doesn't work... Smilie Smilie

I thought that so I had to install nawk, so I installed it but it's the same story, the shell give me an error.

Franklin52, the error for this part of code is:
Code:
MIN=2
MAX=7
for file in *.txt
do
  awk -v min=$MIN -v max=$MAX -v patt=$pattern_da_cercare '
  BEGIN {RS=" |\n"}
  $0 == patt {n++}
  END {
  if(n >= min && n <= max) {print FILENAME " : " n}
  }
  ' "$file"


exit 0
done

The error codes are:
./conta_occorrenze: line 157: syntax error near unexpected token `exit'
./conta_occorrenze: line 157: `exit 0'

The strange fact is the the line 157 is that one in bold, but the line with "exit" is 167

@ joeyg
I installed nawk on my MAC OS X but your code doen't work.
So I tryed on UBUNTU and it doesn't work.
Why do you thing it happen?
# 14  
Old 02-22-2008
Place the exit command after the loop not within it.

Code:
MIN=2
MAX=7
for file in *.txt
do
  awk -v min=$MIN -v max=$MAX -v patt=$pattern_da_cercare '
  BEGIN {RS=" |\n"}
  $0 == patt {n++}
  END {
  if(n >= min && n <= max) {print FILENAME " : " n}
  }
  ' "$file"
done

exit 0

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

7. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

8. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question