K32FF

                  когда в интернете что-нибудь потерял --
                  мир снова кажется большим..

## ДАННЫЕ В POWERSHELL:
-----------------------

Объект данных: скаляры, массивы и хеши.

1. Скаляр это строка. Все остальные скалярные типы данных служат исключительно для ускорения вычислений на типовом (распространенном) железе.

  PS> $a="alpha"; $a
  alpha

2. Массив это вектор. Одномерный массив это вектор скаляров, Двухмерный массив -- вектор векторов. Более высокие размерности не нужны, читайте у Терри Прачета. Таблица это массив хешей.

  PS> $m=Get-Content abc; $m
  alpha
  bravo
  charlie
  PS> $m.length
  3
  
Shuffle

  PS> @("alpha","bravo","charlie") | Sort-Object {Get-Random}
  alpha
  charlie
  bravo
  
3. Хэш это коллекция скаляров, векторов и хешей. 

  PS> $x=@{}
  PS> $x.a="alpha"
  PS> $x.b="bravo"
  PS> $x.c="charlie"
  PS> $x

  Name                           Value
  ----                           -----
  b                              bravo
  c                              charlie
  a                              alpha

  PS> $x | ConvertTo-Json
  {
    "b": "bravo",
    "c": "charlie",
    "a": "alpha"
  }
  
  PS> $x | ConvertTo-Json | Out-File abc.json
  PS> Get-Content .\abc.json | ConvertFrom-Json

  b     c       a
  -     -       -
  bravo charlie alpha
  
  PS> $x | % {[PSCustomObject]$_} | Format-Table -Property a, b, c
  
  a     b     c
  -     -     -
  alpha bravo charlie
  
## ANNEX A. МАРТОВСКИЙ КЕШ (ИЗ НАКОПАННОГО)
-------------------------------------------

K33

If you really need the format that Format-List provides, then you can use Out-String and Trim() to get rid of empty lines*:

  $p | % { if ($_.status -match "L") { $_ | Format-Table -HideTableHeaders | Out-String | ForEach-Object { $_.Trim() } } }

*) https://stackoverflow.com/questions/47453079
     /remove-blank-lines-from-output

K32

Out-String -Stream to forcibly textify those pesky objects.

  PS> function nl{$input | Out-String -Stream | Select-String '.*' | Select-Object LineNumber, Line}
  PS> cat .\food.txt | nl

This will do it and will work with any command that produces output:

  PS> $i=1; dir | % {"$($i) `t $_"; $i++}

  https://superuser.com/questions/1182291
    /how-do-i-get-line-numbers-with-powershell

K31

Shuffle with Powershell

  1..10 | Sort-Object {Get-Random} | Out-File -FilePath pre-shu

For each object that is passed into Sort-Object through the pipeline the scriptblock is called. And since Get-Random evaluates to a rather large random number -- the objects in the array are sorted based on all of those random integers*1.

*1) https://ilovepowershell.com/2015/01/24
    /easiest-way-shuffle-array-powershell/

K31

## Old Dog, New Tricks II
-------------------------

PS> Get-Content .\sample.csv
"No", "English", "Estonian", "Russian"
"1", "Black", "Must", "Черный"
"2", "White", "Valge", "Белый"
"3", "Red", "Punane", "Красный"

PS> import-csv .\sample.csv | ConvertTo-json
[
  {
    "No": "1",
    "English": "Black",
    "Estonian": "Must",
    "Russian": "Черный"
  },
  {
    "No": "2",
    "English": "White",
    "Estonian": "Valge",
    "Russian": "Белый"
  },
  {
    "No": "3",
    "English": "Red",
    "Estonian": "Punane",
    "Russian": "Красный"
  }
]

PS> $sample = Get-Content .\sample.json | ConvertFrom-Json
           
PS> $sample | % {[PSCustomObject]$_} | `
    Format-Table -Property No, Estonian, English, Russian

No Estonian English Russian
-- -------- ------- -------
1  Must     Black   Черный
2  Valge    White   Белый
3  Punane   Red     Красный

Exporting variables and functions with PS> . ./myAPI.ps1

PS> Import-Csv .\sample.csv | ConvertTo-Json | `
    % { $_ -replace '",','";' `
           -replace '\[', '$sample=@(' `
           -replace '\]', ')' `
           -replace '\{', '@{' `
           -replace ':','='} > sample.ps1
           
PS> . ./sample.ps1

K31

Old Dog New Tricks:

0. WSL distro location

  PS> wsl
  $ cd ~
  $ touch bull.sh

  Window 10 -> explorer  search bull.sh
  C:\Users\alien\AppData\Local\Packages
  \TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState
  \rootfs\home\alien

1. UTF8 with Perl
 
  $ sudo apt install libutf8-all-perl
  ..
  $ echo "эх" | perl -Mutf8::all -pe 's/([а-я])/\1\1/g'
    ээхх

2. Well, the .gvfs is a filesystem, so you can umount it! *1
   # chmod 400 /run/user/1000/gvfs
   chmod: cannot access '/run/user/1000/gvfs': Permission denied
   # umount /run/user/1000/gvfs
   # chmod 400 /run/user/1000/gvfs
   # ls -lad /run/user/1000/gvfs
   dr-------- 2 opilane opilane 40 Feb 24 03:37 /run/user/1000/gvfs

3. Use sudo -i to minimize the risk of poisoning user folder
   (/home/user) with root:root files.
   
4. Achieving logical AND with Look-aheads*2

     (?=.*word1)(?=.*word2)(?=.*word3)

   $ grep -PrHni '(?=.*окна)(?=.*очень)' *
   Program.cs:189: Title = "Очень длинное название окна",
   
5. grep for <pattern> only in certain file types

  $grep --include='*.c' <pattern> *
  
6. call make -p in empty folder to print make implicit rules.

7. the filename pattern ending in tilde is produced automatically by the mv , cp , emacs. So rm *~ just removes such backup files*3.
   
*1) https://susetips.wordpress.com/2009/03/13
      /how-to-remove-corrupted-gvfs-directory/

*2) https://www.ocpsoft.org/tutorials/regular-expressions/and-in-regex/

*3) https://stackoverflow.com/questions/4572809
      /what-does-the-bash-command-rm-do

K31

Never write it in 'C' if you can do it in 'awk';
Never do it in 'awk' if 'sed' can handle it;
Never use 'sed' when 'tr' can do the job;
Never invoke 'tr' when 'cat' is sufficient;
Avoid using 'cat' whenever possible.
         
         --Taylor's Laws of Programming 

..