unix-programming-and-regular-expressions-workshop

所属分类:collect
开发工具:Shell
文件大小:0KB
下载次数:0
上传日期:2023-04-19 06:01:15
上 传 者sh-1993
说明:  使用grep、sed、awk、shell编程和正则表达式等工具的Unix编程原则研讨会
(A workshop on Unix Programming Principles using tools such as grep, sed, awk, shell programming and regular expressions)

文件列表:
.eslintrc.js (459, 2019-09-10)
.scripts/ (0, 2019-09-10)
.scripts/print-image-path.sh (85, 2019-09-10)
.scripts/strip-text-in-brackets.sh (163, 2019-09-10)
CONTRIBUTING.md (11271, 2019-09-10)
LICENSE (1076, 2019-09-10)
SUMMARY.md (1581, 2019-09-10)
book.json (110, 2019-09-10)
data/ (0, 2019-09-10)
data/aapl.csv (1767, 2019-09-10)
data/allnumbers.txt (28, 2019-09-10)
data/bank_statement.csv (141, 2019-09-10)
data/betty (0, 2019-09-10)
data/cities.sorted.txt (74, 2019-09-10)
data/cities.txt (73, 2019-09-10)
data/colors.txt (37, 2019-09-10)
data/comm1.txt (40, 2019-09-10)
data/comm2.txt (45, 2019-09-10)
data/dawg (0, 2019-09-10)
data/demographic-states-by-city.csv (27350, 2019-09-10)
data/find-and-replace.txt (27, 2019-09-10)
data/information.txt (137, 2019-09-10)
data/jacob (0, 2019-09-10)
data/jacob.txt (18, 2019-09-10)
data/jenny (0, 2019-09-10)
data/jingleheimer (0, 2019-09-10)
data/john (0, 2019-09-10)
data/msft.csv (1651, 2019-09-10)
data/nestedFolder/ (0, 2019-09-10)
data/nestedFolder/anotherText.dat (11, 2019-09-10)
data/num.txt (2, 2019-09-10)
data/numbers.txt (14, 2019-09-10)
data/numbers1.txt (16, 2019-09-10)
data/numbers2.txt (14, 2019-09-10)
... ...

# Unix Programming and regular expressions workshop A workshop on Unix Programming Principles using tools such as grep, sed, awk, shell programming and regular expressions ## Sections: * [Unix History](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/#unix-history) * [Unix Software Philosophy](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/#unix-software-philosophy) * [Self Contained Shell Scripts](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/#self-contained-shell-scripts) * [Basics of Shell Programming](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/basics-of-shell-programming.md) * [Introduction to Text Processing](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/introduction-to-text-processing.md) * [Text Searching](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/text-searching.md) * [Text Substitution](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/text-substitution.md) * [Filename expansions and globbing](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/filename-expansions-and-globbing.md) * [Working with Fields](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/working-with-fields.md) * [Text Sorting](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/text-sorting.md) * [Arithmetic Operations and variables](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/arithmetic-operations-and-variables.md) * [Decision Making and Exit Status](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/decision-making-and-exit-status.md) * [Looping](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/looping.md) * [Input and Output](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/input-and-output.md) * [Command Process Substitution](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/command-process-substitution.md) * [History Substitution](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/history-substitution.md) * [Evaluation Order](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/evaluation-order.md) * [Subshells](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/subshells.md) * [Shell Functions](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/shell-functions.md) * [Signal Handling](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/signal-handling.md) * [Working with Files](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/working-with-files.md) * [Remote File Sharing and File Access](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/remote-file-sharing-and-file-access.md) * [Build Automation](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/build-automation.md) * [Building Command line applications](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/building-command-line-applications.md) * [Shell Login Order](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/shell-login-order.md) * [Network Utilities](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/network-utilities.md) * [Shell Security](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/shell-security.md) * [Find and Replace Patterns](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/find-and-replace-patterns.md) * [System Administration Commands](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/docs/system-administration-commands.md) ## Unix History * Shell Scripting was developed in the context of the UNIX Operating System from Bell Labs * Early UNIX systems packed incredible power into very small machines * 64 Kb "virtual" address space for the code and for data * This was often less than that of physical memory on the early [PDP-11S](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://en.wikipedia.org/wiki/PDP-11) * Source Code made it easy to experiment and change the system * AT&T Bell Labs heavily influenced Unix by the likes of [Ken Thompson](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://en.wikipedia.org/wiki/Ken_Thompson), [Dennis Ritchie](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://en.wikipedia.org/wiki/Dennis_Ritchie), and others Quote from Dennis Ritchie for Vision of Unix: > What we wanted to preserve was not just a good environment in which to do programming, but a system around which a fellowship could form. We knew from experience that the essence of communal computing, as supplied by remote-access, time-shared machines, is not just to type programs into a terminal instead of a keypunch, but to encourage close communication. * Unix Developers were the users of the system and they developed tools to solve their own problems * Unix Developers were given freedom to experiment and rewrite Unix as needed * Unix was designed in a quest for elegance ## Unix Software Philosophy [Software Tools Book](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://www.amazon.com/Software-Tools-Brian-W-Kernighan/dp/020103669X) and [Software Tools in Pascal](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://www.amazon.com/Software-Tools-Pascal-Brian-Kernighan/dp/0201103427/ref=pd_lpo_sbs_14_img_0?_encoding=UTF8&psc=1&refRID=2HVC5MB1PKHC4YCETHP9&dpID=41eyH0p9aiL&preST=_SY291_BO1,204,203,200_QL40_&dpSrc=detail) * Programs should be like specialized tools in a carpenter's toolbox * Avoid `create programs to rule them all` * Don't create programs that are like a Swiss Army Knife... meaning they do too much * One simple example would be sorting... either you can do one of the two following things: * Write a bunch of programs to do various tasks, each of which has an option to sort its output * Choose a common representation for your system (e.g. streams of ASCII text), create a mechanism for composing pieces of the system (Unix pipes), and only write the sorting functionality once * A less simple example would be the [LLVM compiler](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/http://www.aosabook.org/en/llvm.html) (and compilers in general), which uses an intermediate representation (IR) that is understood and operated on by all pieces of the system as the compiler does all the passes required to generate its target (e.g. lexing, parsing, optimization, code generation, etc.) * In general, choosing a common representation for a system will turn the problem of interfacing the various pieces from an m*n problem into an m+n problem (where m is the number of different outputs in the first stage, n is number of inputs that are received in the second stage) * Tools can be combined using pipelines and the shell to get your work done * One famous example is Doug McIlroy's word count program, compared to Donald Knuth's, as described [here](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/http://www.leancrew.com/all-this/2011/12/more-shell-less-egg/) and elsewhere * This philosophy became popular in [Kernighan & Plauger Books](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://www.amazon.com/Software-Tools-Brian-W-Kernighan/dp/020103669X) * Some example presentations that discuss the Unix philosophy include: * [Unix Pipeline (Brian Kernighan) - Computerphile](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://www.youtube.com/watch?v=bKzonnwoR2I), which discusses the concept of the Unix pipe * [Bringing the Unix Philosophy to Big Data by Bryan Cantrill](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://www.youtube.com/watch?v=S0mviKhVmBI), which discusses the Unix philosophy in relation to big data * [Pycon UK 2016: Python and the Glories of the UNIX Tradition by Brandon Rhodes](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://www.youtube.com/watch?v=zFMdhXYlFfY), which discusses the Unix philosophy in relation to Python #### Do One thing Well Programs are easier to: 1. Write and to correct 2. Document 3. Understand and use The `cat` command originally only concatenated files The `cp` command copies files The `mv` command moves and renames files #### Process Lines of Text Using Text as the main data format has advantages: * Text is easy to process with existing and new tools * Text can be edited with any text editor * Text is portable across networks and machine architectures For example to list some popular baby names and sort them: ```bash cat data/top-10-baby-names-2016.txt | awk '{print $2 }' | sort ``` #### Use Regular Expressions * Regular Expressions provide powerful text matching and substitution 2 Flavors of regular expressions standardized by [POSIX](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://en.wikipedia.org/wiki/POSIX) 1. Basic Regular Expressions (BREs) 1. grep, sed, ... 2. Extended Regular Expressions (EREs) 1. egrep, awk, ... #### Default to Standard Input/Output Use Standard Input/Output (I/O) when there is no files on the command line: * Helps simplify writing programs * Helps you hook programs together with pipelines * Helps encourage programs to do one thing well #### Don't Be Chatty * Status messages that are mixed with standard output confuse programs downstream * If you ask then you get it. Don't prompt with 'Are you sure' * Do know what you are doing: * `rm -rf /` Before running a command like this * This will delete everything starting from the root directory * We have version control systems such as `Git` use them #### Make sure to use the input format for output * If your text is structured then after processing * Write the same format for standard output in the same format of standard input * Doing this affords you to build specialized tools that work together #### Write specialized tools if they do not exist * At times a tool does not exist, that is when you need to write the tool * Can the tool be useful to other people? * Can the tool be generalized? If any of the answers to these questions are yes: * then write a general purpose tool * Scripting languages can often be used to write a software tool: * Awk * Perl * Python * Ruby * Shell * You can also use other languages like for example `Golang` as we will see #### Software Tools Summary * Using the `software tools` approach helps provide a framework and a mindset for programming and scripting * You can combine software tools to solve software programs * This strategy in turn gives you flexibility and helps promote innovation * Know your tools and thinking in the `Software Tools` Philosophy will improve your scripting ## Self Contained Shell Scripts [Executable Definition](https://github.com/jbelmont/unix-programming-and-regular-expressions-workshop/blob/master/https://en.wikipedia.org/wiki/Executable) > In computing, executable code or an executable file or executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instructions,"[1] as opposed to a data file that must be parsed by a program to be meaningful. Typically a high level language is used that compiles to executable machine code files * Executable scripts typicall start with a `Shebang` => `#! /bin/bash` or the like * An optional argument can be provided * Some Unix systems have small limits on the path name length Shell Scripts can be simple executable text files that contain shell commands. * Keep in mind that this only works if the shell script is in the same language as the interactive shell * For example to expect a zsh shell script to run in a bash environment

近期下载者

相关文件


收藏者