1

Background:

I am trying to read a config file and place it in a string variable for further use. For now I've managed to remove the newlines, which was fairly simple. However, I'm having a bit of trouble with removing comments (lines starting with a #).

What I have so far

$var = (Get-Content $HOME/path/to/config.txt -Raw).Replace("`r`n","")

What I've tried

A lot of it is something along the lines of

$var = (Get-Content $HOME/path/to/config.txt -Raw).Replace("#.*?`r`n","").Replace("`r`n","")

( .Replace("(?<=#).*?(?=`r`n)","") , .Replace('^[^#].*?`r`n','') etc)

A lot of the resources I've found have treated how to iteratively read from a file and write back to it or a new one, but what I need is for the result to stay in a variable and for the original file not to be altered in any way (also I'd rather avoid using temp files or even variables if possible). I think there is something fundamental I'm missing about the input to Replace. (Also found this semi-relevant piece when you're using the ConvertFrom-Csv Using Import-CSV in Powershell, ignoring commented lines .)

Sample input:

text;
weird text;
other-sort-of-text;
#commented out possibility;
more-input/with-comment;#comment

Sample output:

text;weird text;other-sort-of-text;more-input/with-comment;

Additional info:

Am going to run this on current builds of Windows 10, locally now I seem to have pwershell version 5.1.14393.693

2
  • 1
    You may use $var -replace "(?m)^#.*`n?", '' to remove the comment lines starting with #. Commented Feb 11, 2017 at 13:10
  • @WiktorStribiżew I got this to do exactly what I wanted when I removed the ^ before the #, effectively leaving me with $var = ((Get-Content $HOME/path/to/config.txt -Raw) -replace "(?m)#.*n?", '').Replace("r`n","") Commented Feb 11, 2017 at 13:25

3 Answers 3

1

Split the string at semicolons, remove element starting with a #, then join the result back to a string.

((Get-Content $HOME/path/to/config.txt -Raw).Replace("`r`n","") -split ';' |
    Where-Object { $_.Trim() -notlike '#*' }) -join ';'
Sign up to request clarification or add additional context in comments.

2 Comments

Personally I like the replace-syntax better, but tested it and this works too (and is an actuall answer and not a comment), so marking accepted =)
@simonra If you feel the other suggestion is a better solution to your problem you're free to post it as an answer of your own and accept that answer (if Wiktor can't be bothered to post it himself).
0

This might be identical to some of the other responses, but here is how I would do it:

$Data = Get-Content $HOME/path/to/config.txt -Raw
(($Data -split(';')).replace("`r`n",'') | Where-Object { $_ -notlike '^#*' }) -join(';')

Anyway you do it, remember that rn needs to be expanded, so it has to be encased in double quotes, unlike the rest of your characters.

Comments

0
#############solution 1 with convertfrom-string####################

#short version
(gc "$HOME/path/to/config.txt" | ? {$_ -notlike "#*"} |  cfs -D ";").P1 -join ";"

#verbose version
(Get-Content "$HOME/path/to/config.txt"  | where {$_ -notlike "#*"} |  ConvertFrom-String -Delimiter ";").P1 -join ";"


#############Solution 2 with convertfrom-csv#######################

(Get-Content "C:\temp\test\config.txt"  | where {$_ -notlike "#*"} |  ConvertFrom-csv -Delimiter ";" -Header "P1").P1 -join ";"


#############Solution 3 with split #######################

(Get-Content "C:\temp\test\config.txt"  | where {$_ -notlike "#*"} |  %{$_.Split(';')[0]}) -join ";"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.