LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2017, 05:48 AM   #1
samasat
LQ Newbie
 
Registered: Nov 2011
Location: Bangalore
Distribution: Ubuntu/Cygwin
Posts: 29

Rep: Reputation: Disabled
find cmd inside nested for loop to explore varying keyword combination


I have a database where I wanted to generate a table of filecount using the combination of the rowKeyword and colKeyword.

Code:
#!/bin/bash

imgRootPath="."

for sCode in "S01" "S02" "S03" "S04" "S05" "S06" "S07" "S08"
do
	for cCode in "C00" "C01" "C02" "C03" "C04" "C05" "C06"
      do

#method 1 - working		
		echo $sCode $cCode 
		find "$imgRootPath" -type f -iname "*.bmp" -name "*_${sCode}_*" -name "*_${cCode}_*" | wc -l

#method 2
#		echo $sCode $cCode $( find "$imgRootPath" -type f -iname "*.bmp" -name "*_${sCode}_*" -name "*_${cCode}_*" | wc -l ) 
	
#method 3
#		echo $sCode $cCode $($( find "$imgRootPath" -type f -iname "*.bmp" -name "*_${sCode}_*" -name "*_${cCode}_*" | wc -l )) 

#method 4
#		count=$( find "$imgRootPath" -type f -iname "*.bmp" -name "*_$sCode_*" -name "*_$cCode_*" | wc -l ) 
#		echo $sCode $cCode $count			
	
#method 5
#		count=$( find "$imgRootPath" -type f -iname "*.bmp" -name "*_${sCode}_*" -name "*_${cCode}_*" | wc -l ) 
#		echo $sCode $cCode $count			

#trial 6
#	        echo "$(find $imgRootPath  -type f -iname "*.bmp" -name "*_${sCode}_*" -name "*_${cCode}_*" | wc -l)" 

      done
done
Output at the terminal looks like this:
PHP Code:
$ ./tabulateCnt.sh
S01 C00
189
S01 C01
711
S01 C02
160
S01 C03
49
S01 C04
0
:
:

As you can see in the output above, I get the filecount, however, I can not output it in the intended readable format like a table as seen below:

PHP Code:
    C01    C02    C03   ...
S01  n11   n12    n13

S02  n21   n22    n23

S03  n31   n32    n33  

:


OR at least as my failed attempts were trying to get

PHP Code:
S01 C01 n11
S01 C02 n12
S01 C03 n13
:
:
S02 C01 n21
S02 C02 n22
:

If I run any of the other cmd method (currently commented out) in the code above, nothing shows up on the screen in place of n** but the S0* and C0* get printed. The execution lag suggests that the "find" cmd is being executed in the bkgrnd. Can someone guide me to improve readability of the output ?
 
Old 06-29-2017, 05:58 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
echo has a -n flag, probably missed.
 
1 members found this post helpful.
Old 06-29-2017, 06:22 AM   #3
samasat
LQ Newbie
 
Registered: Nov 2011
Location: Bangalore
Distribution: Ubuntu/Cygwin
Posts: 29

Original Poster
Rep: Reputation: Disabled
Smile additionally one needs to suppress newline character introduced by "wc -l" using "xarg echo -n"

Thanks pan64. With echo -n I could get the second way of printing out the result.

With some modification in original code I tried to print tabulated output (first one intended display in first post). It was not successful as the find * * * * | wc- l command output introduces a trailing newline.

Following remedy using "xargs" and "echo" helped to suppress newline comment introduced by the wc-l cmd output:

Code:
 find "$imgRootPath" -type f -iname "*.bmp" -name "*_${sCode}_*" -name "*_${cCode}_*" | wc -l | xargs echo -n " "

Thanks again

Last edited by samasat; 06-29-2017 at 06:24 AM. Reason: grammatical correction
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Thorough search for a text file with a specific keyword inside educateme Linux - General 6 10-11-2013 09:34 AM
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] for loop and nested find kez1985 Linux - Newbie 1 10-01-2010 10:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:04 PM.

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