1

How can I remove any white space if any after parsing from JSON?

whereami -r Parse result without sed.

{
  "ip": "95.16.15.10",
  "country_code": "US",
  "country_name": "United States",
  "region_code": "NY",
  "region_name": "New York",
  "city": "The Bronx",
  "zip_code": "10473",
  "time_zone": "America/New_York",
  "latitude": 40.822,
  "longitude": -73.86,
  "metro_code": 501
}

Working one but with space.

whereami -r | sed -rn 's/(^.*city": ")(.*)(".*$)/\2/p'

The Bronx

Sort of working JSON result using sed -rn 's/(^.*city": ")(.*)(".*$)/\2/;s/ //p'

"ip": "95.16.15.10",
 "country_code": "US",
 "country_name": "United States",
 "region_code": "NY",
 "region_name": "New York",
TheBronx
 "zip_code": "10473",
 "time_zone": "America/New_York",
 "latitude": 40.822,
 "longitude": -73.86,
 "metro_code": 501

What I'm trying to accomplished

TheBronx 
2
  • Sure about the "any whitespace"? Eg. "Los Angeles" => "LosAngeles" ? Commented Dec 22, 2020 at 4:26
  • Yes, exactly like that.
    – robotron
    Commented Dec 22, 2020 at 4:27

3 Answers 3

1

awk has no problems with that

awk  '
    $1 == "\"city\":" {
      $1 = ""
      gsub(/ /,"")
      sub(/^"/,"")
      sub(/",$/,"")
      print
    }
'

I'm sure sed can handle it too, but I've never managed its logic. But have a look on the internet eg. "sed oneliners".

The proof of the pudding is in the eating:

$ awk  '
>     $1 == "\"city\":" {
>       $1 = ""
>       gsub(/ /,"")
>       sub(/^"/,"")
>       sub(/",$/,"")
>       print
>     }
> '
{
  "ip": "95.16.15.10",
  "country_code": "US",
  "country_name": "United States",
  "region_code": "NY",
  "region_name": "New York",
  "city": "The Bronx",
  "zip_code": "10473",
  "time_zone": "America/New_York",
  "latitude": 40.822,
  "longitude": -73.86,
  "metro_code": 501
}
TheBronx
4
  • awk: cmd. line:1: $1 == "\"city\":" {$1 = "" gsub(/ /,"") sub(/^"/,"") sub(/",$/,"") print } awk: cmd. line:1: ^ syntax error
    – robotron
    Commented Dec 22, 2020 at 15:50
  • I've added a test run to my answer. Commented Dec 22, 2020 at 16:24
  • You're right, I see what my problems was, I'm trying to get this in one single line to do this location=`sudo whereami -r | awk '$1 == "\"city\":" { $1 = "" gsub(/ /,"") sub(/^"/,"") sub(/",$/,"") print }' echo'$location'
    – robotron
    Commented Dec 22, 2020 at 16:53
  • Try with ";" at the end of each command. "echo '$location'" won't work - well, it won't do what you want. Put $location between double quotes. Commented Dec 22, 2020 at 17:03
0

You can modify the sed pattern to replace whitespace. sed allows chaining patterns.

's/(^.*city": ")(.*)(".*$)/\2/;s/ //p'
3
  • That does work in a way but it shows the rest of the JSON result that was hidden with 's/(^.*city": ")(.*)(".*$)/\2/p'
    – robotron
    Commented Dec 22, 2020 at 4:38
  • Could you please post a sample json ? Commented Dec 22, 2020 at 4:49
  • I modified the post check again.
    – robotron
    Commented Dec 22, 2020 at 5:02
0

To extract all values in proper list form to a file using sed.

sed 's/["{}\]//g' <your_file.json> | sed 's/,/\n/g' >> <your_new_file_to_save>
  • sed 's/regexp/replacement/g' inputFileName > outputFileName.
  • In some versions of sed, the expression must be preceded by -e to indicate that an expression follows.
  • The s stands for substitute, while the g stands for global, which means that all matching occurrences in the line would be replaced. I've put [ ] inside it as elements that you wanna remove from .json file.
  • The pipe character | is used to connect the output from one command to the input of another. then last i did substitute "," and add '\n' known as line breaker.

If you want to show a single value see below command:

sed 's/["{}\]//g' <your_file.json> | sed 's/,/\n/g' | sed 's/<ur_value>//p'

For example You wanna extract the value of ip

sed 's/["{}\]//g' file.json | sed 's/,/\n/g' | sed 's/ip : //p'

Result :- 95.16.15.10

  • p is run; this is equivalent to /pattern match/! p as per above, i.e., "if the line does not match /pattern match/ , print it". So the complete command prints all the lines from the first occurrence of the pattern to the last line, but suppresses the ones that match.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .