INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to access row and column in CSV file

How to access row and column in CSV file

(OP)
I have powershell code,

CODE --> powershell

$file = Import-CSV C:\PS\IP_list.csv
Write-Output $file 

And I have output

http://www.mediafire.com/i/?oldomqsvm9w67wl

I am unable to figure out how to access 1st row, 2nd column, or 2nd row, 4th column.

Help!

RE: How to access row and column in CSV file


Once you've imported using Import-CSV, you can use a ForEach loop to cycle through each row.

CODE --> PowerShell

$file = Import-CSV C:\PS\IP_list.csv
ForEach ($record in $file){
Write-Host "Lower IP: $record.lower_ip"
Write-Host "Upper IP: $record.upper_ip"
Write-Host "Group Name: $record.group_name"
Write-Host "Group ID: $record.group_id"
} 

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

RE: How to access row and column in CSV file

(OP)
Hello,

I tried the above code, and got the following output

http://www.mediafire.com/i/?mq33lw3ujqb5hj9


I am unable to grasp how to access 1st row, 2nd column, or 2nd row, 4th column, etc, etc.

Please help.

RE: How to access row and column in CSV file

Try this:

CODE --> PowerShell

$file = Import-CSV C:\PS\IP_list.csv
ForEach ($record in $file){
$lowerIP = $record.lower_ip
$upperIP = $record.upper_ip
$groupName = $record.group_name
$groupID = $record.group_id

Write-Host "Lower IP: $lowerip"
Write-Host "Upper IP: $upperip"
Write-Host "Group Name: $groupname"
Write-Host "Group ID: $groupid"
} 

You can also modify the Import-CSV to target specific lines, such as

CODE --> PowerShell

$file = Import-CSV C:\PS\IP_list.csv | ?{$_.lower_ip -eq "10.251.10.1" -and $_.upper_ip -eq "10.251.10.254"} 

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

RE: How to access row and column in CSV file

(OP)
I think I am not explaning clearly.

I am looking to create a powershell script that does the following
1. Reads an IP address from an MS SQL database
2. Opens the CSV file of the IP addresses
3. Determine which range the IP address falls in
4. Perform the necessary updates based on which range the IP address falls in

In other words, I need to read the entire CSV file.

Can you point me in the right direction?

RE: How to access row and column in CSV file

Your very first line of code reads in the entire file. My suggested code allows you to loop through each row.
I don't have time to try and do all the work for you. There is plenty of resources online for dealing with SQL access as well as looping through arrays.

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

RE: How to access row and column in CSV file

(OP)
Well, I appreciate your help, I didn't mean to upset you sad

RE: How to access row and column in CSV file

(OP)
By the way, I figured out the solution

CODE --> Powershell

$file_line = @()
$file_multi = @()
$file = Get-Content C:\PS\IP_list.csv
foreach ($i in $file){
    $file_line += $i.split("`n")
}

foreach ($j in $file_line){
    $file_multi += ,@($j.split(",")) 
}

Write-output $file_multi[1][3]
Write-output `n
Write-output $file_line 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close