Forums Register Login

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

+Pie Number of slices to send: Send
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?


+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
Welcome to the Ranch

I added code tags (please always use them) to your post, and doesn't it look better now.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1360 times.
Similar Threads
command execution error
echo <Environment Variable>
command execution
GUI update.
Java interprocess communication
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:46:26.