import CSV File starting from a specific header value [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
Powershell Import-CSV how to skip until particular line based on string?
1 answer
I am trying to import a CSV file. It has some random data on top and I cannot skip the lines as I am not sure where the random data ends. There will be 2 empty lines and after that, there are headers and the desired data.
Lets say the random data looks like this
"AD Group","USER ID","Property",
"NTDOMAINDoctors","sattar""Enabled"
"NTDOMAINDoctors","shayla""Disabled"
"NTDOMAInNurses","mjane","Enabled"
"AD Group","GivenName", "ObjectClass","SURname","Name" ...morecolumns
"NTDOMAINUsers","user1","user","User","1"
"NTDOMAINUsers2","User1","User","User1_1","2"
How can I import the data that is under the headers (GivenName, SURname, Name). Also I just want to select some columns?
powershell
marked as duplicate by JosefZ, Ansgar Wiechers
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 22:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Powershell Import-CSV how to skip until particular line based on string?
1 answer
I am trying to import a CSV file. It has some random data on top and I cannot skip the lines as I am not sure where the random data ends. There will be 2 empty lines and after that, there are headers and the desired data.
Lets say the random data looks like this
"AD Group","USER ID","Property",
"NTDOMAINDoctors","sattar""Enabled"
"NTDOMAINDoctors","shayla""Disabled"
"NTDOMAInNurses","mjane","Enabled"
"AD Group","GivenName", "ObjectClass","SURname","Name" ...morecolumns
"NTDOMAINUsers","user1","user","User","1"
"NTDOMAINUsers2","User1","User","User1_1","2"
How can I import the data that is under the headers (GivenName, SURname, Name). Also I just want to select some columns?
powershell
marked as duplicate by JosefZ, Ansgar Wiechers
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 22:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Do you know what the header line will look like for sure, or can you be confident that no lines before the header line will start with 'Name '?
– TheMadTechnician
Jan 3 at 20:48
This file will have multiple headers 1= A, B, C and under that it will have another set of header 2=Name, Phone, City and Name will always be a header
– Faiq
Jan 3 at 20:55
So, you'd need to detect data start as the "Name Phone..." header, and data end by the "A B C" header. And manually parse each line. Or, use a temp file, where you save filtered data (using data start/end detection by headers), and then import the temp file.
– Max
Jan 3 at 21:13
Your sample data is delimited by spaces, but your comment states a comma?
– LotPings
Jan 3 at 21:26
If I open the file in notepad, it shows the data like "A", "B", "C" "w", "DD", "q" "u", "o", "p" "A","Name" ,"Phone_Number", "City", "State" "2","Nick", "800-524-8763", "NewYork" , "NY" "0","Mike", "112-223-2222" ,"Richmond" , "VA" The desired row has more columns but I wan to get only some columns. Also, I want to skip the first column from the selection as well.
– Faiq
Jan 4 at 14:30
add a comment |
This question already has an answer here:
Powershell Import-CSV how to skip until particular line based on string?
1 answer
I am trying to import a CSV file. It has some random data on top and I cannot skip the lines as I am not sure where the random data ends. There will be 2 empty lines and after that, there are headers and the desired data.
Lets say the random data looks like this
"AD Group","USER ID","Property",
"NTDOMAINDoctors","sattar""Enabled"
"NTDOMAINDoctors","shayla""Disabled"
"NTDOMAInNurses","mjane","Enabled"
"AD Group","GivenName", "ObjectClass","SURname","Name" ...morecolumns
"NTDOMAINUsers","user1","user","User","1"
"NTDOMAINUsers2","User1","User","User1_1","2"
How can I import the data that is under the headers (GivenName, SURname, Name). Also I just want to select some columns?
powershell
This question already has an answer here:
Powershell Import-CSV how to skip until particular line based on string?
1 answer
I am trying to import a CSV file. It has some random data on top and I cannot skip the lines as I am not sure where the random data ends. There will be 2 empty lines and after that, there are headers and the desired data.
Lets say the random data looks like this
"AD Group","USER ID","Property",
"NTDOMAINDoctors","sattar""Enabled"
"NTDOMAINDoctors","shayla""Disabled"
"NTDOMAInNurses","mjane","Enabled"
"AD Group","GivenName", "ObjectClass","SURname","Name" ...morecolumns
"NTDOMAINUsers","user1","user","User","1"
"NTDOMAINUsers2","User1","User","User1_1","2"
How can I import the data that is under the headers (GivenName, SURname, Name). Also I just want to select some columns?
This question already has an answer here:
Powershell Import-CSV how to skip until particular line based on string?
1 answer
powershell
powershell
edited Jan 4 at 16:15
Faiq
asked Jan 3 at 20:26
FaiqFaiq
11
11
marked as duplicate by JosefZ, Ansgar Wiechers
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 22:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by JosefZ, Ansgar Wiechers
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 22:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Do you know what the header line will look like for sure, or can you be confident that no lines before the header line will start with 'Name '?
– TheMadTechnician
Jan 3 at 20:48
This file will have multiple headers 1= A, B, C and under that it will have another set of header 2=Name, Phone, City and Name will always be a header
– Faiq
Jan 3 at 20:55
So, you'd need to detect data start as the "Name Phone..." header, and data end by the "A B C" header. And manually parse each line. Or, use a temp file, where you save filtered data (using data start/end detection by headers), and then import the temp file.
– Max
Jan 3 at 21:13
Your sample data is delimited by spaces, but your comment states a comma?
– LotPings
Jan 3 at 21:26
If I open the file in notepad, it shows the data like "A", "B", "C" "w", "DD", "q" "u", "o", "p" "A","Name" ,"Phone_Number", "City", "State" "2","Nick", "800-524-8763", "NewYork" , "NY" "0","Mike", "112-223-2222" ,"Richmond" , "VA" The desired row has more columns but I wan to get only some columns. Also, I want to skip the first column from the selection as well.
– Faiq
Jan 4 at 14:30
add a comment |
1
Do you know what the header line will look like for sure, or can you be confident that no lines before the header line will start with 'Name '?
– TheMadTechnician
Jan 3 at 20:48
This file will have multiple headers 1= A, B, C and under that it will have another set of header 2=Name, Phone, City and Name will always be a header
– Faiq
Jan 3 at 20:55
So, you'd need to detect data start as the "Name Phone..." header, and data end by the "A B C" header. And manually parse each line. Or, use a temp file, where you save filtered data (using data start/end detection by headers), and then import the temp file.
– Max
Jan 3 at 21:13
Your sample data is delimited by spaces, but your comment states a comma?
– LotPings
Jan 3 at 21:26
If I open the file in notepad, it shows the data like "A", "B", "C" "w", "DD", "q" "u", "o", "p" "A","Name" ,"Phone_Number", "City", "State" "2","Nick", "800-524-8763", "NewYork" , "NY" "0","Mike", "112-223-2222" ,"Richmond" , "VA" The desired row has more columns but I wan to get only some columns. Also, I want to skip the first column from the selection as well.
– Faiq
Jan 4 at 14:30
1
1
Do you know what the header line will look like for sure, or can you be confident that no lines before the header line will start with 'Name '?
– TheMadTechnician
Jan 3 at 20:48
Do you know what the header line will look like for sure, or can you be confident that no lines before the header line will start with 'Name '?
– TheMadTechnician
Jan 3 at 20:48
This file will have multiple headers 1= A, B, C and under that it will have another set of header 2=Name, Phone, City and Name will always be a header
– Faiq
Jan 3 at 20:55
This file will have multiple headers 1= A, B, C and under that it will have another set of header 2=Name, Phone, City and Name will always be a header
– Faiq
Jan 3 at 20:55
So, you'd need to detect data start as the "Name Phone..." header, and data end by the "A B C" header. And manually parse each line. Or, use a temp file, where you save filtered data (using data start/end detection by headers), and then import the temp file.
– Max
Jan 3 at 21:13
So, you'd need to detect data start as the "Name Phone..." header, and data end by the "A B C" header. And manually parse each line. Or, use a temp file, where you save filtered data (using data start/end detection by headers), and then import the temp file.
– Max
Jan 3 at 21:13
Your sample data is delimited by spaces, but your comment states a comma?
– LotPings
Jan 3 at 21:26
Your sample data is delimited by spaces, but your comment states a comma?
– LotPings
Jan 3 at 21:26
If I open the file in notepad, it shows the data like "A", "B", "C" "w", "DD", "q" "u", "o", "p" "A","Name" ,"Phone_Number", "City", "State" "2","Nick", "800-524-8763", "NewYork" , "NY" "0","Mike", "112-223-2222" ,"Richmond" , "VA" The desired row has more columns but I wan to get only some columns. Also, I want to skip the first column from the selection as well.
– Faiq
Jan 4 at 14:30
If I open the file in notepad, it shows the data like "A", "B", "C" "w", "DD", "q" "u", "o", "p" "A","Name" ,"Phone_Number", "City", "State" "2","Nick", "800-524-8763", "NewYork" , "NY" "0","Mike", "112-223-2222" ,"Richmond" , "VA" The desired row has more columns but I wan to get only some columns. Also, I want to skip the first column from the selection as well.
– Faiq
Jan 4 at 14:30
add a comment |
3 Answers
3
active
oldest
votes
Provided the header line is fixed and can be used as a separator,
I'd use a RegEx with a positive lookahead :
$Header = "Name,Phone_Number,City"
$CsvData = (Get-Content '.inputfile.csv' -raw) -Replace "(?s).*?(?=$Header)" | ConvertFrom-Csv
$CsvData
Sample output based on above data (assuming a comma
separated file) :
Name Phone_Number City
---- ------------ ----
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
add a comment |
## Read the file into an array, line-terminated
$f = [System.Io.File]::GetAllLines('path-to-csv')
## Stage the array to receive the filtered results
$clean = @()
## Loop through each entry in the file
for ($i = 0; $i -lt $f.count; $i++) {
## When the CSV header is found, filter the results and exit
if ($f[$i] -eq "Name Phone_Number City") {
$clean = $f[($i..($f.count - 1))]
break
}
}
## Turn the string array into and object that can be used in PowerShellese
$clean = $clean | convertfrom-csv
add a comment |
Something like this is a pattern you can test with using here strings
as input rather than a large file. Very similar to answer from thepip3r. Also, it appears your input is not using Comma
as delimiter, so specified Space
.
$csvtext = @"
A B C
w DD q
u o p
p u o
Name Phone_Number City
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
"@
# replace with Get-Content of your file...
# $data = Get-Content ...
$data = $csvtext -split "`r`n"
$i = 0;
foreach($line in $data)
{
if ($line.StartsWith("Name Phone_Number City")) { break; }
$i++
}
$subdata = $data[$i..($data.Count-1)]
$csv = $subdata | ConvertFrom-Csv -Delimiter ' '
$csv
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Provided the header line is fixed and can be used as a separator,
I'd use a RegEx with a positive lookahead :
$Header = "Name,Phone_Number,City"
$CsvData = (Get-Content '.inputfile.csv' -raw) -Replace "(?s).*?(?=$Header)" | ConvertFrom-Csv
$CsvData
Sample output based on above data (assuming a comma
separated file) :
Name Phone_Number City
---- ------------ ----
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
add a comment |
Provided the header line is fixed and can be used as a separator,
I'd use a RegEx with a positive lookahead :
$Header = "Name,Phone_Number,City"
$CsvData = (Get-Content '.inputfile.csv' -raw) -Replace "(?s).*?(?=$Header)" | ConvertFrom-Csv
$CsvData
Sample output based on above data (assuming a comma
separated file) :
Name Phone_Number City
---- ------------ ----
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
add a comment |
Provided the header line is fixed and can be used as a separator,
I'd use a RegEx with a positive lookahead :
$Header = "Name,Phone_Number,City"
$CsvData = (Get-Content '.inputfile.csv' -raw) -Replace "(?s).*?(?=$Header)" | ConvertFrom-Csv
$CsvData
Sample output based on above data (assuming a comma
separated file) :
Name Phone_Number City
---- ------------ ----
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
Provided the header line is fixed and can be used as a separator,
I'd use a RegEx with a positive lookahead :
$Header = "Name,Phone_Number,City"
$CsvData = (Get-Content '.inputfile.csv' -raw) -Replace "(?s).*?(?=$Header)" | ConvertFrom-Csv
$CsvData
Sample output based on above data (assuming a comma
separated file) :
Name Phone_Number City
---- ------------ ----
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
edited Jan 3 at 21:49
answered Jan 3 at 21:40
LotPingsLotPings
20.3k61633
20.3k61633
add a comment |
add a comment |
## Read the file into an array, line-terminated
$f = [System.Io.File]::GetAllLines('path-to-csv')
## Stage the array to receive the filtered results
$clean = @()
## Loop through each entry in the file
for ($i = 0; $i -lt $f.count; $i++) {
## When the CSV header is found, filter the results and exit
if ($f[$i] -eq "Name Phone_Number City") {
$clean = $f[($i..($f.count - 1))]
break
}
}
## Turn the string array into and object that can be used in PowerShellese
$clean = $clean | convertfrom-csv
add a comment |
## Read the file into an array, line-terminated
$f = [System.Io.File]::GetAllLines('path-to-csv')
## Stage the array to receive the filtered results
$clean = @()
## Loop through each entry in the file
for ($i = 0; $i -lt $f.count; $i++) {
## When the CSV header is found, filter the results and exit
if ($f[$i] -eq "Name Phone_Number City") {
$clean = $f[($i..($f.count - 1))]
break
}
}
## Turn the string array into and object that can be used in PowerShellese
$clean = $clean | convertfrom-csv
add a comment |
## Read the file into an array, line-terminated
$f = [System.Io.File]::GetAllLines('path-to-csv')
## Stage the array to receive the filtered results
$clean = @()
## Loop through each entry in the file
for ($i = 0; $i -lt $f.count; $i++) {
## When the CSV header is found, filter the results and exit
if ($f[$i] -eq "Name Phone_Number City") {
$clean = $f[($i..($f.count - 1))]
break
}
}
## Turn the string array into and object that can be used in PowerShellese
$clean = $clean | convertfrom-csv
## Read the file into an array, line-terminated
$f = [System.Io.File]::GetAllLines('path-to-csv')
## Stage the array to receive the filtered results
$clean = @()
## Loop through each entry in the file
for ($i = 0; $i -lt $f.count; $i++) {
## When the CSV header is found, filter the results and exit
if ($f[$i] -eq "Name Phone_Number City") {
$clean = $f[($i..($f.count - 1))]
break
}
}
## Turn the string array into and object that can be used in PowerShellese
$clean = $clean | convertfrom-csv
answered Jan 3 at 21:16
thepip3rthepip3r
1,50042328
1,50042328
add a comment |
add a comment |
Something like this is a pattern you can test with using here strings
as input rather than a large file. Very similar to answer from thepip3r. Also, it appears your input is not using Comma
as delimiter, so specified Space
.
$csvtext = @"
A B C
w DD q
u o p
p u o
Name Phone_Number City
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
"@
# replace with Get-Content of your file...
# $data = Get-Content ...
$data = $csvtext -split "`r`n"
$i = 0;
foreach($line in $data)
{
if ($line.StartsWith("Name Phone_Number City")) { break; }
$i++
}
$subdata = $data[$i..($data.Count-1)]
$csv = $subdata | ConvertFrom-Csv -Delimiter ' '
$csv
add a comment |
Something like this is a pattern you can test with using here strings
as input rather than a large file. Very similar to answer from thepip3r. Also, it appears your input is not using Comma
as delimiter, so specified Space
.
$csvtext = @"
A B C
w DD q
u o p
p u o
Name Phone_Number City
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
"@
# replace with Get-Content of your file...
# $data = Get-Content ...
$data = $csvtext -split "`r`n"
$i = 0;
foreach($line in $data)
{
if ($line.StartsWith("Name Phone_Number City")) { break; }
$i++
}
$subdata = $data[$i..($data.Count-1)]
$csv = $subdata | ConvertFrom-Csv -Delimiter ' '
$csv
add a comment |
Something like this is a pattern you can test with using here strings
as input rather than a large file. Very similar to answer from thepip3r. Also, it appears your input is not using Comma
as delimiter, so specified Space
.
$csvtext = @"
A B C
w DD q
u o p
p u o
Name Phone_Number City
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
"@
# replace with Get-Content of your file...
# $data = Get-Content ...
$data = $csvtext -split "`r`n"
$i = 0;
foreach($line in $data)
{
if ($line.StartsWith("Name Phone_Number City")) { break; }
$i++
}
$subdata = $data[$i..($data.Count-1)]
$csv = $subdata | ConvertFrom-Csv -Delimiter ' '
$csv
Something like this is a pattern you can test with using here strings
as input rather than a large file. Very similar to answer from thepip3r. Also, it appears your input is not using Comma
as delimiter, so specified Space
.
$csvtext = @"
A B C
w DD q
u o p
p u o
Name Phone_Number City
Nick 800-524-8763 NewYork
Mike 112-223-2222 Richmond
"@
# replace with Get-Content of your file...
# $data = Get-Content ...
$data = $csvtext -split "`r`n"
$i = 0;
foreach($line in $data)
{
if ($line.StartsWith("Name Phone_Number City")) { break; }
$i++
}
$subdata = $data[$i..($data.Count-1)]
$csv = $subdata | ConvertFrom-Csv -Delimiter ' '
$csv
answered Jan 3 at 21:23
Kory GillKory Gill
5,58611026
5,58611026
add a comment |
add a comment |
1
Do you know what the header line will look like for sure, or can you be confident that no lines before the header line will start with 'Name '?
– TheMadTechnician
Jan 3 at 20:48
This file will have multiple headers 1= A, B, C and under that it will have another set of header 2=Name, Phone, City and Name will always be a header
– Faiq
Jan 3 at 20:55
So, you'd need to detect data start as the "Name Phone..." header, and data end by the "A B C" header. And manually parse each line. Or, use a temp file, where you save filtered data (using data start/end detection by headers), and then import the temp file.
– Max
Jan 3 at 21:13
Your sample data is delimited by spaces, but your comment states a comma?
– LotPings
Jan 3 at 21:26
If I open the file in notepad, it shows the data like "A", "B", "C" "w", "DD", "q" "u", "o", "p" "A","Name" ,"Phone_Number", "City", "State" "2","Nick", "800-524-8763", "NewYork" , "NY" "0","Mike", "112-223-2222" ,"Richmond" , "VA" The desired row has more columns but I wan to get only some columns. Also, I want to skip the first column from the selection as well.
– Faiq
Jan 4 at 14:30