Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through a variable line by line

Tags:

powershell

I have a small script that outputs text into a variable. I need to go through it line by one in order to parse it. I could do this by outputting the variable to a text file and then reading it using Get-Content but that seems a bit redundant.

My script connects to a Fortigate unit and runs a certain query. It's response is what I'm looking to parse.

New-SshSession 10.0.0.138 -Port 65432 -Credential (Get-Credential) -AcceptKey
$command = 'config router policy
show'
$result = Invoke-SSHCommand -Index 0 -Command $command
like image 281
JustAGuy Avatar asked Jul 22 '15 12:07

JustAGuy


People also ask

How do I loop through a line in bash?

Simply setting the IFS variable to a new line works for the output of a command but not when processing a variable that contains new lines. As you can see, echoing the variable or iterating over the cat command prints each of the lines one by one correctly.

How read file line by line in shell script and store each line in a variable?

We use the read command with -r argument to read the contents without escaping the backslash character. We read the content of each line and store that in the variable line and inside the while loop we echo with a formatted -e argument to use special characters like \n and print the contents of the line variable.

How do I read a text file line by line in Unix?

Syntax: Read file line by line on a Bash Unix & Linux shell file. The -r option passed to read command prevents backslash escapes from being interpreted. Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed. while IFS= read -r line; do COMMAND_on $line; done < input.


1 Answers

ForEach ($line in $($result -split "`r`n"))
{
    Write-host $Line
}
like image 168
Scott Thompson Avatar answered Sep 24 '22 10:09

Scott Thompson