update
This commit is contained in:
parent
959a406ece
commit
6e4c9561a4
@ -102,6 +102,94 @@
|
||||
"cat myamazingfile.txt \n",
|
||||
"#These are the contents of the file\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Pipes and pipelines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ls -l | less\n",
|
||||
"#(... A list of files appears...)\n",
|
||||
"# It lists all files and directories in the current directory in long format (including permissions, number of links, owner, group, size, and modification date). The output is then piped into \"less\" command which allows you to page through the results one screen at a time.\n",
|
||||
"# This code does the following: \n",
|
||||
"# 1. cat spider.txt: Reads the contents of the file \"spider.txt\"\n",
|
||||
"# 2. tr ' ' '\\n': Uses the 'tr' command to replace all spaces in the text with newline characters. This transforms each word into a separate line, preparing it for sorting and counting unique occurrences.\n",
|
||||
"# 3. sort: Sorts the lines (words) alphabetically\n",
|
||||
"# 4. uniq -c: Counts the number of occurrences of each line (word), prefixing each line with the count\n",
|
||||
"# 5. sort -nr: Sorts the lines in numerical and reverse order (i.e., descending order) based on the counts from the \"uniq -c\" command. This will put the most frequent words at the top.\n",
|
||||
"# 6. head: Displays only the first few lines of the sorted output. By default it shows the first line but here we may change this number to view more results.\n",
|
||||
"cat spider.txt | tr ' ' '\\n' | sort | uniq -c | sort -nr | head\n",
|
||||
"# This part does the following:\n",
|
||||
" # 7 the\n",
|
||||
" # 3 up\n",
|
||||
" # 3 spider\n",
|
||||
" # 3 and\n",
|
||||
" # 2 rain\n",
|
||||
" # 2 itsy\n",
|
||||
" # 2 climbed\n",
|
||||
" # 2 came\n",
|
||||
" # 2 bitsy\n",
|
||||
" # 1 waterspout."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"cat capitalize.py\n",
|
||||
"#!/usr/bin/env python3\n",
|
||||
"\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"for line in sys.stdin:\n",
|
||||
" print(line.strip().capitalize())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"cat haiku.txt \n",
|
||||
"#advance your career,\n",
|
||||
"#automating with Python,\n",
|
||||
"#it's so fun to learn.\n",
|
||||
"\n",
|
||||
"cat haiku.txt | ./capitalize.py \n",
|
||||
"#Advance your career,\n",
|
||||
"#Automating with python,\n",
|
||||
"#It's so fun to learn.\n",
|
||||
"\n",
|
||||
"./capitalize.py < haiku.txt\n",
|
||||
"#Advance your career,\n",
|
||||
"#Automating with python,\n",
|
||||
"#It's so fun to learn."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
Loading…
Reference in New Issue
Block a user