LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find cmd inside nested for loop to explore varying keyword combination (https://www.linuxquestions.org/questions/linux-newbie-8/find-cmd-inside-nested-for-loop-to-explore-varying-keyword-combination-4175608835/)

samasat 06-29-2017 05:48 AM

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 ?

pan64 06-29-2017 05:58 AM

echo has a -n flag, probably missed.

samasat 06-29-2017 06:22 AM

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


All times are GMT -5. The time now is 02:06 AM.