I have a folder which have multiple Jar files.
* dummy.jar
* myjar-xx.jar
* myjar-yy.jar

Using find, I need to find first file starting with "myjar"

Now, I want to grep the contents of the Manifest file inside the Jar file found using find command.

I am using following command to check contents of Manifest file.

java -jar $(find . -type f -name "myjar-*" -print | head -n 1) -ai

It gives output on console -

Version: 2   
Name: MyName   
Company: ABC

Now, since on console i can see the contents of the file, how can I search whether "Na" exists for line which contains "Name" ?

  • Please edit your question and explain what you are trying to do. Specifically, what you think the find command is doing. It will just print the 1st file name found that begins with myjar-, is that what you want? You seem to want to parse the manifest file somehow but you don't tell us what you want to achieve. – terdon Mar 24 '15 at 11:52
  • Done. Does it make sense now ? – Novice User Mar 24 '15 at 12:02
  • Thanks, better but still not clear. What is your desired output from the find and head commands? You seem to think that they will return MyName. Is that correct? You need to explain exactly what you are attempting. You say that you're using bash "to do that" but what is that? – terdon Mar 24 '15 at 12:04
  • Could you take a look now ? – Novice User Mar 24 '15 at 12:10

If I understand correctly, you are saying that this command:

java -jar $(find . -type f -name "myjar-*" -print | head -n 1) -ai

Returns this output:

Version: 2   
Name: MyName   
Company: ABC

And you want to check whether that output contains a line that starts with Name: and contains the string Na (excluding the Na in Name:). If so, you could do:

java -jar $(find . -type f -name "myjar-*" -print | head -n 1) -ai |
     | grep -q '^Name:.*Na' && echo YES

The -q flag tells grep not to print its matches. It simply returns a 0 exit code if a match was found. Therefore, you can use && command to run a command if the match was successful. Here, I am running echo YES but you can change that to whatever command you want to run.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.