Text processing
Line-oriented work on text files: count, sort, filter, compare and rewrite. The other 5 (tokenize, parse an expression, postfix, balanced brackets, recursive descent) are in language-implementation and syntax-and-semantics.
Concepts
- Delimited text
Delimited text stores a table as lines of text: each line is a record and a delimiter character separates its fields. A format must say what happens when a field holds the delimiter.
intro - Line ending
A line ending marks the end of a line of text. Unix uses LF, Windows uses CR LF and classic Mac OS used CR, so a reader must know which endings to accept and whether to keep them.
intro
Tasks
- Compute a word count
Count the lines, words and bytes of a UTF-8 file as wc does, with a word as a run of code points that do not have the Unicode property White_Space.
10 languagesintro - Count the lines of a file
Count the LF bytes of a file as wc -l does, and the lines with a last line that has no LF, by reading the bytes in chunks without decoding them.
10 languagesintro - Filter lines that match a pattern
Read a UTF-8 file and return each line in which a regular expression matches some part, in the order of the file, as grep -E does.
10 languagesintro - Print the first and last lines of a file
Read a UTF-8 file one time and return its first N lines and its last N lines, as head -n and tail -n do, with a buffer that holds only N lines for the end.
10 languagesintro - Read a specific line from a file
Read a UTF-8 file up to line N and return that line, or no line if the file has fewer lines, with line numbers that start at 1 and with only LF as the end of a line.
10 languagesintro - Remove duplicate lines
Read a UTF-8 file and return its lines with only the first copy of each line, in the order of the file, with a set of the lines that were seen, as awk '!seen[$0]++' does.
10 languagesintro - Sort the lines of a file
Read a UTF-8 file, split it at each LF, and return its lines sorted by code point, which is the order of the UTF-8 bytes and the order of LC_ALL=C sort.
10 languagesintro - Align columns of text
Align the fields of a text in columns, left, right or center, where a dollar sign separates two fields and the width of a column is the number of code points of its longest field.
10 languagescore - Apply a patch
Read a UTF-8 file and a unified diff, and return the new text. Each hunk must match the old lines at the place that its header gives, with no fuzz and no offset.
10 languagescore - Compute word frequencies
Read a file of any size in parts and return its n most frequent words, with a word as a run of ASCII letters in lower case and ties in byte order, as the tr, sort and uniq pipeline does.
10 languagescore - Diff two texts
Read two UTF-8 files, split each into lines, and return a shortest edit script of kept, deleted and added lines, made from a longest common subsequence of the lines.
10 languagescore - Remove lines from a file
Remove count lines from a file, from line start, by writing the rest to a new file in the same directory and renaming it over the old one, so a reader never sees a part of a file.
10 languagescore - Replace text in many files
Replace each instance of a text with another text in a list of files, and write each changed file to a new file that a rename puts in the place of the old one.
10 languagescore - Follow a growing file
Read each new line of a file as it grows, as tail -F does, and keep the lines when the file is truncated, renamed, deleted or made again under the same name.
9 languagesadvanced
Techniques
- Process a large text file as a stream
Read a text file one line at a time and keep only the state that the result needs, so that the memory of the program does not grow with the size of the file.
core - Unix text processing pipeline
Solve a text task with a chain of small filter programs that each read lines from standard input and write lines to standard output, connected with the shell operator |.
core
Formats
- Fixed-width text format
A fixed-width text file puts each field of a record at fixed columns and pads it with spaces. A layout outside the file gives the columns, so the reader cuts the line at those positions.
core - Unified diff format
A unified diff shows the changes between two text files as hunks. Each hunk has a header with the line ranges in the old and the new file, then the lines with a mark: a space, - or +.
core