1
votes

I have a notepad ++ with a column to modify like the column mode editing in notepad ++ by doing alt+shift+arrow keys. (example by putting B instead of A) :

enter image description here

enter image description here

I want to automate this by a powershell script without doing notepad ++ column mode editing. For example, I want by entering a letter in my script that the whole column is changed

$lettre = [string]
$array = "B","D","M","N","P","Z"
while ($true){

    $letter = Read-Host "What medicalization wanted to put you ? B, D, M, N, P, Z ? or t to finish"
    if ($lettre -eq "B" ){
    echo "Medicalization B will be inserted"
    }
    if ($lettre -eq "D"){
    echo "Medicalization D will be inserted"
    }
    if ($lettre -eq "M"){
    echo "Medicalization M will be inserted"
    }
    if ($lettre -eq "N"){
    echo "Medicalization N will be inserted"
    }
    if ($lettre -eq "P"){
    echo "Medicalization N will be inserted"
    }
    if ($lettre -eq "Z"){
    echo "Medicalization Z will be inserted"
    }
    if ($lettre -eq "t"){
    echo "Finished"
    break
    }
    }

I was thinking of going based on row and column or using column mode editing when entering commands on powershell. Is it possible to do this on powershell? If not, which programming language should I use and how?

1
We haven't heard from you.. Did my answer solve your problem? If so, please consider accepting it by clicking the icon on the left. This will help others with a similar question finding it more easily and helps motivating others to answer any questions you may have in the future. - Theo

1 Answers

0
votes

Although I'm not quite sure what you want to achieve here, but you could use switch instead of all those if conditions.

BTW, you ask for variable $letter, but in your if's you use $lettre

Suppose you have a text file with these lines:

Entrance:          A
Departure:         A
Consultation:      A
Meeting:           A
Transfer:          A
Permission:        A

Then you can do

# read the text from the file as string array
$txt = Get-Content -Path 'D:\Text\inputFile.txt'
# create a boolean flag to exit the endless while loop
$stopLoop = $false   

while(!$stopLoop) {
    switch -Regex (Read-Host "What medicalization wanted to put you ? B, D, M, N, P, Z ? or T to finish") {
        '^T'        { $stopLoop = $true; break }  # set the flag to exit the while loop and break out of the switch
        '^[BDMNPZ]' { 
            # get the first (or only) character from the user input and convert to uppercase
            $letter = $_[0].ToString().ToUpper()  
            # loop through each line of the input text and replace the last character
            $newtext = $txt | ForEach-Object {$_ -replace '[a-z]$', $letter}
            # output on screen
            $newtext
            # or write to new text file if you like
            # $newtext | Set-Content -Path 'D:\Text\newText.txt'
        }
        default { Write-Host "Bad input.. Only B, D, M, N, P, Z or T allowed" -ForegroundColor Red }
    }
    Write-Host
}

Regex details:

  • the ^ means the input string should start with the character(s) that follow.
  • ^[BDMNPZ] means the input string should start with any on the characters inside the square brackets
  • in the -replace, [a-z]$ means to target only the last character of the line