Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A way to ignore certain characters when going get-content?

33 views
Skip to first unread message

akcorr

unread,
Jun 11, 2008, 1:26:02 PM6/11/08
to
I want to ignore any line that begins with #. I'm not sure how to do it and
I just starting working with regex.

Thanks in advance.

Marco Shaw [MVP]

unread,
Jun 11, 2008, 1:41:47 PM6/11/08
to

PS > gc testing.txt
foo
#bar
123
PS >
# Something like this for v1:
PS > gc testing.txt| `
>> %{if($_ -match "^#"){"starts with #"}else{"doesn't start with #"}}
>>
doesn't start with #
starts with #
doesn't start with #
# Now for v2 CTP/CTP2:
PS > gc testing.txt|select-string -notmatch "^#"

foo
123


PS >

-NotMatch does add annoying blank lines to the output...

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com

Marco Shaw [MVP]

unread,
Jun 11, 2008, 2:00:38 PM6/11/08
to
Another way:

PS > gc testing.txt|`
>>%{if($_.startswith("#")){"starts with #"}else{"doesn't start with #"}}
>>

Of course there are even more possibilities...

Arnoud Jansveld

unread,
Jun 11, 2008, 2:28:00 PM6/11/08
to
Why not just:

(Get-Content testing.txt) -notmatch '^#'

Simple and fast!

--
http://www.jansveld.net/powershell

akcorr

unread,
Jun 11, 2008, 2:31:00 PM6/11/08
to
Awesome! Worked great! Thanks!

Marco Shaw [MVP]

unread,
Jun 11, 2008, 2:39:15 PM6/11/08
to
Arnoud Jansveld wrote:
> Why not just:
>
> (Get-Content testing.txt) -notmatch '^#'
>
> Simple and fast!
>

Yup, you're right on that one...

Marco

Karl Prosser[MVP]

unread,
Jun 11, 2008, 3:00:20 PM6/11/08
to
Do a regex a day, lest you lose it away.

Jon

unread,
Jun 11, 2008, 3:48:41 PM6/11/08
to
"akcorr" <akc...@discussions.microsoft.com> wrote in message
news:7D2E4A9C-FC14-4281...@microsoft.com...


In the interests of throwing a spanner into the works, and also because I'm
a believer in the virtues of readable code (which generally means avoiding
regular expressions)....

Get-Content C:\Somefile.txt | Where {-not ($_.StartsWith('#'))}

--
Jon

0 new messages