• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to Skip comment in text - GC file.txt

T

techstarts

#1
I have below file called servers.txt
--------------------------------------------------------
#Hostnames should be lower caps
Server1
Server2
Server3
#Server4
Server5
----------------------------------------------------------
I would like to put some comment in the text file which powershell
should skip it. is it possible. e.g. I do not want powershell to read
anything which starts with #

Thank you in Advance
 
V

Vadims Podans [MVP]

#2
gc servers | ?{$_ -notmatch "^#"}
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

<techstarts@xxxxxx> rakstija zinojuma
"news:a852e49f-3c62-4dc2-b41c-9c3d0587ffad@xxxxxx"...

> I have below file called servers.txt
> --------------------------------------------------------
> #Hostnames should be lower caps
> Server1
> Server2
> Server3
> #Server4
> Server5
> ----------------------------------------------------------
> I would like to put some comment in the text file which powershell
> should skip it. is it possible. e.g. I do not want powershell to read
> anything which starts with #
>
> Thank you in Advance
 
T

techstarts

#3
On Jun 4, 3:04 pm, "Vadims Podans [MVP]" <vpodans> wrote:

> gc servers | ?{$_ -notmatch "^#"}
> --
> WBR, Vadims Podans
> MVP: PowerShell
> PowerShell blog -www.sysadmins.lv
>
> <techsta...@xxxxxx> rakstija zinojuma
> "news:a852e49f-3c62-4dc2-b41c-9c3d0587ffad@xxxxxx"....
>
>
>

> > I have below file called servers.txt
> > --------------------------------------------------------
> > #Hostnames should be lower caps
> > Server1
> > Server2
> > Server3
> > #Server4
> > Server5
> > ----------------------------------------------------------
> > I would like to put some comment in the text file which powershell
> > should skip it. is it possible. e.g. I do not want powershell to read
> > anything which starts with #
>

> > Thank you in Advance
Again thank you very much. Problem solved.
Do I know what ^ does ?
 
V

Vadims Podans [MVP]

#5
^ means that pattern must match at string begining.
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

<techstarts@xxxxxx> rakstija zinojuma
"news:96452047-f04c-48a8-a198-4a5d8c0900f8@xxxxxx"...

> On Jun 4, 3:04 pm, "Vadims Podans [MVP]" <vpodans> wrote:

>> gc servers | ?{$_ -notmatch "^#"}
>> --
>> WBR, Vadims Podans
>> MVP: PowerShell
>> PowerShell blog -www.sysadmins.lv
>>
>> <techsta...@xxxxxx> rakstija zinojuma
>> "news:a852e49f-3c62-4dc2-b41c-9c3d0587ffad@xxxxxx"...
>>
>>
>>

>> > I have below file called servers.txt
>> > --------------------------------------------------------
>> > #Hostnames should be lower caps
>> > Server1
>> > Server2
>> > Server3
>> > #Server4
>> > Server5
>> > ----------------------------------------------------------
>> > I would like to put some comment in the text file which powershell
>> > should skip it. is it possible. e.g. I do not want powershell to read
>> > anything which starts with #
>>

>> > Thank you in Advance
>
> Again thank you very much. Problem solved.
> Do I know what ^ does ?
>
 
U

Umesh Thakur

#6
alternatively,
gc servers | Where-Object {$_.StartsWith("#") -eq $false}

should work too

--
Umesh

"Old programmers never die. They just terminate and stay resident."



"Vadims Podans [MVP]" wrote:

> ^ means that pattern must match at string begining.
> --
> WBR, Vadims Podans
> MVP: PowerShell
> PowerShell blog - www.sysadmins.lv
>
> <techstarts@xxxxxx> rakstija zinojuma
> "news:96452047-f04c-48a8-a198-4a5d8c0900f8@xxxxxx"...

> > On Jun 4, 3:04 pm, "Vadims Podans [MVP]" <vpodans> wrote:

> >> gc servers | ?{$_ -notmatch "^#"}
> >> --
> >> WBR, Vadims Podans
> >> MVP: PowerShell
> >> PowerShell blog -www.sysadmins.lv
> >>
> >> <techsta...@xxxxxx> rakstija zinojuma
> >> "news:a852e49f-3c62-4dc2-b41c-9c3d0587ffad@xxxxxx"...
> >>
> >>
> >>
> >> > I have below file called servers.txt
> >> > --------------------------------------------------------
> >> > #Hostnames should be lower caps
> >> > Server1
> >> > Server2
> >> > Server3
> >> > #Server4
> >> > Server5
> >> > ----------------------------------------------------------
> >> > I would like to put some comment in the text file which powershell
> >> > should skip it. is it possible. e.g. I do not want powershell to read
> >> > anything which starts with #
> >>
> >> > Thank you in Advance
> >
> > Again thank you very much. Problem solved.
> > Do I know what ^ does ?
> >
>
 
O

OldDog

#7
On Jun 5, 8:36 am, Matthias Tacke <Matth...@xxxxxx> wrote:

> techsta...@xxxxxx wrote:

> > Again thank you very much. Problem solved.
> > Do I know what ^ does ?
>
> You should.
> At least after having read:
>    get-help about_regular_expressions
> or the online version
>    http://go.microsoft.com/fwlink/?LinkId=133231
>
> Regards
> Matthias
^ Matches the beginning "book" -match "^bo"
characters

And it's get-help about_regular_expression ' No s at the end.
 
J

Josh Einstein

#8
I figured I'd mention you could do it in one command with select-string...

select-string '^(?!#)' file.txt

<techstarts@xxxxxx> wrote in message
news:a852e49f-3c62-4dc2-b41c-9c3d0587ffad@xxxxxx

> I have below file called servers.txt
> --------------------------------------------------------
> #Hostnames should be lower caps
> Server1
> Server2
> Server3
> #Server4
> Server5
> ----------------------------------------------------------
> I would like to put some comment in the text file which powershell
> should skip it. is it possible. e.g. I do not want powershell to read
> anything which starts with #
>
> Thank you in Advance
 
M

Matthias Tacke

#9
OldDog wrote:

> On Jun 5, 8:36 am, Matthias Tacke <Matth...@xxxxxx> wrote:

>> get-help about_regular_expressions

> And it's get-help about_regular_expression ' No s at the end.
Well to be unambiguous
get-help about_reg
is sufficient,
but PowerShell V2 from Windows 7 where I copied the whole line from has the
s at the end.

Regards
Matthias