Hi there,<\/p>\n
I’m requesting help on powershell script to clean up a input file and write to an output file.<\/p>\n
Contents of the input.txt<\/strong> file are…<\/p>\n abcd [“efgh”]<\/p>\n xyz<\/p>\n Expected OUTPUT.txt file<\/p>\n abcd [“efgh”]<\/p>\n xyz<\/p>\n Wanted to suggest the criteria to clean up the file is Looking forward and appreciate your help towards script.<\/p>\n Thanks.<\/p>\n The below is missing few lines and im not sure where im missing.<\/p>\n
\ncdef
\n[“abcd”]
\n[\"ab<\/strong>
\ncd\"]<\/strong><\/p>\n
\ncdef
\n[“abcd”]
\n[“abcd”]<\/strong><\/p>\n
\nIf a line contains start square bracket but does not end with square bracket, then concatenate the next line to current line.<\/p>\n$file = \"c:\\junk\\test.txt\"\n $BufferedLine = $null\n \n Get-Content $file |\n ForEach-Object {\n if ($_ -match \"^[^[].*[^]]$\") {\n # line doesn't begin with \"[\" or end with \"]\"\n $_ # -- line is okay, just return it\n }\n elseif ($_ -match \"^\\[.*\\]$\") {\n # line begins with \"[\" and ends with \"]\"\n $_ # -- line is okay, just return it\n }\n elseif ($_ -match \"^\\[.*[^]]$\") {\n # line begins with \"[\" but doesn't end with \"]\"\n $BufferedLine = $_ # remember line as beginning\n }\n elseif ($_ -match \"^[^[].*\\]$\") {\n # line doesn't begin with \"[\" but ends with \"]\"\n if ($BufferedLine) { # AND there's a preceeding line awaiting closure\n $BufferedLine += $_ # concatenate with contents of previous line\n $BufferedLine # return completed line\n $BufferedLine = $null # and forget the value\n }\n \n }\n }\n if ($BufferedLine) {\n $BufferedLine # return the last line of necessary\n }\n<\/code><\/pre>\n