I got a file with two fields delimitered by various length of space in between.
CA CADevserver1
CA CADevserver1
CA CADevserver1
...
I need to get the second field only, this is the powershell command I use:
PS C:\> gc "c:\dropit\\tmp.txt" |foreach {($_ -split '\s+',2)[1]} > temp.txt
The idea come from this link, thanks for that:
https://stackoverflow.com/questions/2503010/extracting-columns-from-text-file-using-powershell
Though the second field content are saved in temp.txt, the lines in the file cannot be processed properly by the following code:
"
@echo off
for /F "tokens=*" %%A in (temp.txt) do call :processline %%A
pause
goto :eof
:processline
set pcpserver=%1
echo "pcpserver name is :"%pcpserver%
goto :eof
:eof
"
The above script suppose to loop each line but it did not, I do not have time to troubleshoot, the workaround is to copy the lines from temp.txt and paste to new file and save the file, use the new file to process, that works. there might be some hidden characters in the temp.txt
CA CADevserver1
CA CADevserver1
CA CADevserver1
...
I need to get the second field only, this is the powershell command I use:
PS C:\> gc "c:\dropit\\tmp.txt" |foreach {($_ -split '\s+',2)[1]} > temp.txt
The idea come from this link, thanks for that:
https://stackoverflow.com/questions/2503010/extracting-columns-from-text-file-using-powershell
"
@echo off
for /F "tokens=*" %%A in (temp.txt) do call :processline %%A
pause
goto :eof
:processline
set pcpserver=%1
echo "pcpserver name is :"%pcpserver%
goto :eof
:eof
"
The above script suppose to loop each line but it did not, I do not have time to troubleshoot, the workaround is to copy the lines from temp.txt and paste to new file and save the file, use the new file to process, that works. there might be some hidden characters in the temp.txt
Comments
Post a Comment