• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we execute WC -l unix command using processBuilder in java  RSS feed

 
Kala Saseendran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below program is working fine. But intead of ls i want to get the wordcount of the files. is it possible to do that?


 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for each file in a directory you want to obtain the number of lines in that file. Yes it is possible but before rushing off you should note that ou are falling for at least two of the traps detailed in the 4 sections of this . Even though it is primarily concerned with Runtime.exec() the traps article applies to Process objects created by ProcessBuilder; you should implement all the recommendations. Now you might get away without implementing some of the recommendations but I will guarantee that at some time failure to do so will jump up and bite you.

For the 'bash' shell the command is
Check the documentation to see what you need for your shell.

P.S. Of course you now have to parse the output to do anything with the word counts. It might be easier not to use ProcessBuilder and just to count the number of lines using a Java program!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags (please always use them) to your post, and doesn't it look better now.
 
Kala Saseendran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help. Will try this option.

I am not aware about code tags. will try to include them while posting code next time.

Thanks
Kala
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kala Saseendran wrote:Thank you for the help. Will try this option.


I hope you will also read the traps article and implement the recommendations! You may think that in your case implementing the recommendations is unnecessary and you may get away with it this time but are you willing to risk looking silly when your code becomes deadlocked and the finger of blame point at you?
 
Kala Saseendran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i read the article.. and after that i am giving it a second thought whether i should use it or not. I need line count of a single file which can have 2 to 3 lakh lines. That is why i thought about this option. But learning how to do it was a good experience. Thank you

Thanks
Kala
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!