Sponsored Content
Top Forums Shell Programming and Scripting Parsing and Editing a json file with bash script Post 302971476 by RudiC on Wednesday 20th of April 2016 03:36:04 PM
Old 04-20-2016
Well, although a parser is not simply done but usually an intricate piece of software, here is
- a bash script
- replacing ALL kind of URLs (abc.com; def.org; ghi.de; ...)
- only in "appMar" section
As the URL can assume any shape, we can't rely on a string constant ("URLnn") but need to check for the file structure (the URL is the first entry in the second nesting level).

Try
Code:
#!/bin/bash
#
TGSCT=0
LEVEL=0
URL=""

declare -A RP
RP["a"]=XYZ.com                                         # set up replacement URLs
RP["b"]=YZX.org
RP["c"]=ZXY.biz

while IFS="" read LN                                    # don't destroy original line
 do     IFS=": " read P1 P2 R <<< $LN                   # get elements from line; use bashs "here string"
        [ "$P1" != "${P1/appMap}" ] && TGSCT=1          # test for the right target section ("appMap")

        OLDLVL=$LEVEL                                   # save nesting level
        T1=${LN//\{}                                    # compute actual nesting level (based on braces)
        T2=${LN//\}}
        LEVEL=$((LEVEL + ${#T2} - ${#T1}))              # which is count opening braces minus count closing braces

        [ "$TGSCT" -eq 0 ] &&   { echo "$LN"
                                  continue
                                }                       # not in right section: print and leave

        [ "$OLDLVL" -eq 2 ] &&  { URL=${P1//\"}         # test for nesting level (URL is 2), compute URL w/o double quotes
                                  TMP="$LN"             # save URL in variable and total line in temp. var
                                }

        [ "$P1" != "${P1/name}" ] && {  NM=${P2//\"}    # test for "name" tag, compute name value w/o double quotes
                                        LN="${TMP/$URL/${RP[$NM]}}"$'\n'"$LN"
                                                        # replace old URL with new URL
                                        URL=""; }       # empty URL variable

        [ -z "$URL" ] && echo "$LN"                     # print only if no URL has been intercepted

        [ "$LEVEL" -eq 0 ] && TGSCT=0                   # zero nesting level means target section left

   done < file                                          # read from json file

This is just straightforward functionality to prove it's working, there's nothing extra like error checking or debugging. If need be, this is left to the reader.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conversion of bash parsing script to perl?

I need help with a perl parsing script. I have some error logs on a windows machine that I need to parse from a text file, but I know nothing about perl. I usually run this bash script on my linux box and it does just what I need. How would I do the same thing with perl and port it to my windows... (2 Replies)
Discussion started by: cstovall
2 Replies

2. Web Development

bash script editing my apache config files

okay i'm going to try to say this uber-simple: I use dropbox (file-sync service). in order for dropbox sync files, they must be its children eg. somewhere under /home/jzacsh/Dropbox]. I want to now use it to keep my development files in sync across my machines: easy: just move my dev. files... (2 Replies)
Discussion started by: jzacsh
2 Replies

3. Shell Programming and Scripting

Help parsing filename with bash script

Hi all! Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to... (4 Replies)
Discussion started by: mtehonica
4 Replies

4. Shell Programming and Scripting

Problems editing file with awk in bash script

Hello dear users, here I have a script to manipulate .csv files that are like this originally: And I need to make a script to delete certain fields. Each field is separated with a comma. So, here is my script (at least a part of it): Field $1 is composed of a name, and then a... (5 Replies)
Discussion started by: sr00t
5 Replies

5. Shell Programming and Scripting

Column parsing in a bash script - HELP

I would like to setup a script that pulls in time/date in two seperate columns, and then name the other columns as listed below: Column1=Art/TJ output Column2=Art/TJ output Column3=TJ output column4=Art output Column5=If time/date past 12:00 noon -fail Colume6=If time/date before... (1 Reply)
Discussion started by: walnutpony123
1 Replies

6. Shell Programming and Scripting

How to define a variable in a BASH script by using a JSON file online?

Hello, I would like to modify an existing script of mine that uses a manually defined "MCVERSION" variable and make it define that variable instead based on this JSON file stored online: https://s3.amazonaws.com/Minecraft.Download/versions/versions.json Within that JSON, I 'm looking for... (4 Replies)
Discussion started by: nbsparks
4 Replies

7. Shell Programming and Scripting

Bash script not parsing file with spaces in path

Hi everyone, I'm trying to write my first ever shell script, the OS is Raspbian. The code I have written must be executed whenever a certain database has been modified. The database resides on a Windows server to which I have a mount point, and I have no control over the Windows server at all so... (2 Replies)
Discussion started by: gjws
2 Replies

8. Shell Programming and Scripting

Bash script - cygwin (powershell?) pull from GitHub API Parse JSON

All, Have a weird issue where i need to generate a report from GitHub monthly detailing user accounts and the last time they logged in. I'm using a windows box to do this (work issued) and would like to know if anyone has any experience scripting for GitAPI using windows / cygwin / powershell?... (9 Replies)
Discussion started by: ChocoTaco
9 Replies

9. Shell Programming and Scripting

JSON structure to table form in awk, bash

Hello guys, I want to parse a JSON file in order to get the data in a table form. My JSON file is like this: { "document":{ "page": }, { "column": } ] }, { ... (6 Replies)
Discussion started by: Gescad
6 Replies

10. Shell Programming and Scripting

Building JSON command with bash script

Hello. I'm new to bash script and I'm learning the basics by writing some scripts. Recently a friend of mine asked me if I could try to write a script to him to automate a couple of processes that uses JSON RPCs. I'll try to explain in few words the workflow just to contextualize the problem.... (48 Replies)
Discussion started by: psysc0rpi0n
48 Replies
IO::All::HTTP(3pm)					User Contributed Perl Documentation					IO::All::HTTP(3pm)

NAME
IO::All::LWP - Extends IO::All to HTTP URLs SYNOPSIS
use IO::All; $content < io('http://example.org'); # GET webpage into scalar io('http://example.org') > io('index.html'); # GET to file "hello " > io('http://example.org/index.html'); # PUT webpage # two ways of getting a page with a password: $content < io('http://me:secret@example.org'); $content < io('http://example.org')->user('me')->password('secret'); DESCRIPTION
This module extends IO::All for dealing with HTTP URLs. Note that you don't need to use it explicitly, as it is autoloaded by IO::All whenever it sees something that looks like an HTTP URL. The SYNOPSIS shows some simple typical examples, but there are many other interesting combinations with other IO::All features! For example, you can get an HTTP URL and write the content to a socket, or to an FTP URL, of to a DBM file. METHODS
This is a subclass of IO::All::LWP. The only new method is "http", which can be used to create a blank IO::All::HTTP object; or it can also take an HTTP URL as a parameter. Note that in most cases it is simpler just to call io('http://example.com'), which calls the "http" method automatically. OPERATOR OVERLOADING
The same operators from IO::All may be used. < GETs an HTTP URL; > PUTs to an HTTP URL. SEE ALSO
IO::All, IO::All::LWP, LWP. AUTHORS
Ivan Tubert-Brohman <itub@cpan.org> and Brian Ingerson <ingy@cpan.org> COPYRIGHT
Copyright (c) 2007. Ivan Tubert-Brohman and Brian Ingerson. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See <http://www.perl.com/perl/misc/Artistic.html> perl v5.10.0 2007-03-29 IO::All::HTTP(3pm)
All times are GMT -4. The time now is 11:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy