-2

I have the follow Problem, my file '2018_08_18__Lysto BackUp.plist' looks like this:

/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/exit_codes/
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/exit_codes/code_FUNC
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/exit_codes/code_SCRI
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/login/
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/login/check_appprivilege.php
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/login/check_login.php
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/login/privilege.php
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/scripte/
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/scripte/Lysto BackUp/
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/scripte/Lysto BackUp/sys
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/scripte/Lysto BackUp/sys_func
/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/SSH_ERROR

i read the file with an for loop so i get every single lines in:

mainDirectory

i need the output like this... Count --> '6'

so i need an idea to count every single /*/

1 --> /volume1/
2 --> 02_public/
3 --> 3rd_Party_Apps/
4 --> SPK_SCRIPTS/
5 --> SynoDSApps/
6 --> webapp/

in this case:

/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/scripte/Lysto BackUp/sys_func

i need only this count --> '8'

1 --> /volume1/
2 --> 02_public/
3 --> 3rd_Party_Apps/
4 --> SPK_SCRIPTS/
5 --> SynoDSApps/
6 --> webapp/
7 --> scripte/
8 --> Lysto BackUp/
not in the count result --> sys_func

I hope you Guys can Help me to fix my Problems.... i look for a result since one Week :(

Kalysto
  • 19
  • 6
  • Please avoid *"Give me the codez"* questions. Instead show the script you are working on and state where the problem is. Also see [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/608639) – jww Aug 30 '18 at 02:07

1 Answers1

2

You can get this with awk:

echo "/volume1/02_public/3rd_Party_Apps/SPK_SCRIPTS/SynoDSApps/webapp/scripte/Lysto BackUp/sys_func" | awk -F"/" '{print NF-2}'

Which says:

  1. Split the incoming line by character / (-F"/")
  2. Print the Number of Fields NF minus 2 ({print NF-2}). The "minus 2" for the blank before the first / and the filename in the directory after the last /

You can also pass a file to awk full of paths and have it spit out a new file with the counts or whatever:

awk -F"/" '{print NF-2,$0}` 2018_08_18__Lysto BackUp.plist > outputfile

Which will make a new file called "outputfile" that has the count you are after followed by the path that generated that count (as an example)

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • there you see ^^its awesome i look over a week for a result and 5 minutes later i got the perfect answer ^^:D – Kalysto Aug 28 '18 at 20:55
  • I'm glad it did the trick! If you run into a lot of parsing stuff like this, it's worth it to learn awk. – JNevill Aug 28 '18 at 21:04
  • can you tell me a site where i can learn awk ?? maybe in German ^^ its easier for me ;) – Kalysto Aug 29 '18 at 15:36
  • Harvard has a nice little page explaining the basics. Once you get the basics down, then you get into the fancy stuff to really squeeze value out of it. It's a tool that's been around since the 70s so there is a ton of documentation out there if you search it up. http://www.hcs.harvard.edu/~dholland/computers/awk.html – JNevill Aug 29 '18 at 15:41