powershell - Copy last column values -
i have text file (dynamically generated script) shown below:
host1 answer myip1 host6557 answer myip2 host12 question myip3 host253 answer myip4
what want: need copy "myip" columns file. can see space between "host" , "a" not fixed. ex, between host1 , has 7 spaces, between host6557 , has 4 spaces. need copy last column start values, "10." or "192."
how can achieved in powershell?
update
i want data pasted in file in place of 1st column contents shown below: means need replace values of ip1,2,3 these copied values.
if it's done, suppose let's have 4th value above (without credentials) again need copy username, password 3rd value , paste there near 4th value. final expected figure:
update
$hip = get-content "c:\timezone\host - copy.txt" foreach ($ips in $hip) { $ip = $ips -split ',' $sip = $ip[0] if ($iplist -match $sip) { write-host "nothing do" } else { add-content $hip "`n$iplist',username,password'" } }
no error, cant find checks last if loop, because values says "nothing do".
use -split
operator split line @ consecutive spaces, , take last element:
get-content testfile.txt | foreach { ($_.trim() -split '\s+')[-1] }
the .trim()
remove potential trailing spaces in line result in last split element being null.
Comments
Post a Comment