Linux pipelines, redirects, and text processing
- 1 Linux Multi-command collaboration: Pipelines and redirects
- 2 Linux command line Text Processing tool
- File browsing
- Search-grep based on keyword
- Column-based processing of text-cut
- Text Statistics-WC
- Text Sort-sort
- Delete duplicate rows
- Text comparison-diff
- Check spelling-aspell
- Working with text content-tr
- Search for replacement-sed
An important idea of open source: Don't reinvent the wheel .
Many open source software is the re-grouping of existing software, code, and functionality.
"The Cathedral and the Bazaar"
1 Linux Multi-command collaboration: pipelines and redirect pipelines and redirects
Linux, most of the commands are simple, often only one or a few simple functions, we can combine the commands of different functions together to achieve the purpose of a complex function.
In Linux, almost all commands are returned in plain text, whereas plain text data is the input format for most commands, which makes collaboration possible.
The command behavior of Linux we provide pipelines and redirection mechanisms, and multi-command collaboration is done through pipelines and redirects.
- The command line shell data flow has the following definitions:
name |
Description |
numbering |
default |
Stdin |
Standard input |
0 |
Keyboard |
STDOUT |
Standard output |
1 |
Terminal |
STDERR |
Standard error |
2 |
Terminal |
The command receives the parameter or data through the stdin, stdout the output or passes the STDERR output error.
- Pipeline and redirection to control the data flow of the CLI
- Pipelines are often used to combine different commands for more complex functions
- Redirection is typically used to save output or error messages for a command
category |
Key Words |
definition |
Example |
redirect |
> |
redirect stdout to file (overwrite) |
echo "linuxcast.net" > outfile |
redirect |
>> |
redirect stdout to file (append) |
echo "linuxcast.net" >> outfile |
redirect |
2> |
redirect stderr to file (overwrite) |
echo "linuxcast.net" 2> outfile |
redirect |
2>&1 |
Combine stderr with stdout |
echo "linuxcast.net" 2>&1 outfile |
redirect |
< |
Stdin |
grep linuxcast < /etc/passwd |
Pipeline |
| |
StdOut a command as a stdin of another command |
ls -l | grep linuxcast |
A more complex example: Find the video file of the user Linuxcast, redirect the lookup error to null (discard)
find / -user linuxcast 2> /dev/null | grep Video
2 Linux command line Text Processing tool file browsing
Search-grep based on keyword
This command uses regular expressions to search for a specified plain text file or plain text (pipe)
-i
Ignore case
-n
Number of rows in the actual result (file)
-v
Output lines without keywords (inverse selection)
-Ax
The output contains the specified number of rows after the row of the result
-Bx
The output contains the specified number of rows before the row of the result
Column-based processing of text-cut
-d
Specify the split character, which is tab by default
-f
Specify the column number of the output
-c
Cut based on character, fixed length
Cases:
cut -d: -f1 /etc/passwd
To :
display the first column as a separator
grep linuxcast /etc/passwd | cut -d: -f3
UID of output user linuxcast (third column)
cut -c2-6 /etc/passwd
第2-6个 characters
Text Statistics-WC
-l
Count Rows Only Line
-w
Count only word Word
-c
Count bytes Only
-m
Count characters only
Text Sort-sort
-r
Make a flashback sort
-n
Sort based on a number
-f
Ignore case
-u
Delete duplicate rows
-tc
Use C as a delimiter to sort columns
-kx
Specifies which column to sort on when sorting based on the specified character is divided into columns
Delete duplicate rows
sort -u
Delete duplicate rows when sorting
uniq
Delete a duplicate adjacent row
Text comparison-diff
-i
Ignore case
-b
Ignore the change in the number of spaces
-u
Unified display of comparison information (typically used to generate patch files)
diff -u linuxcast linuxcast-new > final.patch
Check spelling-aspell
aspell check linuxcast
aspell list < linuxcast
Working with text content-tr
Translate
tr -d ‘TMD‘ < linuxcast
Delete keyword
tr ‘a-z‘ ‘A-Z‘ < linuxcast
Convert case
Search for replacement-sed
Working with regular expressions to work with text files
sed ‘s/linux/unix/g‘ linuxcast
Replace Linux for UNIX, /g
replace all
sed ‘1,50s/linux/unix/g‘ linuxcast
Replace 1-50 lines
sed -e ‘s/linux/unix/g‘ -e ‘s/nash/nash_su/g‘ linuxcast
Multiple regular expressions
sed -f sededit linuxcast
To save a match expression in an Sededit file
Getting Started with Linux-7 Linux pipelines, redirects, and text processing