Windows Vista Forums

How to Skip comment in text - GC file.txt

  1. #1


    techstarts Guest

    How to Skip comment in text - GC file.txt

    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

      My System SpecsSystem Spec

  2.   


  3. #2


    Vadims Podans [MVP] Guest

    Re: How to Skip comment in text - GC file.txt

    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

      My System SpecsSystem Spec

  4. #3


    techstarts Guest

    Re: How to Skip comment in text - GC file.txt

    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 ?


      My System SpecsSystem Spec

  5. #4


    Matthias Tacke Guest

    Re: How to Skip comment in text - GC file.txt

    techstarts@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

      My System SpecsSystem Spec

  6. #5


    Vadims Podans [MVP] Guest

    Re: How to Skip comment in text - GC file.txt

    ^ 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 ?
    >

      My System SpecsSystem Spec

  7. #6


    Umesh Thakur Guest

    Re: How to Skip comment in text - GC file.txt

    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 ?
    > >
    >

      My System SpecsSystem Spec

  8. #7


    OldDog Guest

    Re: How to Skip comment in text - GC file.txt

    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.

      My System SpecsSystem Spec

  9. #8


    Josh Einstein Guest

    Re: How to Skip comment in text - GC file.txt

    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

      My System SpecsSystem Spec

  10. #9


    Matthias Tacke Guest

    Re: How to Skip comment in text - GC file.txt

    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

      My System SpecsSystem Spec


How to Skip comment in text - GC file.txt
Similar Threads
Thread Forum
compare two text files and report differences in a text file format PowerShell
Convert hostname text file to IP text file VB Script
Looping Through A Text File To Find Patterns From Another Text Fil PowerShell
Howto: Add lines of text from a specific point in a text file.. VB Script
How do I read a text file and sort text by fixed positions? PowerShell