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
htdig(1)						      General Commands Manual							  htdig(1)

NAME
htpurge - remove unused documents from the database (general maintenance script) SYNOPSIS
htpurge [-][-a][-c configfile][-u][-v] DESCRIPTION
Htpurge functions to remove specified URLs from the databases as well as bad URLs, unretrieved URLs, obsolete documents, etc. It is recom- mended that htpurge be run after htdig to clean out any documents of this sort. OPTIONS - Take URL list from standard input (rather than specified with -u). Format of input file is one URL per line. -a Use alternate work files. Tells htpurge to append .work to database files, causing a second copy of the database to be built. This allows the original files to be used by htsearch during the run. -c configfile Use the specified configfile instead of the default. -u URL Add this URL to the list of documents to remove. Must be specified multiple times if more than one URL are to be removed. Should nor be used together with -. -v Verbose mode. This increases the verbosity of the program. Using more than 2 is probably only useful for debugging purposes. The default verbose mode (using only one -v) gives a nice progress report while digging. FILES
/etc/htdig/htdig.conf The default configuration file. SEE ALSO
Please refer to the HTML pages (in the htdig-doc package) /usr/share/doc/htdig-doc/html/index.html and the manual pages htdigconfig(8) , htdig(1) and htmerge(1) for a detailed description of ht://Dig and its commands. AUTHOR
This manual page was written by Robert Ribnitz, based on the HTML documentation of ht://Dig. January 2004 htdig(1)
All times are GMT -4. The time now is 04:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy