• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

How to check if file does not contain, and skip lines starting with #

I need to modify my loop.

The file rootdircache contains the path to my cfg files, which is read line by line in foreach.

this works great, until i find a "corrupted" cfg file which does not contain 'DATA_DIR'. then i get a null-valued expression.

I need to modify this loop to make it skip file that does not contain 'DATA_DIR' and write it to a errorlog.

I also need this loop to skip lines that start with # (since i dont want any outcommented 'DATA_DIR') and it can also skip empty lines, which i belive is probably just for performance and show :)
$rootdircache = get-content $pidsti 
            foreach ($data in $rootdircache) {
                if (!$data.indexOf('DATA_DIR')) {
                    $hit = $data.indexOf('=')
                    $rootdir = $data.substring($hit+1).trim()
                }

Open in new window

0
frankEQS
Asked:
frankEQS
  • 2
  • 2
  • 2
1 Solution
 
sugunakarCommented:
we can use grep to skip the lines which begin with a #.
example: grep -v "#" filename
use select_match in powershell with
0
 
sugunakarCommented:
we can use grep to skip the lines which begin with a #.
example: grep -v "#" filename
use select_match in powershell with NoMatch argument use "#" .
then it will skip all the lines which start with "#"
0
 
frankEQSAuthor Commented:
im not sure what you mean.

grep is no cmdlet in powershell i think.

Can you place it in an example, or best, place it in my attached code snippet?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chrismerrittCommented:
This should do roughly what you want, let me know if you have any other questions. Won't process lines that have 0 length, or start with #, or don't contain ROOT_DIR text string:

#Vars
$pidsti = "C:\TEMP\file1.txt"
			
$rootdircache = gc $pidsti
foreach ($Line in $rootdircache)
{
	if ($Line.Length -gt 0)
	{
		if ($Line.StartsWith("#") -eq $False)
		{
			if ($Line -match "ROOT_DIR")
			{
				Write-Host -ForeGroundColor "magenta" $Line
			}
			else
			{
				write-host -ForeGroundColor "red" "Line does not contain ROOT_DIR string"
			}
		}
		else
		{
			write-host -ForeGroundColor "red" "Line Starts with #"
		}
	}
	else
	{
		write-host -ForeGroundColor "red" "Line is empty"
	}
}

Open in new window

0
 
frankEQSAuthor Commented:
I solved this before leaving work friday.

 $find_text = Get-Content $pidsti | Select-String "DATA_DIR" -quiet
   
        if (!$find_text)
        {
            write-host "could not find DATA_DIR for: $pidlinje" -foregroundcolor "red"
            add-content temp\errordir.txt "`r`nfinner ikke DATA_DIR i pidfil: '$pidlinje'"
            "$pidsti" | add-content temp\errordir.txt
             continue
        }

 And it looks like your solution would work too, i will test it.



Another thing:

i need to get a path from user:



$input_kopi_sti = read-host "Where do you want to copy (Ex: D:\EQS_kopi or \\server\sti)"


Then i need to make directory and check that line for a correct path:

$kopi_folder = Test-path "$input_kopi_sti"

    If (!$kopi_folder)
{
         mkdir $input_kopi_sti
}

$kopi_folder = Test-path "$input_kopi_sti"


If (!$kopi_folder)
{
         Write-host "Error in path, try again"
         exit
}


but i cant seem to figure out how to check the line from user if it is a correct path. Now the scripts exits if he cant create the path, but if the user ex only writes "folder" then the scripts makes a subfolder of where the scripts is run from.

any ideas?
0
 
chrismerrittCommented:
Honestly this last question is totally out of the scope of your original question and should be asked seperately if you need to get the answer.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now