search within the file in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search within the file in script
# 1  
Old 05-17-2010
search within the file in script

Hi all,

I have to search a particular folder which conatins around 2000 XML files.
I need to identify those files which have a value GSM under Product Value

Code:
 
<item name="Product Value">
<value>GSM</value>
</item>

I need to copy those file names to another file.

This is my script. When I print $line, it is showing the entire line. But the grep statememt is taking it word by word. Also,the variables FOUNIT and FOUNDENT are not being set correctly.can somebody help me out?

Code:
 
#!/bin/ksh
cd /files/gsm
logfile=/users/gsm.txt
found=0
for i in `ls`
do
 
while read line
do
 
if [[ $found -eq 1 ]] ; then
 
found=0
FOUNDENT=`grep "GSM" ${line}`
if [ -z ${FOUNDENT} ]; then
echo $i |tee -a $logile
break
fi
fi
FOUNDIT=`grep "Product Value" ${line}`
if [ ! -z ${FOUNDIT} ]; then
found=0
else
found=1
fi
 
done< $i
done

Moderator's Comments:
Mod Comment Use code tags please, ty. Indention would be nice too.

Last edited by zaxxon; 05-17-2010 at 07:48 AM..
# 2  
Old 05-17-2010
Another approach:
Code:
for f in *; do tr -d '\n' < $f| grep -l '<item name="Product Value"><value>GSM</value></item>' && echo $f; done

It takes any file in the folder and removes the newlines for every file being processed. Then it greps for the string you are looking for which would be else on 3 different lines. If grep is successful, it prints the name $f of the file. At this point you could place your copy command.
The for loop will only work if no file has a blank/spaces in it. Else a while/read loop should be used for example.
# 3  
Old 05-17-2010
Thanks zaxxon.

I tried to execute the script you have given. But it did not echo any file where there are so many files which match this condition.Can you suggest any remedies here?
# 4  
Old 05-17-2010
I tried it with some example files using exactly your pattern and it worked; maybe there is a small typo or something?

Code:
$> cat file1
aowidaw
awd
yyppp
yyppp
yyppp
$>
$> cat file2
aoipwjda
aoipwjda
aoipwjda
<item name="Product Value">
<value>GSM</value>
</item>
aoipwjda
$>
$> cat file3
inow23nr23
inow23nr23
inow23nr23
inow23nr23
inow23nr23
$>
$> for f in *; do tr -d '\n' < $f| grep -l '<item name="Product Value"><value>GSM</value></item>' && echo $f; done
file2

# 5  
Old 05-17-2010
Tried again after making sure there are no typos. It did not return anything.

Not sure why.

- Also, I need to fetch all filenames which have a value contains GSM (like GSM management, GSM networks etc) within the value column inside Product value. How do we modify ?
# 6  
Old 05-17-2010
If the lines with <item name> and <value> are consecutive you can try something like this:
Code:
gawk -F"[<>]" '/Product Value/{getline;if(match($3,"GSM"))print FILENAME;nextfile}' *.xml

Remove the coloured part if you don't use gawk.
# 7  
Old 05-17-2010
Gives error. The file names have no extensions here.

$ gawk -F"[<>]" '/Product Value/{getline;if(match($3,"GSM"))print FILENAME;nextfile}' *


gawk: not found

$ awk -F"[<>]" '/Product Value/{getline;if(match($3,"GSM"))print FILENAME;nextfile}' *

awk: syntax error near line 1
awk: illegal statement near line 1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

2. Shell Programming and Scripting

Exclude file from a search script

Hello, I use the following script to delete files from folders. There is an old file I wish to exclude the deletion script. How to exclude a file by name? Current script: ============================= #!/bin/ksh # ---------------------------------------------------- #... (1 Reply)
Discussion started by: pointer
1 Replies

3. Shell Programming and Scripting

Script to search a large file with a list of terms in another file

Hi- I am trying to search a large file with a number of different search terms that are listed one per line in 3 different files. Most importantly I need to be able to do a case insensitive search. I have tried just using egrep -f but it doesn't seam to be able to handle the -i option when... (3 Replies)
Discussion started by: dougzilla
3 Replies

4. Shell Programming and Scripting

To search a file for a specific word in a file using shell script

Hi All, I have a sql output file has below. I want to get the values 200000040 and 1055.49 .Can anyone help me to write a shell script to get this. ACCOUNT_NO ------------------------------------------------------------ BILL_NO ... (8 Replies)
Discussion started by: girish.raos
8 Replies

5. Shell Programming and Scripting

Script which will search for a file for 15 mins

Hi All, I would like to write a script which will search a file say abc.dat in /a/b/data for 15 mins only. If the script finds the file in 15 mins then it will exit will exit sucessfully and if there is no file for 15 mins it will exit and copy the last day file (abc.dat_ddmmyyhhmmss) from... (1 Reply)
Discussion started by: chandancsc
1 Replies

6. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

7. Shell Programming and Scripting

script to search a file

hi all i have a requirement to write a script which will serach a file in a particuar location for 3 hours from the time when the script is run.if during this period the file arrives in a particular location then this will transmit the file to a other server.please help me writing the script which... (1 Reply)
Discussion started by: dr46014
1 Replies

8. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies

9. Shell Programming and Scripting

File search for pattern - script

Hi All, I have two directories as 1) mi/job -> job1.sh, job2.sh, job3.sh 2) mi/sysin -> sysin1, sysin2, sysin3,sysin4 I want to wrrite a script such that it should accept two parameters as directory paths. $ myscript mi/sysin mi/job. The script should be able to scan all files... (3 Replies)
Discussion started by: rahulrathod
3 Replies

10. Shell Programming and Scripting

Search File Script

Nice forum for noobs like me to UNIX. With that said, I need assistance. I am trying to make a script to search files for a specific date and string within the file. Then I need the script to move the file that have the specific date and string to a directory. In the interim I have been manually... (7 Replies)
Discussion started by: I4ce
7 Replies
Login or Register to Ask a Question