How to read Name & IP Addresses from file?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a file(.txt) with the following per line format for the contents:
<Name> <IPAddress>
For example, the file contents can be:
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
How to read the IP Addresses into an array efficiently ?
powershell
add a comment |
I have a file(.txt) with the following per line format for the contents:
<Name> <IPAddress>
For example, the file contents can be:
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
How to read the IP Addresses into an array efficiently ?
powershell
1
UseImport-Csv.
– Andrew Morton
Nov 16 '18 at 19:44
@AndrewMorton Thanks
– Monku
Nov 16 '18 at 19:46
1
@Monku Even questions should have a persistent value for follow-up readers to not get down-voted/put on hold. You possibly should take the tour and How to Ask (again).
– LotPings
Nov 16 '18 at 19:52
add a comment |
I have a file(.txt) with the following per line format for the contents:
<Name> <IPAddress>
For example, the file contents can be:
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
How to read the IP Addresses into an array efficiently ?
powershell
I have a file(.txt) with the following per line format for the contents:
<Name> <IPAddress>
For example, the file contents can be:
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
How to read the IP Addresses into an array efficiently ?
powershell
powershell
edited Nov 16 '18 at 19:43
Monku
asked Nov 16 '18 at 19:34
MonkuMonku
78111227
78111227
1
UseImport-Csv.
– Andrew Morton
Nov 16 '18 at 19:44
@AndrewMorton Thanks
– Monku
Nov 16 '18 at 19:46
1
@Monku Even questions should have a persistent value for follow-up readers to not get down-voted/put on hold. You possibly should take the tour and How to Ask (again).
– LotPings
Nov 16 '18 at 19:52
add a comment |
1
UseImport-Csv.
– Andrew Morton
Nov 16 '18 at 19:44
@AndrewMorton Thanks
– Monku
Nov 16 '18 at 19:46
1
@Monku Even questions should have a persistent value for follow-up readers to not get down-voted/put on hold. You possibly should take the tour and How to Ask (again).
– LotPings
Nov 16 '18 at 19:52
1
1
Use
Import-Csv.– Andrew Morton
Nov 16 '18 at 19:44
Use
Import-Csv.– Andrew Morton
Nov 16 '18 at 19:44
@AndrewMorton Thanks
– Monku
Nov 16 '18 at 19:46
@AndrewMorton Thanks
– Monku
Nov 16 '18 at 19:46
1
1
@Monku Even questions should have a persistent value for follow-up readers to not get down-voted/put on hold. You possibly should take the tour and How to Ask (again).
– LotPings
Nov 16 '18 at 19:52
@Monku Even questions should have a persistent value for follow-up readers to not get down-voted/put on hold. You possibly should take the tour and How to Ask (again).
– LotPings
Nov 16 '18 at 19:52
add a comment |
4 Answers
4
active
oldest
votes
What the comments above mean is that you should show your work, what you researched, where you have questions, etc. as you are new to PowerShell, so others can guess what you may know, or don't know.
For example, do you know how to read a file, parse it, use regular expressions, pscustomobject, and more?
$datafile = @'
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
'@
$data = $datafile -split "`n"
$data.count
$obj = @()
$data | % {
$line = $_
$matches = [regex]::Matches($line, "(.*)(bd{1,3}.d{1,3}.d{1,3}.d{1,3}b)")
$obj += [pscustomobject]@{'Name'=$matches.Groups[1].Value.Trim(); 'IP'=$matches.Groups[2].Value;}
}
$obj
add a comment |
this gets the job done.
$ServerAddressArray = @()
$file #Set to file path here
ForEach ($line in Get-Content $file)
{
$Columns = "ServerName", "IP"
$Data = ConvertFrom-csv -inputobject $line -Header $Columns -Delimiter "`t"
$ipAddress = $Data[0].IP.replace(' ', '');
$ipAddress = $ipAddress.replace('`t', '');
$ServerAddressArray+=$ipAddress
}
add a comment |
Your file uses multiple spaces as a delimiter what Import-Csv can't handle.
But there is a work around use Get-Content to replace these spaces with a single character
Emulating the file with a here string
$Content = @"
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
"@ -replace ' +(?=d)','|'
$data = $Content | ConvertFrom-Csv -Header Name,IPAddress -Delimiter '|'
$data
# $data | Out-Gridview
# $data | Export-Csv '.file.csv' -NoTypeInformation
Similar to Korys answer but using a regular expresson with a lookahead to use only spaces followed by a (nonconsuming) digit as a delimiter.
The output to a Gridview and exporting to a csv file are optional/commented out.
Sample output:
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
add a comment |
Or how about using raw, split and join on the content...
(Get-Content -Path D:TempServerIpaList.txt -Raw) -split 's+(?=d)' -join ',' |
ConvertFrom-Csv -Header Name,IPAddress
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344277%2fhow-to-read-name-ip-addresses-from-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
What the comments above mean is that you should show your work, what you researched, where you have questions, etc. as you are new to PowerShell, so others can guess what you may know, or don't know.
For example, do you know how to read a file, parse it, use regular expressions, pscustomobject, and more?
$datafile = @'
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
'@
$data = $datafile -split "`n"
$data.count
$obj = @()
$data | % {
$line = $_
$matches = [regex]::Matches($line, "(.*)(bd{1,3}.d{1,3}.d{1,3}.d{1,3}b)")
$obj += [pscustomobject]@{'Name'=$matches.Groups[1].Value.Trim(); 'IP'=$matches.Groups[2].Value;}
}
$obj
add a comment |
What the comments above mean is that you should show your work, what you researched, where you have questions, etc. as you are new to PowerShell, so others can guess what you may know, or don't know.
For example, do you know how to read a file, parse it, use regular expressions, pscustomobject, and more?
$datafile = @'
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
'@
$data = $datafile -split "`n"
$data.count
$obj = @()
$data | % {
$line = $_
$matches = [regex]::Matches($line, "(.*)(bd{1,3}.d{1,3}.d{1,3}.d{1,3}b)")
$obj += [pscustomobject]@{'Name'=$matches.Groups[1].Value.Trim(); 'IP'=$matches.Groups[2].Value;}
}
$obj
add a comment |
What the comments above mean is that you should show your work, what you researched, where you have questions, etc. as you are new to PowerShell, so others can guess what you may know, or don't know.
For example, do you know how to read a file, parse it, use regular expressions, pscustomobject, and more?
$datafile = @'
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
'@
$data = $datafile -split "`n"
$data.count
$obj = @()
$data | % {
$line = $_
$matches = [regex]::Matches($line, "(.*)(bd{1,3}.d{1,3}.d{1,3}.d{1,3}b)")
$obj += [pscustomobject]@{'Name'=$matches.Groups[1].Value.Trim(); 'IP'=$matches.Groups[2].Value;}
}
$obj
What the comments above mean is that you should show your work, what you researched, where you have questions, etc. as you are new to PowerShell, so others can guess what you may know, or don't know.
For example, do you know how to read a file, parse it, use regular expressions, pscustomobject, and more?
$datafile = @'
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
'@
$data = $datafile -split "`n"
$data.count
$obj = @()
$data | % {
$line = $_
$matches = [regex]::Matches($line, "(.*)(bd{1,3}.d{1,3}.d{1,3}.d{1,3}b)")
$obj += [pscustomobject]@{'Name'=$matches.Groups[1].Value.Trim(); 'IP'=$matches.Groups[2].Value;}
}
$obj
answered Nov 16 '18 at 19:59
Kory GillKory Gill
5,59611026
5,59611026
add a comment |
add a comment |
this gets the job done.
$ServerAddressArray = @()
$file #Set to file path here
ForEach ($line in Get-Content $file)
{
$Columns = "ServerName", "IP"
$Data = ConvertFrom-csv -inputobject $line -Header $Columns -Delimiter "`t"
$ipAddress = $Data[0].IP.replace(' ', '');
$ipAddress = $ipAddress.replace('`t', '');
$ServerAddressArray+=$ipAddress
}
add a comment |
this gets the job done.
$ServerAddressArray = @()
$file #Set to file path here
ForEach ($line in Get-Content $file)
{
$Columns = "ServerName", "IP"
$Data = ConvertFrom-csv -inputobject $line -Header $Columns -Delimiter "`t"
$ipAddress = $Data[0].IP.replace(' ', '');
$ipAddress = $ipAddress.replace('`t', '');
$ServerAddressArray+=$ipAddress
}
add a comment |
this gets the job done.
$ServerAddressArray = @()
$file #Set to file path here
ForEach ($line in Get-Content $file)
{
$Columns = "ServerName", "IP"
$Data = ConvertFrom-csv -inputobject $line -Header $Columns -Delimiter "`t"
$ipAddress = $Data[0].IP.replace(' ', '');
$ipAddress = $ipAddress.replace('`t', '');
$ServerAddressArray+=$ipAddress
}
this gets the job done.
$ServerAddressArray = @()
$file #Set to file path here
ForEach ($line in Get-Content $file)
{
$Columns = "ServerName", "IP"
$Data = ConvertFrom-csv -inputobject $line -Header $Columns -Delimiter "`t"
$ipAddress = $Data[0].IP.replace(' ', '');
$ipAddress = $ipAddress.replace('`t', '');
$ServerAddressArray+=$ipAddress
}
answered Nov 16 '18 at 20:11
MonkuMonku
78111227
78111227
add a comment |
add a comment |
Your file uses multiple spaces as a delimiter what Import-Csv can't handle.
But there is a work around use Get-Content to replace these spaces with a single character
Emulating the file with a here string
$Content = @"
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
"@ -replace ' +(?=d)','|'
$data = $Content | ConvertFrom-Csv -Header Name,IPAddress -Delimiter '|'
$data
# $data | Out-Gridview
# $data | Export-Csv '.file.csv' -NoTypeInformation
Similar to Korys answer but using a regular expresson with a lookahead to use only spaces followed by a (nonconsuming) digit as a delimiter.
The output to a Gridview and exporting to a csv file are optional/commented out.
Sample output:
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
add a comment |
Your file uses multiple spaces as a delimiter what Import-Csv can't handle.
But there is a work around use Get-Content to replace these spaces with a single character
Emulating the file with a here string
$Content = @"
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
"@ -replace ' +(?=d)','|'
$data = $Content | ConvertFrom-Csv -Header Name,IPAddress -Delimiter '|'
$data
# $data | Out-Gridview
# $data | Export-Csv '.file.csv' -NoTypeInformation
Similar to Korys answer but using a regular expresson with a lookahead to use only spaces followed by a (nonconsuming) digit as a delimiter.
The output to a Gridview and exporting to a csv file are optional/commented out.
Sample output:
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
add a comment |
Your file uses multiple spaces as a delimiter what Import-Csv can't handle.
But there is a work around use Get-Content to replace these spaces with a single character
Emulating the file with a here string
$Content = @"
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
"@ -replace ' +(?=d)','|'
$data = $Content | ConvertFrom-Csv -Header Name,IPAddress -Delimiter '|'
$data
# $data | Out-Gridview
# $data | Export-Csv '.file.csv' -NoTypeInformation
Similar to Korys answer but using a regular expresson with a lookahead to use only spaces followed by a (nonconsuming) digit as a delimiter.
The output to a Gridview and exporting to a csv file are optional/commented out.
Sample output:
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
Your file uses multiple spaces as a delimiter what Import-Csv can't handle.
But there is a work around use Get-Content to replace these spaces with a single character
Emulating the file with a here string
$Content = @"
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
"@ -replace ' +(?=d)','|'
$data = $Content | ConvertFrom-Csv -Header Name,IPAddress -Delimiter '|'
$data
# $data | Out-Gridview
# $data | Export-Csv '.file.csv' -NoTypeInformation
Similar to Korys answer but using a regular expresson with a lookahead to use only spaces followed by a (nonconsuming) digit as a delimiter.
The output to a Gridview and exporting to a csv file are optional/commented out.
Sample output:
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
answered Nov 16 '18 at 20:13
LotPingsLotPings
20.6k61633
20.6k61633
add a comment |
add a comment |
Or how about using raw, split and join on the content...
(Get-Content -Path D:TempServerIpaList.txt -Raw) -split 's+(?=d)' -join ',' |
ConvertFrom-Csv -Header Name,IPAddress
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
add a comment |
Or how about using raw, split and join on the content...
(Get-Content -Path D:TempServerIpaList.txt -Raw) -split 's+(?=d)' -join ',' |
ConvertFrom-Csv -Header Name,IPAddress
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
add a comment |
Or how about using raw, split and join on the content...
(Get-Content -Path D:TempServerIpaList.txt -Raw) -split 's+(?=d)' -join ',' |
ConvertFrom-Csv -Header Name,IPAddress
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
Or how about using raw, split and join on the content...
(Get-Content -Path D:TempServerIpaList.txt -Raw) -split 's+(?=d)' -join ',' |
ConvertFrom-Csv -Header Name,IPAddress
Name IPAddress
---- ---------
Tampa Server 10.73.202.91
Hollywood Server 10.73.203.91
Coconut Server 10.73.204.91
Brighton Server 10.73.206.91
Bollywood Server 10.73.207.91
answered Nov 17 '18 at 1:33
postanotepostanote
4,1772411
4,1772411
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344277%2fhow-to-read-name-ip-addresses-from-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use
Import-Csv.– Andrew Morton
Nov 16 '18 at 19:44
@AndrewMorton Thanks
– Monku
Nov 16 '18 at 19:46
1
@Monku Even questions should have a persistent value for follow-up readers to not get down-voted/put on hold. You possibly should take the tour and How to Ask (again).
– LotPings
Nov 16 '18 at 19:52