Grep from txt file (JSON format)

Related searches

I have a txt in a JSON format:

{
  "items": [ {
    "downloadUrl" : "some url",
    "path": "yxxsf",
    "id" : "abc",
    "repository" : "example",
    "format" : "zip",
    "checksum" : {
      "sha1" : "kdhjfksjdfasdfa",
      "md5" : "skjfhkjshdfkjshfkjsdhf"
    }
  }],
  "continuationToken" : null
}

I want to extract download url context (in this example i want "some url") using grep and store it in another txt file. TBH i have never used grep

command line - grepping patterns in a json file, How can I select the lines from my text files similar to this one "created_at": "Wed � The Grep CommandPermalink. A basic grep command uses the following syntax: grep "string" ~/threads.txt. The first argument to grep is a search pattern. The second (optional) argument is the name of a file to be searched. The above sequence will search for all occurrences of “string” in the ~/threads file.

Using grep

grep -oP 'downloadUrl"\s:\s"(.*)",' myfile > urlFile.txt

See this Regex in action: https://regex101.com/r/DvnXCO/1

A better way to do this is to use jq

Download jq for Windows: https://stedolan.github.io/jq/download/

jq ".items[0].downloadUrl" myfile > urlFile.txt

Grep a string from json data, Try the below command $ sed "s/{/\n/g" file.txt | awk -F, 'NF>=2{split($2,Arr,":"); print Arr[2]}' "FORESTER" "OUTBACK". we break the single line� The added whitespace does not affect the actual JSON data. To extract the values of all entries corresponding to the key CAR, use jq, a command line JSON parser: $ jq '.[] | .CAR' file.json "FORESTER" "OUTBACK" The expression .[] | .CAR means "For each element of the top-most array, return the value of the key CAR. You could also have used .[].CAR.

If you want to use only grep:

grep downloadURL myfile > new_file.txt

If you prefer a cleaner option, adding cut command:

grep downloadURL myfile | cut -d\" -f4 > new_file.txt

BTW the image of the json file shows that you are using notepad (windows?)

Controlling command output from the AWS CLI, How to select the output formatJSON output formatYAML output formatText output formatTable output formatHow to filter the output with the --query optionHow to� -l just print the filenames of files that contain the match.* any number of any characters here; It's probably OK to parse this file with grep especially for something so simple, but usinga JSON parser as mentioned in David Foerster's answer is the Right Way (i.e. it will likely be more reliable, especially if you need to do anything complex).

Can grep show context, but not a full line?, I want to grep for a string which may occur several times in the file, including This question is old, but since I stumbled on it while looking for a way to grep only part of a line, here goes: and then to use RegExp's power to grep a bit more than your text: Bet way I know to display json in a pretty format is using pjson. $ grep -o '"VpcId": "[^"]*' infile | grep -o '[^"]*$' vpc-123 The first one extracts up to and excluding the closing quotes, the second one searches from the end of the line for non-quotes. But, as mentioned, you'd be better off properly parsing your JSON. Apart from jq mentioned in another answer, I know of. Jshon; JSON.sh

Guide to Linux jq Command for JSON Processing, JSON is a widely used structured data format typically used in most modern APIs and data services. text manipulation using a combination of tools such as sed and grep. We can also apply this filter directly to a JSON file:� Search String In A File Search String In Multiple Files. In previous example we have searched given string in a single file but real world problems are more than that. We can search string in multiple files by providing file name or extension with the help asterisk. In this example we will search in all text files by specifying *.txt file name.

The syntax is: grep -R --include =GLOB "pattern" / path / to /dir grep -R --include = "*.txt" "pattern" / path / to /dir grep -R --include = "*.txt" "foo" ~ / projects / You can include files whose base name matches GLOB using wildcard matching.

Comments
  • Please copy and paste the actual file contents into your question instead of posting a picture of them. Then, grep is the wrong tool for JSON parsing; you should use something like jq for this. And finally, if you have never used grep, it feels like you're expecting somebody else to do your work; you have to make an effort yourself first.
  • I was expecting a syntax of grep that can find that particular url. Thank you for your time. I researched about it and found the solution using regex
  • grep -oP 'some..([^"]+)*' worked perfect for me. Thanks for the answer tho. Really appreciate that
  • @Peace9795 thank you for the feedback. Good to know it works. If you consider your problem is well solved with my answer, I'd appreciate if you can accept it by clicking on the check mark beside the answer. BR.
  • I would like to it with grep only That command also copied downloadUrl" : "some url", I just want some url no commas no colons
  • @Peace9795, be mindful that grep solution is prone to failures. E.g.: "downloadUrl": label and "some url" could reside on different lines, which is still a valid JSON, but will break grep based solutions
  • Yes,I'm on windows running a bash script to get the url. I have used curl to get that information and put it in a txt file
  • It didn't work. The new file is empty FYI: I want to extract only "some url" (if you see the text file)