steven
steven

Reputation: 1

How to skip text from file txt when using curl command in powershell

I am using Curl command to check connection of host but in a list i have text like that. So i want to use Curl command to read this list and skip texts such as TEXT1 TEXT2 TEXT3 but still show them into screen. Everyone, please help me how to skip it. Thank you for your helping!

TEXT1
10.0.254.161:9080
TEXT2
10.0.140.11:8866
TEXT3
10.0.110.96 

Upvotes: 0

Views: 94

Answers (1)

HerbM
HerbM

Reputation: 571

There are several ways to do this.

First if you wish to show and pass through some items you can first write each item to verbose, host, information streams, and then at the end filter for the things you wish "to keep" or pass through to the next command.

Writing to verbose frequently makes a lot of sense as you can turn it off and own with your own preferences: $VerbosePreference defaults to: SilentlyContinue

Try this for a start but please understand this is not a very "PowerShell" way to do things, just an answer to your actual question:

For purposes of testing I loaded your sample data into the variable "$curl". You can just pipe the output of your actual "curl" command to the ForEach-Object at the start if you prefer:

$curl -split '\n' |             # I have single string so split
  ForEach-Object -begin {            
    $buffer=@()                 # initialize empty array
  } -process { 
    write-host $_ -fore blue -back white      # show all blue on white
    $buffer += $_                             # add each item to buffer
  } -end {  
    $buffer                                   # hold buffer until end
} | Where-Object { $_ -match '[\d.:]{7,}' }   # filter for just IP:Port

You'll get an obvious (Blue on White) printing of everything, but at the end you'll get just the IP:Port patterns to the standard output, which you can capture to a variable, send to a file, or pipe to another command.

This match is looking for at least 7 Digits, DOTs, or COLONs, (4.4.4.4 would be about a minimal IP), but it isn't very sophisticated and would get fooled by phone numbers (without dashes or spaces):

$_ -match '[\d.:]{7,}' # any 7 digits, periods or colons will match

You can work a lot harder to match an "IP with optional Port number" if you wish, something like:

$_ -match '(\d{1,3}\.){3}(\d{1,3})(:(\d{1,5}))?' # picky match

...but it returns the same (35) items for your sample data.

1to3 digits and a dot, 3 times, then 1to3 digits, optionally followed by a COLON and 1 to 5 digits.

You could wrap it in ^ beginning of line and $ end of line if you wanted to be really stringent.

The above is really ugly for PowerShell but represent a 'minimalist' answer to the question.

Upvotes: 0

Related Questions