Utilizing Powershell to eliminate characters from a string

After pulling a list of IPs from a JSON file, I've been using the code snippet below:

$Request = 'https://url-to-json.com/file.json'
$AWSIPs = Invoke-WebRequest $Request | ConvertFrom-Json | Select-Object prefix -ExpandProperty prefixes -ExcludeProperty ("/.*") | Where-Object -Property "service" -EQ "service_name" | select ip_prefix
foreach ($awsip in $AWSIPs){
echo $awsip
}

The IPs are displayed as follows: - 0.0.0.0/00

  • 0.0.0.0/00
  • 0.0.0.0/00
  • 0.0.0.0/00
  • 0.0.0.0/00
  • 0.0.0.0/00

However, I now need to strip out the "/00" at the end before proceeding with using these IPs.

I would be grateful for any assistance in achieving this task.

Many thanks.

Answer №1

Here is a solution using the -replace method:

$ips =  '192.168.1.1/24',
        '10.0.0.1/16',
        '172.16.0.1/12'

$ips -replace '(.*)/.*', '$1'

You can see that you can apply the -replace directly to an array in PowerShell.

The result will be:

192.168.1.1
10.0.0.1
172.16.0.1

Another option is to use -split, but it requires an explicit loop:

foreach ($ip in $ips) {
  ($ip -split '/')[0]
}

Although it's possible to avoid the loop, it's not recommended for performance and readability reasons. Here is an example that utilizes PowerShell's flexibility:

($ips -split '/')[(0..($ips.Count-1)).ForEach({ $_ * 2 })]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to modify a JSON file using PowerShell?

There's a json file named mytest.json, and I am looking to modify its values using a PowerShell script. The update.json file looks like this: { "update": [ { "Name": "test1", "Version": "2.1" }, ...

Understanding and aggregating data from JSON files

I am in possession of a group of json values outlined below {"labels":[ "time", "softirq", "user", "system", "nice", "iowait" ], "data":[ [ 1490088352, 0, 14.64646, 3.53535, 0, 1.0101 ], [ 1490088351, 0, 27.77778, 3.0303, 0, 0 ], [ 1490088350, 0.49751, 12 ...

Turning PHP array into JSON using Zend RPC

Greetings, I am facing a small issue that I need help with. Currently, I am developing a RPC service using ZendFramework and Apigility which requires the response to be in a json array format. Below is the content negotiation code snippet that I have imple ...

Inaccurate formatting of body in Swift POST request received by node.js/express application

In my ios app using Swift, I am encountering an issue when sending a JSON object. The problem lies in the format of the request body on the node.js/express backend; it is awkwardly parsed where the entire JSON object from Swift is treated as the key. This ...

While going through multiple JSON links, I encountered a JSON decode error

I'm facing an issue with a JSON data structure that contains information on various "videos". Each "video" in the JSON includes a link to another JSON file containing "messages". My goal is to loop through the links to the "message" JSON files and in ...

AngularJS - Choose and establish initial values for Editing or Creating New Items

My first project involving AngularJS has left me a bit stuck when it comes to using the select list. I need to either set the default value to the first option for a new entry, or if it's an edit, select the appropriate value. In my form, there are t ...

Navigating through JSON arrays with Node.js

I have been given the task of iterating through a complex JSON file that contains an array of JSON objects. I am finding it difficult to access the array object within the JSON file. Specifically, I need to access the "class-name" object from the JSON f ...

What is the best way to retrieve all objects from this data model?

I am looking to collect all the Model Objects from the data structure provided below. const PRODUCTS = [ { brand: 'Audi', allSeries: { serie: 'A3', allModels: [ { model: ' ...

Inquiries regarding the recently updated Instagram API for integration with Swift 3. Exploring features such as location coordinates (latitude and longitude) and hashtag

I am currently working on updating an older Instagram Swift app that is now in Sandbox mode. In this limited access state, I can only view my own last 20 images. As I navigate through this development phase, uncertainty lingers about how these changes will ...

The WCF response has been deemed invalid because of the presence of an unexpected string combination involving WCF and JSON

I recently set up a WCF service to interact with JSON data, but I'm encountering an issue where all entries are being escaped. [ { "rel":"http:\/\/localhost:3354\/customer\/1\/order", &qu ...

Tips for effectively redirecting traffic to another website

Is there a way to redirect someone from a page URL like example.com/2458/233 to example2.com/2458/233? The first URL contains only a file that redirects to the other domain. Can anybody provide instructions on how to achieve this process? To clarify furt ...

Getting all the dictionary data from a JSON file by searching for a specific value

Looking for a solution to read a json file, extract specific details based on xyz values, and generate an excel report? The following code snippet shows how the process can be implemented: def pop_ws(dictionary,ws): r=1 count=1 for k,v in dictionary. ...

Storing the result of parsing JSON data into a global variable

$(function() { var countFromData = 0; getReminder(); alert(countFromData); }); function getReminder() { $.getJSON("<?=base_url()?>home/leavereminder", {}, function(data) { ...

Storing JSON data for retrieval using Ajax request

Apologies if this is a silly question. I am working with a JSON data file and I want to retrieve it using AJAX. Since the data is already serialized, do I still need an action result to return JSON? Or not? Is it okay to store the JSON file within my pro ...

Navigating JSON Data with ES6 Iteration

Issue Description There are two APIs I am working with. The first one, let's call it API #1, provides JSON data related to forum posts as shown below: [{ "userId": 1, "id": 10, "title": "Tt1", "body": "qBb2" }, { "userId": 2, ...

Learn how to effortlessly update models by integrating AngularJS with Django and Django Rest Framework

Here is a JSON representation of a post based on its ID: http://127.0.0.1:8000/update/1?format=json {"title": "about me", "content": "I like program", "created": "2014-11-29T18:07:18.173Z", "rating": 1, "id": 1} I am attempting to update the rating ...

Arranging JSON data in Python based on string values for sorting

I have a JSON file containing employee information: ll = {"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"James", "lastName":"Bond"}, {"firstName":"Celestial", "lastName":"Syste ...

Encountered an issue while setting up the MongoDB dependencies for the Node.js application

Looking for assistance with this error message encountered during package installation: "unexpected end of JSON input." You can check out the log file here. Here's a glimpse of the log file while installing the MongoDB package: 0 info it worked i ...

Unable to view text in TableView when fetching JSON data from API request

I successfully established a connection to the API using Alamofire and SwiftyJson. After connecting, I received the JSON response: { "contacts": [ { "id": "c200", "name": "Ravi Tamada", "email": " ...

Generating an image from a byte array using Jax-RS

I need help displaying an image when a user accesses the URL http://localhost:8080/app/profile/getImage/5315d33284aec8c202eef15a. The image to be displayed corresponds to the ID 5315d33284aec8c202eef15a. This is my service implementation: @GET @Path( ...