Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Py3 /
20250603bSzűrőkkel kapcsolatos feladatok Implementáljuk a következő szűrőket. (Lustaságból ezeket nem írtam át magyarra.) (1) between: Print lines between $ cat 123.txt one two three $ cat 123.txt | between 2 3 two three (2) bin: Decimal number to binary $ echo 2025 | bin 0b11111101001 $ echo 2025 | bin | unbin 2025 (3) border: Draw border around text $ echo "Section 1" | border ################# ## Section 1 ## ################# $ echo "Section 1" | border '+' +++++++++++++++++ ++ Section 1 ++ +++++++++++++++++ (4) capitalize: Convert to capitalized text $ echo "hEllO" | capitalize Hello (5) collapse: Collapse multiple whitespaces into single space $ echo " .aa bb. " | quotes ' .aa bb. ' $ echo " .aa bb. " | collapse .aa bb. $ echo " .aa bb. " | collapse | quotes '.aa bb.' (6) dedup: Remove duplicate lines and keep the original order $ cat file.txt dd bb dd # duplicate aa aa # duplicate bb # duplicate $ cat file.txt | dedup dd bb aa In the first example, "# duplicate" is just a note, not part of the file. (7) dups: Find duplicate lines $ cat file.txt dd bb dd # duplicate aa aa # duplicate aa # duplicate $ cat file.txt | dups dd aa aa $ cat file.txt | dups | dedup dd aa In the first example, "# duplicate" is just a note, not part of the file.
(8) ex.title: Extract HTML title from a webpage $ curl -s https://fishshell.com/ | ex.title fish shell This result is extracted from the HTML part " (9) filesize: Convert filesize [bytes] to human-readable format $ echo 123456 | filesize 120.56 KB (10) freq: Word frequency [simple, case-insensitive] $ cat words.txt aa bb aa aa cc cc dd $ cat words.txt | freq aa: 3 cc: 2 bb: 1 dd: 1 (11) getcol: Split the input on whitespace and print the column indicated $ echo 1 2 | getcol 2 2 (12) getrow: Print the row of the input indicated $ seq 3 1 2 3 $ seq 3 | getrow 2 2 (13) hex: Decimal number to hex $ echo 2025 | hex 0x7e9 $ echo 2025 | hex | unhex 2025 (14) joinlines: Join the input lines with a separator $ cat text.txt aaa bbb ccc $ cat text.txt | joinlines aaabbbccc $ cat text.txt | joinlines ", " aaa, bbb, ccc (15) len: Length of a string $ echo "hello" | len 5 (16) longest: Length of the longest line $ cat main.c | longest 22 (17) lower: Convert to lowercase $ echo "hEllO" | lower hello $ echo "hEllO" | lower | upper HELLO (18) morse: Convert text to Morse code [e.g., 'SOS' → '… --- …'] $ echo "SOS Titanic" | morse ... --- ... / - .. - .- -. .. -.-. $ echo "SOS Titanic" | morse | unmorse SOS TITANIC
(19) noaccents: Remove accents [á → a, etc.] $ echo "László" | noaccents Laszlo (20) nonempty: Remove empty lines $ cat main.c #include <stdio.h> int main() { printf("hello\n"); return 0; } $ cat main.c | nonempty #include <stdio.h> int main() { printf("hello\n"); return 0; } (21) obfuscate: Replace letters with similar-looking symbols [e → 3, a → @, etc.] $ echo "This is just a sentence." | obfuscate 7h1$ 1$ ju$7 @ $3n73nc3. (22) oct: Decimal number to octal $ echo 2025 | oct 0o3751 $ echo 2025 | oct | unoct 2025 (23) p.allext: Path [/usr/lib/a.tar.gz → .tar.gz] $ echo /usr/lib/python2.5/stuff.tar.gz | p.allext .tar.gz (24) p.ext: Path [/usr/lib/a.tar.gz → .gz] $ echo /usr/lib/python2.5/stuff.tar.gz | p.ext .gz (25) p.fname: Path [/usr/lib/stuff.tar.gz → stuff] $ echo /usr/lib/python2.5/stuff.tar.gz | p.fname stuff (26) p.name: Path [/usr/lib/python2.5/gopherlib.py → gopherlib.py] $ echo /usr/lib/python2.5/stuff.tar.gz | p.name stuff.tar.gz (27) p.parent: Path [/usr/lib/python2.5/gopherlib.py → /usr/lib/python2.5] $ echo /usr/lib/python2.5/stuff.tar.gz | p.parent /usr/lib/python2.5 (28) p.stem: Path [/usr/lib/python2.5/gopherlib.py → gopherlib] $ echo /usr/lib/python2.5/stuff.tar.gz | p.stem stuff.tar (29) prettyjson: Pretty-print JSON $ cat example.json { "title": "Hackers", "year": 1995 } $ cat example.json | prettyjson { "title": "Hackers", "year": 1995 } (30) prettynum: Prettify a number $ echo 12345679 | prettynum 12,345,679 $ echo 12345679 | prettynum '_' 12_345_679 (31) quotes: Add quotes around the input $ echo "hello" hello $ echo "hello" | quotes 'hello' (32) randomcase: Alternate case randomly $ echo "Hello World" | randomcase heLLo woRLd $ echo "Hello World" | randomcase HEllo WorLD (33) randomline: Select a non-empty random line from input $ cat 123.txt one two three $ cat 123.txt | randomline two $ cat 123.txt | randomline one (34) repeat: Repeat a text $ echo "*" | repeat 5 ***** (35) replace: Replace $ echo "cat dog cat cat" | replace cat kitten kitten dog kitten kitten (36) reverse: Reverse a string $ echo "hello" | reverse olleh $ echo "hello" | reverse | reverse hello If you pass it the content of a text file, it reads the whole content of the file and reverses the whole content. (37) rot: Rotate letters with with $ echo "Fish" | rot 2 Hkuj $ echo "Fish" | rot -2 Dgqf $ echo "Fish" | rot 2 | rot -2 Fish $ echo "Fish" | rot 13 Svfu $ echo "Fish" | rot 13 | rot13 Fish Letters of the English alphabet are rotated by (38) shuffle: Shuffle characters in each line $ echo "12345678" | shuffle 14857362 $ echo "12345678" | shuffle 12563487 (39) skip: Skip the first $ seq 5 1 2 3 4 5 $ seq 5 | skip 2 3 4 5 (40) swapcase: Swap lower- and uppercase $ echo "Hello World" | swapcase hELLO wORLD (41) take: Take the first $ seq 5 1 2 3 4 5 $ seq 5 | take 3 1 2 3 (42) title: Convert to title case $ echo "hello world" | title Hello World (43) trim: Trim leading/trailing whitespace $ echo " aa bb " | trim aa bb $ echo " aa bb " | trim | len 5 (44) typewriter: Print like a typewriter $ cat main.c | typewriter #include <stdio.h> int main... The content of the file is printed character by character, with some delay, similarly to a typewriter. (45) unbin: Binary number to decimal $ echo 1110 | unbin 14 $ echo 1110 | unbin | bin 0b1110 (46) unhex: Hex number to decimal $ echo ff | unhex 255 $ echo ff | unhex | hex 0xff (47) unmorse: Convert Morse code to text [e.g., '… --- …' → 'SOS'] $ echo "- .. - .- -. .. -.-." | unmorse TITANIC $ echo "- .. - .- -. .. -.-." | unmorse | morse - .. - .- -. .. -.-.
(48) unoct: Octal number to decimal $ echo 755 | unoct 493 $ echo 755 | unoct | oct 0o755 (49) upper: Convert to uppercase $ echo "hEllO" | upper HELLO $ echo "hEllO" | upper | lower hello (50) words: Split input into words $ cat file.txt aa bb cc dd $ cat file.txt | words aa bb cc dd |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |