LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-05-2015, 12:32 PM   #1
Mikibelavista
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Rep: Reputation: Disabled
How to write shell script for this?


I have seven folders in my folder
meas_2015-06-29_19-03-00 meas_2015-06-29_19-18-28 meas_2015-06-29_19-22-58 meas_2015-06-29_19-26-58 meas_2015-06-29_19-30-58 meas_2015-06-29_19-47-58 meas_2015-06-29_20-39-58
Each of them has inside them xml file,which contains all data about the measurements,including this line
<num_samples>245760</num_samples>
How to write a script that enters each of the folders reads the number of samples and writes them back on the screen?
 
Old 10-05-2015, 12:33 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
grep -Ri num_samples . | wc -l

Last edited by szboardstretcher; 10-05-2015 at 01:11 PM. Reason: Forgot dot
 
Old 10-05-2015, 12:55 PM   #3
Mikibelavista
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Original Poster
Rep: Reputation: Disabled
It is not working.
grep -Ri num-samples | wc -l
0
 
Old 10-05-2015, 01:05 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Probably because you are searching for num-samples with a dash,. and not num_samples with an underscore.
 
Old 10-05-2015, 01:08 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
Blog Entries: 13

Rep: Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945
Quote:
Originally Posted by Mikibelavista View Post
It is not working.
grep -Ri num-samples | wc -l
0
I don't know how that worked anyway to get you an answer. You have a pattern, but need a file for the grep term, such as "*.xml"
Code:
grep -Ri "num-samples" "*.xml" | wc -l
I'd perform the grep without the pipe to wc first just to see what it finds and then add the pipe.
 
1 members found this post helpful.
Old 10-05-2015, 01:29 PM   #6
Mikibelavista
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Original Poster
Rep: Reputation: Disabled
No it is not working because all the xml files are in subdirectories.Again
milenko@milenko-HP-Compaq-6830s:~/MT8$ ls
EDIs meas_2015-06-29_19-03-00 meas_2015-06-29_19-18-28 meas_2015-06-29_19-22-58 meas_2015-06-29_19-26-58 meas_2015-06-29_19-30-58 meas_2015-06-29_19-47-58 meas_2015-06-29_20-39-58
milenko@milenko-HP-Compaq-6830s:~/MT8$ grep -Ri "num-samples" "*.xml" | wc -l
grep: *.xml: No such file or directory
0
 
Old 10-05-2015, 01:37 PM   #7
Mikibelavista
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Original Poster
Rep: Reputation: Disabled
The point is that we need to go to every subdirectory and there we can grep,but not before.
 
Old 10-05-2015, 01:42 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
grep -R means "RECURSE INTO SUBDIRECTORIES"

Look very CAREFULLY AT WHAT YOU ARE TYPING.

Your searching for numDASHsamples,. when what is in your files is numUNDERSCOREsamples.

Read what you have been told, copy and paste the command correctly and come back when you have more information.

I said this already:

Quote:
Probably because you are searching for num-samples with a dash,. and not num_samples with an underscore.
So copy and paste this command...

Code:
grep -Ri "num_samples" "*.xml" | wc -l
#           ^^^^ 
# See? UNDERSCORE _ not DASH -

Last edited by szboardstretcher; 10-05-2015 at 01:46 PM.
 
Old 10-05-2015, 01:49 PM   #9
Mikibelavista
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Original Poster
Rep: Reputation: Disabled
The same.
grep -Ri "num_samples" "*.xml" | wc -l
grep: *.xml: No such file or directory
0
 
Old 10-05-2015, 01:57 PM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Try my way.

grep -Ri num_samples . | wc -l

My test:
Code:
root@scratch:~# mkdir meas_2015-06-29_19-03-00
root@scratch:~# mkdir meas_2015-06-29_19-03-01
root@scratch:~# mkdir meas_2015-06-29_19-03-02
root@scratch:~# mkdir meas_2015-06-29_19-03-03
root@scratch:~# echo "<num_samples>245760</num_samples>" > meas_2015-06-29_19-03-00/somefile.xml
root@scratch:~# echo "<num_samples>245760</num_samples>" > meas_2015-06-29_19-03-01/somefile.xml
root@scratch:~# echo "<num_samples>245760</num_samples>" > meas_2015-06-29_19-03-02/somefile.xml
root@scratch:~# echo "<num_samples>245760</num_samples>" > meas_2015-06-29_19-03-03/somefile.xml
root@scratch:~# grep -Ri "num_samples" "*.xml" | wc -l
grep: *.xml: No such file or directory
0
root@scratch:~# grep -Ri num_samples . | wc -l
4

Last edited by szboardstretcher; 10-05-2015 at 01:58 PM.
 
1 members found this post helpful.
Old 10-05-2015, 02:08 PM   #11
Mikibelavista
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Original Poster
Rep: Reputation: Disabled
Works but wrong number
grep -Ri num_samples .|wc -l
28

<num_samples>245760</num_samples> in 263_2015-06-29_19-03-00_2015-06-29_19-05-00_R000_2048H.xml
<num_samples>11796480</num_samples> in 263_2015-06-29_19-18-28_2015-06-29_19-19-58_R000_131072H.xml

and so on
 
Old 10-05-2015, 02:14 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
Blog Entries: 13

Rep: Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945
While szboardstretcher's grep form does work I've realized that instead for something like this I prefer using the find command with -exec for the grep:
Code:
$ find . -name "*.xml" -exec grep num_samples {} /dev/null \; | wc -l
And I don't think that it gave you the incorrect number, remove the "| wc -l" term and physically could how many occurrences of num_samples it finds. I bet it is 28.

Last edited by rtmistler; 10-05-2015 at 02:15 PM.
 
2 members found this post helpful.
Old 10-05-2015, 02:19 PM   #13
Mikibelavista
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thanks,I got it.
milenko@milenko-HP-Compaq-6830s:~/MT8$ find . -name "*.xml" -exec grep num_samples {} /dev/null \;
./meas_2015-06-29_19-03-00/263_2015-06-29_19-03-00_2015-06-29_19-05-00_R000_2048H.xml: <num_samples>245760</num_samples>
./meas_2015-06-29_19-03-00/263_2015-06-29_19-03-00_2015-06-29_19-05-00_R000_2048H.xml: <num_samples>245760</num_samples>
./meas_2015-06-29_19-03-00/263_2015-06-29_19-03-00_2015-06-29_19-05-00_R000_2048H.xml: <num_samples>245760</num_samples>
./meas_2015-06-29_19-03-00/263_2015-06-29_19-03-00_2015-06-29_19-05-00_R000_2048H.xml: <num_samples>245760</num_samples>
./meas_2015-06-29_19-22-58/263_2015-06-29_19-22-58_2015-06-29_19-24-58_R000_65536H.xml: <num_samples>7864320</num_samples>
./meas_2015-06-29_19-22-58/263_2015-06-29_19-22-58_2015-06-29_19-24-58_R000_65536H.xml: <num_samples>7864320</num_samples>
./meas_2015-06-29_19-22-58/263_2015-06-29_19-22-58_2015-06-29_19-24-58_R000_65536H.xml: <num_samples>7864320</num_samples>
./meas_2015-06-29_19-22-58/263_2015-06-29_19-22-58_2015-06-29_19-24-58_R000_65536H.xml: <num_samples>7864320</num_samples>
./meas_2015-06-29_19-30-58/263_2015-06-29_19-30-58_2015-06-29_19-45-58_R000_4096H.xml: <num_samples>3686400</num_samples>
./meas_2015-06-29_19-30-58/263_2015-06-29_19-30-58_2015-06-29_19-45-58_R000_4096H.xml: <num_samples>3686400</num_samples>
./meas_2015-06-29_19-30-58/263_2015-06-29_19-30-58_2015-06-29_19-45-58_R000_4096H.xml: <num_samples>3686400</num_samples>
./meas_2015-06-29_19-30-58/263_2015-06-29_19-30-58_2015-06-29_19-45-58_R000_4096H.xml: <num_samples>3686400</num_samples>
./meas_2015-06-29_19-26-58/263_2015-06-29_19-26-58_2015-06-29_19-28-58_R000_16384H.xml: <num_samples>1966080</num_samples>
./meas_2015-06-29_19-26-58/263_2015-06-29_19-26-58_2015-06-29_19-28-58_R000_16384H.xml: <num_samples>1966080</num_samples>
./meas_2015-06-29_19-26-58/263_2015-06-29_19-26-58_2015-06-29_19-28-58_R000_16384H.xml: <num_samples>1966080</num_samples>
./meas_2015-06-29_19-26-58/263_2015-06-29_19-26-58_2015-06-29_19-28-58_R000_16384H.xml: <num_samples>1966080</num_samples>
./meas_2015-06-29_19-18-28/263_2015-06-29_19-18-28_2015-06-29_19-19-58_R000_131072H.xml: <num_samples>11796480</num_samples>
./meas_2015-06-29_19-18-28/263_2015-06-29_19-18-28_2015-06-29_19-19-58_R000_131072H.xml: <num_samples>11796480</num_samples>
./meas_2015-06-29_19-18-28/263_2015-06-29_19-18-28_2015-06-29_19-19-58_R000_131072H.xml: <num_samples>11796480</num_samples>
./meas_2015-06-29_19-18-28/263_2015-06-29_19-18-28_2015-06-29_19-19-58_R000_131072H.xml: <num_samples>11796480</num_samples>
./meas_2015-06-29_20-39-58/263_2015-06-29_20-39-58_2015-06-30_15-39-58_R000_128H.xml: <num_samples>8755200</num_samples>
./meas_2015-06-29_20-39-58/263_2015-06-29_20-39-58_2015-06-30_15-39-58_R000_128H.xml: <num_samples>8755200</num_samples>
./meas_2015-06-29_20-39-58/263_2015-06-29_20-39-58_2015-06-30_15-39-58_R000_128H.xml: <num_samples>8755200</num_samples>
./meas_2015-06-29_20-39-58/263_2015-06-29_20-39-58_2015-06-30_15-39-58_R000_128H.xml: <num_samples>8755200</num_samples>
./meas_2015-06-29_19-47-58/263_2015-06-29_19-47-58_2015-06-29_20-37-58_R000_512H.xml: <num_samples>1536000</num_samples>
./meas_2015-06-29_19-47-58/263_2015-06-29_19-47-58_2015-06-29_20-37-58_R000_512H.xml: <num_samples>1536000</num_samples>
./meas_2015-06-29_19-47-58/263_2015-06-29_19-47-58_2015-06-29_20-37-58_R000_512H.xml: <num_samples>1536000</num_samples>
./meas_2015-06-29_19-47-58/263_2015-06-29_19-47-58_2015-06-29_20-37-58_R000_512H.xml: <num_samples>1536000</num_samples>
 
Old 10-05-2015, 02:20 PM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
Blog Entries: 13

Rep: Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945
Quote:
Originally Posted by szboardstretcher View Post
Try my way.

grep -Ri num_samples . | wc -l

My test:
Code:
root@scratch:~# mkdir meas_2015-06-29_19-03-00
root@scratch:~# mkdir meas_2015-06-29_19-03-01
root@scratch:~# mkdir meas_2015-06-29_19-03-02
root@scratch:~# mkdir meas_2015-06-29_19-03-03
root@scratch:~# echo "<num_samples>245760</num_samples>" > meas_2015-06-29_19-03-00/somefile.xml
root@scratch:~# echo "<num_samples>245760</num_samples>" > meas_2015-06-29_19-03-01/somefile.xml
root@scratch:~# echo "<num_samples>245760</num_samples>" > meas_2015-06-29_19-03-02/somefile.xml
root@scratch:~# echo "<num_samples>245760</num_samples>" > meas_2015-06-29_19-03-03/somefile.xml
root@scratch:~# grep -Ri "num_samples" "*.xml" | wc -l
grep: *.xml: No such file or directory
0
root@scratch:~# grep -Ri num_samples . | wc -l
4
Not to hi-jack the thread, this is actually related though. Can you, or someone explain why grep won't work with "*.xml" but does work fine with . (DOT), and actually * (STAR), but "*.*" or "*.xml" do not work. Meanwhile the find works fine.
 
Old 10-05-2015, 02:55 PM   #15
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
Quote:
Originally Posted by rtmistler View Post
Can you, or someone explain why grep won't work with "*.xml" but does work fine with . (DOT), and actually * (STAR), but "*.*" or "*.xml" do not work. Meanwhile the find works fine.
STAR symbol for grep doesn't mean 'any character' as it expected (like in terminal). It is something different - repetition operator:
http://stackoverflow.com/questions/1...r-sign-in-grep
 
2 members found this post helpful.
  


Reply

Tags
scripting, shells


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
[SOLVED] can i write a shell script in /etc/init.d that will run a python script? lapishater276 Debian 1 06-27-2014 03:23 PM
SHELL SCRIPT Write at the right of the shell window solo9300 Linux - General 3 09-29-2009 04:56 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:28 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
Open Source Consulting | Domain Registration