0

I am trying to get a substring from a line in a file using a shell script. The sticky part is the line in the file is delimited by double quotes. I'm trying to get the string that represents a version number that is between the second set of double quotes.

This is the line in my file:

"parentDirectory/childLibrary": "2.4.177"

I'm trying to extract 2.4.177 and store it in a variable so I can use it later on in my shell script.

Here is my shell script so far:

currentVersion=$(grep '\"parentDirectory/childLibrary\": \"' somefile.txt)

this gets the line that has the version number in it that I want to work with, but I can't figure out how to get what is between the 3rd and 4th double quotes in the string. I can't just say take x amount of characters starting at position y because I don't know how long the substring will be. The closest thing I've found is the answer here but I can't figure out how to use this to set a variable equal to the substring I want it to. Any help would be much appreciated. As always, a working answer that is explained clearly will be upvoted and marked as selected answer. Thanks in advance.

Gharbad The Weak
  • 1,541
  • 1
  • 17
  • 39
  • 3
    Is this is a JSON file that you are trying to parse? Download and setup a syntax aware parser like `jq` instead of line oriented tools `grep` – Inian Jul 16 '19 at 03:32
  • Yes this is a JSON file but my situation requires that I have to use a shell script to accomplish what I need to do because of the total scope of what I am doing. – Gharbad The Weak Jul 16 '19 at 03:38
  • After grep, `currentVersion=$( echo $currentVersion | tr - d '"' ) `. This will delete `"` in the line. After this `currentVersion=${currentVersion##*: } `. I am currently on my phone so can't test it. I don't remember exactly which one to use (#, ##, %, %%). But you can solve with similar approach. – Mihir Luthra Jul 16 '19 at 03:40
  • See [Parsing JSON with Unix tools](https://stackoverflow.com/q/1955505/5291015) – Inian Jul 16 '19 at 03:40
  • If your "total scope" doesn't allow you to use the right tool for the job, I'd take a step back and rethink your approach, to be honest. – Benjamin W. Jul 16 '19 at 04:10
  • @tripleee I understand that there are unix tools for parsing JSON files, but with my shell script I am creating I'm doing more than just parsing a JSON file, so I need to be able to get a substring from my JSON file but I need to do a lot more than that in my shell script. Please remove the `Marked as duplicate` so I can get an answer to my question – Gharbad The Weak Jul 16 '19 at 16:49
  • Several of the answers in the duplicate show exactly how to use Bash alone or standard Unix utilities like Awk or `sed` to extract information from JSON. You really need to show how this is not a duplicate, or perhaps specify precisely which part *after* the actual extraction isn't obvious. – tripleee Jul 16 '19 at 16:55
  • Trivially `awk -F '"' '{ print $4 }' file` extracts the string between the third and fourth double quote on a line. Factoring your regex into the Awk script should be another 5 seconds of effort. (Hint: http://www.iki.fi/era/unix/award.html#grep) – tripleee Jul 16 '19 at 17:01

0 Answers0