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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
10-05-2015, 12:32 PM
|
#1
|
LQ Newbie
Registered: Mar 2015
Posts: 20
Rep:
|
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?
|
|
|
10-05-2015, 12:33 PM
|
#2
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
grep -Ri num_samples . | wc -l
Last edited by szboardstretcher; 10-05-2015 at 01:11 PM.
Reason: Forgot dot
|
|
|
10-05-2015, 12:55 PM
|
#3
|
LQ Newbie
Registered: Mar 2015
Posts: 20
Original Poster
Rep:
|
It is not working.
grep -Ri num-samples | wc -l
0
|
|
|
10-05-2015, 01:05 PM
|
#4
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Probably because you are searching for num-samples with a dash,. and not num_samples with an underscore.
|
|
|
10-05-2015, 01:08 PM
|
#5
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
Quote:
Originally Posted by Mikibelavista
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.
|
10-05-2015, 01:29 PM
|
#6
|
LQ Newbie
Registered: Mar 2015
Posts: 20
Original Poster
Rep:
|
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
|
|
|
10-05-2015, 01:37 PM
|
#7
|
LQ Newbie
Registered: Mar 2015
Posts: 20
Original Poster
Rep:
|
The point is that we need to go to every subdirectory and there we can grep,but not before.
|
|
|
10-05-2015, 01:42 PM
|
#8
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
|
|
|
10-05-2015, 01:49 PM
|
#9
|
LQ Newbie
Registered: Mar 2015
Posts: 20
Original Poster
Rep:
|
The same.
grep -Ri "num_samples" "*.xml" | wc -l
grep: *.xml: No such file or directory
0
|
|
|
10-05-2015, 01:57 PM
|
#10
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
|
10-05-2015, 02:08 PM
|
#11
|
LQ Newbie
Registered: Mar 2015
Posts: 20
Original Poster
Rep:
|
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
|
|
|
10-05-2015, 02:14 PM
|
#12
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
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.
|
10-05-2015, 02:19 PM
|
#13
|
LQ Newbie
Registered: Mar 2015
Posts: 20
Original Poster
Rep:
|
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>
|
|
|
10-05-2015, 02:20 PM
|
#14
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
Quote:
Originally Posted by szboardstretcher
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.
|
|
|
10-05-2015, 02:55 PM
|
#15
|
Member
Registered: Apr 2012
Distribution: Gentoo
Posts: 616
Rep:
|
Quote:
Originally Posted by rtmistler
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.
|
All times are GMT -5. The time now is 05:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|