Home > Enterprise >  Powershell remove text after first instance of special character in every line
Powershell remove text after first instance of special character in every line

Time:06-21

I have a multi-line requirement text after pip freeze:

arrow @ file:///opt/conda/conda-bld/arrow_1649166651673/work
astroid @ file:///C:/ci/astroid_1628063282661/work
astropy @ file:///C:/ci/astropy_1650634291321/work
asttokens @ file:///opt/conda/conda-bld/asttokens_1646925590279/work
astunparse>=1.6.3

at every line and I want to delete the rest of the line from @ onwards.

CodePudding user response:

So you want to remove all the characters from each line (string) from the '@' symbol? (including the @?).

Assuming each line comes in as it's own string, you could do something like the below (which returns the string "Astroid"):

$s1 = "astroid @ @file:///C:/ci/astroid_1628063282661/work"
$s1 = $teststring.substring(0,$teststring.IndexOf('@'))
write-host $s1

If you are getting the content as a multi-lined string you will need to chuck a foreach loop in front of it to iterate through each line

CodePudding user response:

Use -replace, the regular-expression-based string replacement operator:

(py -m pip freeze) -replace ' @.*'
  • Related