-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample.nu
3215 lines (1255 loc) · 43.8 KB
/
example.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[[a b]; [1 2] [1 4] [2 6] [2 4]]
| into df
| group-by a
| agg [
(col b | min | as "b_min")
(col b | max | as "b_max")
(col b | sum | as "b_sum")
]
[[a b]; [1 2] [1 4] [2 6] [2 4]]
| into lazy
| group-by a
| agg [
(col b | min | as "b_min")
(col b | max | as "b_max")
(col b | sum | as "b_sum")
]
| collect
alias ll = ls -l
echo [[status]; [UP] [UP]] | all status == UP
echo [2 4 6 8] | all ($it mod 2) == 0
[false false false] | into df | all-false
let s = ([5 6 2 10] | into df);
let res = ($s > 9);
$res | all-false
[true true true] | into df | all-true
let s = ([5 6 2 8] | into df);
let res = ($s > 9);
$res | all-true
(field a) > 1 | and ((field a) < 10) | into nu
open db.sqlite
| from table table_1
| select a
| where ((field a) > 1)
| and ((field b) == 1)
| describe
open db.sqlite
| from table table_1
| select a
| where ((field a) > 1 | and ((field a) < 10))
| and ((field b) == 1)
| describe
ansi green
ansi reset
echo [(ansi rb) Hello " " (ansi gb) Nu " " (ansi pb) World (ansi reset)] | str collect
echo [(ansi -e '3;93;41m') Hello (ansi reset) " " (ansi gb) Nu " " (ansi pb) World (ansi reset)] | str collect
$"(ansi -e { fg: '#0000ff' bg: '#ff0000' attr: b })Hello Nu World(ansi reset)"
echo 'Hello, Nushell! This is a gradient.' | ansi gradient --fgstart 0x40c9ff --fgend 0xe81cff
echo 'Hello, Nushell! This is a gradient.' | ansi gradient --fgstart 0x40c9ff --fgend 0xe81cff --bgstart 0xe81cff --bgend 0x40c9ff
echo 'Hello, Nushell! This is a gradient.' | ansi gradient --fgstart 0x40c9ff
echo 'Hello, Nushell! This is a gradient.' | ansi gradient --fgend 0xe81cff
echo [ (ansi green) (ansi cursor_on) "hello" ] | str collect | ansi strip
echo [[status]; [UP] [DOWN] [UP]] | any status == DOWN
echo [2 4 1 6 8] | any ($it mod 2) == 1
let a = ([[a b]; [1 2] [3 4]] | into df);
$a | append $a
let a = ([[a b]; [1 2] [3 4]] | into df);
$a | append $a --col
[0,1,2,3] | append 4
[0,1] | append [2,3,4]
[0,1] | append [2,nu,4,shell]
[1 3 2] | into df | arg-max
[1 3 2] | into df | arg-min
[1 2 2 3 3] | into df | arg-sort
[1 2 2 3 3] | into df | arg-sort -r
[false true false] | into df | arg-true
[1 2 2 3 3] | into df | arg-unique
let df = ([[a b]; [one 1] [two 2] [three 3]] | into df);
$df | select (arg-where ((col b) >= 2) | as b_arg)
col a | as new_a | into nu
field name_a | as new_a | into nu
open db.sqlite
| from table table_1
| select a
| as t1
| describe
open db.sqlite
| from table (
open db.sqlite
| from table table_a
| select a b
)
| select a
| as t1
| describe
["2021-12-30" "2021-12-31"] | into df | as-datetime "%Y-%m-%d"
["2021-12-30 00:00:00" "2021-12-31 00:00:00"] | into df | as-datetime "%Y-%m-%d %H:%M:%S"
ast 'hello'
ast 'ls | where name =~ README'
ast 'for x in 1..10 { echo $x '
benchmark { sleep 500ms }
2 | bits and 2
[4 3 2] | bits and 2
[4 3 2] | bits not
[4 3 2] | bits not -n 2
[4 3 2] | bits not -s
2 | bits or 6
[8 3 2] | bits or 2
17 | bits rol 2
[5 3 2] | bits rol 2
17 | bits ror 60
[15 33 92] | bits ror 2 -n 1
2 | bits shl 7
2 | bits shl 7 -n 1
0x7F | bits shl 1 -s
[5 3 2] | bits shl 2
8 | bits shr 2
[15 35 2] | bits shr 2
2 | bits xor 2
[8 3 2] | bits xor 2
build-string a b c
build-string $"(1 + 2)" = one ' ' plus ' ' two
0x[1F FF AA AA] | bytes add 0x[AA]
0x[1F FF AA AA] | bytes add 0x[AA BB] -i 1
0x[FF AA AA] | bytes add 0x[11] -e
0x[FF AA BB] | bytes add 0x[11 22 33] -e -i 1
0x[33 44 55 10 01 13] | bytes at [3 4]
0x[33 44 55 10 01 13] | bytes at '3,4'
0x[33 44 55 10 01 13] | bytes at ',-3'
0x[33 44 55 10 01 13] | bytes at '3,'
0x[33 44 55 10 01 13] | bytes at ',4'
[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes at "1," ColB ColC
bytes build 0x[01 02] 0x[03] 0x[04]
[0x[11] 0x[13 15]] | bytes collect
[0x[11] 0x[33] 0x[44]] | bytes collect 0x[01]
0x[1F FF AA AA] | bytes ends-with 0x[AA]
0x[1F FF AA AA] | bytes ends-with 0x[FF AA AA]
0x[1F FF AA AA] | bytes ends-with 0x[11]
0x[33 44 55 10 01 13 44 55] | bytes index-of 0x[44 55]
0x[33 44 55 10 01 13 44 55] | bytes index-of -e 0x[44 55]
0x[33 44 55 10 01 33 44 33 44] | bytes index-of -a 0x[33 44]
0x[33 44 55 10 01 33 44 33 44] | bytes index-of -a -e 0x[33 44]
[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes index-of 0x[11] ColA ColC
0x[1F FF AA AB] | bytes length
[0x[1F FF AA AB] 0x[1F]] | bytes length
0x[10 AA FF AA FF] | bytes remove 0x[10 AA]
0x[10 AA 10 BB 10] | bytes remove -a 0x[10]
0x[10 AA 10 BB CC AA 10] | bytes remove -e 0x[10]
[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC
0x[10 AA FF AA FF] | bytes replace 0x[10 AA] 0x[FF]
0x[10 AA 10 BB 10] | bytes replace -a 0x[10] 0x[A0]
[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes replace -a 0x[11] 0x[13] ColA ColC
0x[1F FF AA AA] | bytes reverse
0x[FF AA AA] | bytes reverse
0x[1F FF AA AA] | bytes starts-with 0x[1F FF AA]
0x[1F FF AA AA] | bytes starts-with 0x[1F]
0x[1F FF AA AA] | bytes starts-with 0x[11]
[[a b]; [6 2] [4 2] [2 2]] | into df | reverse | cache
cal
cal --full-year 2012
cal --week-start monday
cd ~
cd d/s/9
cd -
char newline
echo [(char prompt) (char newline) (char hamburger)] | str collect
char -u "1f378"
char -i (0x60 + 1) (0x60 + 2)
char -u "1F468 200D 1F466 200D 1F466"
clear
col a | into nu
echo 1 2 3 | collect { |x| echo $x.1 }
open foo.db | from table table_1 db | select a | collect
[[a b]; [1 2] [3 4]] | into lazy | collect
[[a b]; [1 2] [3 4]] | into df | columns
[[name,age,grade]; [bill,20,a]] | columns
[[name,age,grade]; [bill,20,a]] | columns | first
[[name,age,grade]; [bill,20,a]] | columns | select 1
echo [["Hello" "World"]; [$nothing 3]]| compact Hello
echo [["Hello" "World"]; [$nothing 3]]| compact World
echo [1, $nothing, 2] | compact
^external arg1 | complete
let df = ([[a b c]; [one two 1] [three four 2]] | into df);
$df | with-column ((concat-str "-" [(col a) (col b) ((col c) * 2)]) | as concat)
let other = ([za xs cd] | into df);
[abc abc abc] | into df | concatenate $other
config env
config nu
config reset
[abc acb acb] | into df | contains ab
let s = ([1 1 0 0 3 3 4] | into df);
($s / $s) | count-null
cp myfile dir_b
cp -r dir_a dir_b
cp -r -v dir_a dir_b
cp *.txt dir_a
[1 2 3 4 5] | into df | cumulative sum
"2021-10-22 20:00:12 +01:00" | date format
date format '%Y-%m-%d'
date format "%Y-%m-%d %H:%M:%S"
"2021-10-22 20:00:12 +01:00" | date format "%Y-%m-%d"
date humanize
"2021-10-22 20:00:12 +01:00" | date humanize
date list-timezone | where timezone =~ Shanghai
date now | date format "%Y-%m-%d %H:%M:%S"
(date now) - d"2019-05-01"
(date now) - d"2019-05-01T04:12:05.20+08:00"
date now | debug
date to-table
date now | date to-record
'2020-04-12 22:10:57 +0200' | date to-record
date to-table
date now | date to-table
'2020-04-12 22:10:57 +0200' | date to-table
date now | date to-timezone +0500
date now | date to-timezone local
date now | date to-timezone US/Hawaii
"2020-10-10 10:00:00 +02:00" | date to-timezone "+0500"
'hello' | debug
echo [[version patch]; ["0.1.0" false] ["0.1.1" true] ["0.2.0" false]] | debug
^cat myfile.q | decode utf-8
0x[00 53 00 6F 00 6D 00 65 00 20 00 44 00 61 00 74 00 61] | decode utf-16be
echo 'U29tZSBEYXRh' | decode base64
echo 'U29tZSBEYXRh' | decode base64 --binary
def say-hi [] { echo 'hi' }; say-hi
def say-sth [sth: string] { echo $sth }; say-sth hi
def-env foo [] { let-env BAR = "BAZ" }; foo; $env.BAR
ls -la | default 'nothing' target
$env | get -i MY_ENV | default 'abc'
[1, 2, $nothing, 4] | default 3
'hello' | describe
[[a b]; [1 1] [1 1]] | into df | describe
open foo.db | from table table_1 | select col_1 | describe
echo 'a b c' | detect columns -n
echo $'c1 c2 c3(char nl)a b c' | detect columns
[true false true] | into df | df-not
do { echo hello }
do -i { thisisnotarealcommand }
do {|x| 100 + $x } 50
[0,1,2,3] | drop
[0,1,2,3] | drop 0
[0,1,2,3] | drop 2
[[a b]; [1 2] [3 4]] | into df | drop a
echo [[lib, extension]; [nu-lib, rs] [nu-core, rb]] | drop column
[sam,sarah,2,3,4,5] | drop nth 0 1 2
[0,1,2,3,4,5] | drop nth 0 1 2
[0,1,2,3,4,5] | drop nth 0 2 4
[0,1,2,3,4,5] | drop nth 2 0 4
echo [first second third fourth fifth] | drop nth (1..3)
[0,1,2,3,4,5] | drop nth 1..
[0,1,2,3,4,5] | drop nth 3..
[[a b]; [1 2] [3 4] [1 2]] | into df | drop-duplicates
let df = ([[a b]; [1 2] [3 0] [1 2]] | into df);
let res = ($df.b / $df.b);
let a = ($df | with-column $res --name res);
$a | drop-nulls
let s = ([1 2 0 0 3 4] | into df);
($s / $s) | drop-nulls
[[a b]; [1 2] [3 4]] | into df | dtypes
du
[[a b]; [1 2] [3 4]] | into df | dummies
[1 2 2 3 3] | into df | dummies
[1 2 3] | each { |it| 2 * $it }
[1 2 3] | each { |it| if $it == 2 { echo "found 2!"} }
[1 2 3] | each -n { |it| if $it.item == 2 { echo $"found 2 at ($it.index)!"} }
[1 2 3] | each --keep-empty { |it| if $it == 2 { echo "found 2!"} }
[1 2 3] | each while { |it| if $it < 3 {$it} else {$nothing} }
[1 2 3] | each while -n { |it| if $it.item < 2 { $"value ($it.item) at ($it.index)!"} else { $nothing } }
echo 'hello'
echo $nu
echo "負けると知って戦うのが、遥かに美しいのだ" | encode shift-jis
echo 'Some Data' | encode base64
echo 'Some Data' | encode base64 --character-set binhex
enter ../dir-foo
env | where name == PATH
env | any name == MY_ENV_ABC
'PATH' in (env).name
def foo [x] {
let span = (metadata $x).span;
error make {msg: "this is fishy", label: {text: "fish right here", start: $span.start, end: $span.end } }
}
def foo [x] {
error make {msg: "this is fishy"}
}
[1 2 3 4 5] | every 2
[1 2 3 4 5] | every 2 --skip
exec ps aux
exec nautilus
exit
exit --now
module utils { export def my-command [] { "hello" } }; use utils my-command; my-command
export alias ll = ls -l
module spam { export def foo [] { "foo" } }; use spam foo; foo
module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR
module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV
export extern echo [text: string]
module spam { export def foo [] { "foo" } }
module eggs { export use spam foo }
use eggs foo
foo
export-env { let-env SPAM = 'eggs' }; $env.SPAM
((col a) > 2) | expr-not
extern echo [text: string]
[[a b]; [6 2] [4 2] [2 2]] | into df | fetch 2
fetch url.com
fetch -u myuser -p mypass url.com
fetch -H [my-header-key my-header-value] url.com
field name_1 | into nu
[1 2 2 3 3] | into df | shift 2 | fill-null 0
[[a b]; [6 2] [4 2] [2 2]] | into df | filter ((col a) >= 4)
let mask = ([true false] | into df);
[[a b]; [1 2] [3 4]] | into df | filter-with $mask
[[a b]; [1 2] [3 4]] | into df | filter-with ((col a) > 1)
ls | find toml md sh
echo Cargo.toml | find toml
[1 5 3kb 4 3Mb] | find 5 3kb
[moe larry curly] | find l
[2 4 3 6 5 8] | find --predicate { |it| ($it mod 2) == 1 }
[[version patch]; ["0.1.0" false] ["0.1.1" true] ["0.2.0" false]] | find -p { |it| $it.patch }
[abc bde arc abf] | find --regex "ab"
[aBc bde Arc abf] | find --regex "ab" -i
[[version name]; ["0.1.0" nushell] ["0.1.1" fish] ["0.2.0" zsh]] | find -r "nu"
[1 2 3] | first
[1 2 3] | first 2
0x[01 23 45] | first 2
col a | first
[[a b]; [1 2] [3 4]] | into df | first
[[a b]; [1 2] [3 4]] | into df | first 2
[[N, u, s, h, e, l, l]] | flatten
[[N, u, s, h, e, l, l]] | flatten | first
[[origin, people]; [Ecuador, ([[name, meal]; ['Andres', 'arepa']])]] | flatten --all | get meal
[[origin, crate, versions]; [World, ([[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten versions --all | last | get versions
{ a: b, d: [ 1 2 3 4 ], e: [ 4 3 ] } | flatten d --all
42 | fmt
fn count name_1 | into nu
open db.sqlite
| from table table_a
| select (fn lead col_a)
| describe
for x in [1 2 3] { $x * $x }
for $x in 1..3 { $x }
for $it in ['bob' 'fred'] --numbered { $"($it.index) is ($it.item)" }
ls | format '{name}: {size}'
echo [[col1, col2]; [v1, v2] [v3, v4]] | format '{col2}'
ls | format filesize size KB
du | format filesize apparent B
open data.txt | from csv
open data.txt | from csv --noheaders
open data.txt | from csv -n
open data.txt | from csv --separator ';'
open data.txt | from csv --trim all
open data.txt | from csv --trim headers
open data.txt | from csv --trim fields
'From: [email protected]
Subject: Welcome
Test' | from eml
'From: [email protected]
Subject: Welcome
Test' | from eml -b 1
'BEGIN:VCALENDAR
END:VCALENDAR' | from ics
'[foo]
a=1
b=2' | from ini
'{ "a": 1 }' | from json
'{ "a": 1, "b": [1, 2] }' | from json
'{ a:1 }' | from nuon
'{ a:1, b: [1, 2] }' | from nuon
open --raw test.ods | from ods
open --raw test.ods | from ods -s [Spreadsheet1]
'FOO BAR
1 2' | from ssv
'FOO BAR
1 2' | from ssv -n
open db.sqlite | from table table_a | describe
'a = 1' | from toml
'a = 1
b = [1, 2]' | from toml
echo $'c1(char tab)c2(char tab)c3(char nl)1(char tab)2(char tab)3' | save tsv-data | open tsv-data | from tsv
echo $'a1(char tab)b1(char tab)c1(char nl)a2(char tab)b2(char tab)c2' | save tsv-data | open tsv-data | from tsv -n
echo $'a1(char tab)b1(char tab)c1(char nl)a2(char tab)b2(char tab)c2' | save tsv-data | open tsv-data | from tsv --trim all
echo $'a1(char tab)b1(char tab)c1(char nl)a2(char tab)b2(char tab)c2' | save tsv-data | open tsv-data | from tsv --trim headers
echo $'a1(char tab)b1(char tab)c1(char nl)a2(char tab)b2(char tab)c2' | save tsv-data | open tsv-data | from tsv --trim fields
'bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter' | from url
'BEGIN:VCARD
N:Foo
FN:Bar
EMAIL:[email protected]
END:VCARD' | from vcf