Home > database >  Only want filename from url in json file
Only want filename from url in json file

Time:06-09

I have the following json file below:

{"cloud":"https://cloudfronturl/folder/folder",
"env": "int"
"sources":["https://www.example.com/some.tar.gz","https://www.example2.com/folder1/folder2/another.tar.gz"],
"owner": "some manager"
}

How can I modify the file to read like below, where only the file names stripped from sources url? Don't want to touch cloud value

{"cloud":"https://cloudfronturl/folder/folder",
"env": "int"
"sources":["some.tar.gz","another.tar.gz"],
"owner": "some manager"
}

CodePudding user response:

Use this Perl one-liner:

perl -i.bak -pe 's{http[^"] /}{}g' in_file

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.

The regex uses this modifier:
/g : Match the pattern repeatedly.

http[^"] / : literal http, followed by any character other than ", repeated 1 or more times.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlre: Perl regular expressions (regexes)
perldoc perlre: Perl regular expressions (regexes): Quantifiers; Character Classes and other Special Escapes; Assertions; Capture groups
perldoc perlrequick: Perl regular expressions quick start

CodePudding user response:

Assuming your JSON snippet is fixed and using jq is an option, you could do

jq '.sources[] |= ( split("/") | last )'
  • Related