LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-06-2009, 03:54 AM   #1
bhagirathi
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Rep: Reputation: 0
read files from a folder and grep a string


Hi

I have a folder and there some random files are created after my product run. The files names are different but extn is .xml. and i have to grep a particular string from all the files and get the total count. like in that folder the files are q12qw.xml,w23ws.xml,s24de.xml ... and from these files i have to count the name "<CX900BT>". So in my script it reads the file but cannot grep the string.

#!/bin/bash
# read files from a folder and grep a string
LIST="$(ls *.xml)"
count=0
for i in "$LIST"; do
count1=$(grep -c '<CX900BT>' $i)
#echo $count1
count=$count+$count1
done
echo $count

The output comes as

0+q12qw.xml:1 w23ws.xml:9 s24de.xml:1

But i want like it come

0+1+9+1=11 or total=11



Could any one help me.
 
Old 07-06-2009, 04:14 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
awk '/CX900/{s+=1}END{print s}' *.xml
 
Old 07-06-2009, 04:15 AM   #3
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Lenny
Posts: 351

Rep: Reputation: 47
can you remove "" from the list -> this makes it a single line rather than a list
i.e
change
for i in "$LIST"; do
AS

for i in $LIST; do
 
Old 07-06-2009, 04:23 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
The one liner given by ghostdog is definitely a good solution for your problem
In case you want correction in your script see this code

Code:
#!/bin/bash
# read files from a folder and grep a string
LIST="$(ls *.txt)"

count=0
for i in $LIST; do
count1=`grep -c 'CX900BT' $i | cut -d":" -f2`
count=`echo $count+$count1 | bc`
done
echo $count

Last edited by PMP; 07-06-2009 at 04:26 AM.
 
Old 07-06-2009, 04:24 AM   #5
bhagirathi
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bhaslinux View Post
can you remove "" from the list -> this makes it a single line rather than a list
i.e
change
for i in "$LIST"; do
AS

for i in $LIST; do
Thanks , but the final addition is not coming
 
Old 07-06-2009, 04:26 AM   #6
bhagirathi
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by ghostdog74 View Post
Code:
awk '/CX900/{s+=1}END{print s}' *.xml
Thanks ghostdog
 
Old 07-06-2009, 06:27 AM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 328

Rep: Reputation: 140Reputation: 140
If the files are cat to grep it treats them as one continuous file and only outputs the count total.
cat *.xml | grep -c '<CX900BT>'
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not able to read list of files in folder from java sbzenith Linux - Newbie 2 05-26-2009 01:50 AM
How to grep for a string in a file kaprasanna Linux - Newbie 3 01-06-2009 06:29 AM
grep the exact string only ZAMO Linux - General 11 08-28-2008 05:08 AM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 09:40 AM
grep - finding string and replacing with new ckibler Linux - Newbie 6 08-01-2003 06:25 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration