windows - How to remove comments from text file -
my text file contains 1 line comments being "// ". 2 forward slashes , space. these may either take whole line or last part of line. each comment not extend beyond line it's on. no /* */ type comments crossing multiple lines.
in simple terms, comments start "//space" anywhere on line. starting "//space" should removed , trailing spaces on line should removed. leading spaces should stay. blank lines should removed.
sample file:
// comment x = 1 // comment after double slash x = 2 x = 3 // above blank line // comment on record nothing precedes it, should deleted. y = 4 // line leading spaces should kept. z = "//path"; // first double slashes not comment since space missing after "//" // last comment line.
result file (no trailing spaces, keep leading spaces.:
x = 1 x = 2 x = 3 y = 4 z = "//path";
i can remove blank lines using gc file.txt | where-object { $_ -ne ''} > result.txt. i'm having trouble reading beginning part of line "//" comment part.
i tried findstr haven't found how read each line "//" , trim spaces out.
i write script program loop throught file , this, seems there should way accomplish using simple 1 or 2 line powershell or bat file command.
what easiest way (shortest amount of code) remove these comments while keeping uncommented contents of file?
since seem equate "easy" "short", here's simple solution:
gc .\samplefile.txt|%{$_-replace"(.*)(// .*)",'$1'}|?{$_}
if it's important :-)
a bit more verbose version (still using regex):
get-content .\samplefile.txt | where-object { -not ([string]::isnullorempty($_.trim()) -or $_-match"^\s*// ") } |foreach-object { $_ -replace "(.*)(// .*)",'$1' }
that being said, (personally) go more verbose , easier-to-read/maintain solution:
to remove after //
, easiest way find first occurrence of //
string.indexof()
, grab first part string.substring()
:
ps c:\> $commentedstring = "content // comment" ps c:\> $commentindex = $commentedstring.indexof('// ') ps c:\> $commentedstring.substring(0,$commentindex) content
for indented comments can use string.trim()
remove whitespace beginning , end of string:
ps c:\> " // indented comment" -match '^//' true
you can use foreach-object
cmdlet go through every line , apply above:
function remove-comments { param( [string]$path, [string]$outfile ) # read file, remove comments , blank lines $cleanlines = get-content $path |foreach-object { $line = $_ # trim() removes whitespace both ends of string $trimmedline = $line.trim() # check if what's left either nothing or comment if([string]::isnullorempty($trimmedline) -or $trimmedline -match "^// ") { # if so, return nothing (inside foreach-object "return" acts "coninue") return } # see if non-empty line contains comment $commentindex = $line.indexof("// ") if($commentindex -ge 0) { # if so, remove comment $line = $line.substring(0,$commentindex) } # return $line $cleanlines return $line } if($outfile -and (test-path $outfile)){ [system.io.file]::writealllines($outfile, $cleanlines) } else { # no outfile specified, write lines pipeline write-output $cleanlines } }
applied sample:
ps c:\> remove-comments d:\samplefile.txt x = 1 x = 2 x = 3
Comments
Post a Comment