Home  >  Article  >  Operation and Maintenance  >  What is the Linux pipe command?

What is the Linux pipe command?

青灯夜游
青灯夜游Original
2023-02-02 18:21:447535browse

The Linux pipeline command is "|", which is used to connect multiple instructions. The output stream of the previous instruction will be used as the operation object of the subsequent instruction. The command format is "Instruction 1 | Instruction 2 | ...", the following instruction of this command must be able to receive the standard input stream command before it can be executed. The pipeline command can only process the correct output of the previous instruction, but cannot handle the error output; the subsequent instruction of the pipeline command must be able to receive the standard input stream command before it can be executed.

What is the Linux pipe command?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is the Linux pipe command?

1. The Linux pipeline command is “ | ”, which is used to connect multiple instructions. The output stream of the previous instruction will be used as the following The operation object of an instruction has the command format of "Instruction 1 | Instruction 2 | ...". The subsequent instruction of this command must be able to receive the standard input stream command before it can be executed.

2. The operator of the pipeline command is: "|", which can only process the correct output information sent by the previous instruction, and has no direct processing capability for error information. Then, it is passed to the next instruction as the operation object.

Basic format: Command 1 | Command 2 | …

[Reminder]:

1. The pipeline command can only process the correct output of the previous instruction, but cannot handle the error output;

2. The next instruction of the pipeline command must be able to receive the standard input stream The command can be executed.


Cut— Extract from the command result corresponding content

Step one: Intercept Specify the first 2 lines in the file The 5 character

Command Meaning
cut action file Intercept content from the specified file
Parameters English Meaning
- c characters Select content by characters
Order: head -2 file name | cut -c 5

The second step: Intercept Specify the first 2## in the file #The lines are separated by ":"#1,2Section content

##-d 'Delimiter'-f n1,n2##fifields
Parameters
English
Meaning
delimiter
#Specify delimiter
Which paragraph of content will be displayed after splitting
, Use , to split
##Range ControlMeaningnDisplay only the nth item Display from the nth item to the end of the lineDisplay items from n to m (including m)
n-
n-m
Command:

head -2 file name | cut -d ':' -f 1,2

## or

head -2

File name

| cut -d ':' -f 1-2 Step Three

: Interception Export the first 2 lines in the specified ## file with ":"#1,2,3Section contentCommand:

head -2 file name | cut -d ':' -f 1 ,2,3

or ##head -2 file name | cut -d ':' -f 1-3

sort—Can sort the contents of text files in units of lines

First step

:

YesString##SortCommand: sort

File name

Step 2

:

DeduplicationSort Its function is very simple, it is to remove duplicates in the output rows OK.

  • ParametersEnglish
MeaningRemove duplicates
-u unique

Command: sort -u File name

Step Three: Sort the values

##-nnumeric-sortSort by numerical value-rreverseReverse the number of times
Parameters English Meaning
  • Command

Default sorting is based on string: sort file name

Ascending order: sort -n file name

Descending order: sort -n -r file name

Merge: sort -nr File name —> The effect is the same as descending order

Step 4: Sort results

ParameterEnglishMeaning-tSpecify the field separator-kkeySort according to one column
fifield -separator
# Display all contents in reverse order based on the second section score
sort -t ' ' -k2nr File name
Note: ' ' middle There is a space

##wc command - display/statistics of the specified number of bytes, number of words, and number of lines in the file Information

Step 1: Display the number of bytes in the specified file , Word count, Line count information.

##CommandMeaning##wc file namedisplaynumber of lines , number of words, number of bytes, specified file Command:
information

wc File name##Step 2

: Only display the number of lines in the file##Parameters

EnglishMeaningbytesNumber of bytesnumber of wordsnumber of lines
-c
- w words
-l lines

Command:

#wc -l file name -----> Number of lines

wc -c file name -----> Number of bytes

wc -w file name - ----> Number of words

Step 3: Count the number of lines, words, bytes of multiple files

Command: wc File 1 File 2 File 3 File 4

Example: wc 1.txt 2. txt 3.txt 4.txt

Or:

Command: wc *.txt

Step 4: View/etc How many sub-contents are there in the directory

Command: ls /etc | wc -l

##uniq— Used to check and delete repeated lines in text files [Deduplication

    is generally used in conjunction with the sort
  • command.

Step one: Achieve duplicate removal effect

Commanduniq [parameter] file
English Meaning
unique unique remove Repeat lines
Command:

cat file name | sort | uniq —》Sort according to string And remove duplicates

The second step: not only remove duplicates, but also count the number of occurrences

Parameters##-ccountCount the number of occurrences of each lineCommand:
English Meaning
cat file name | sort | uniq -c

tee —
Put the command result

through the pipeline Output to Multiple files

##CommandMeaningCommand result| tee file 1 file 2 file 3
pass tee
You can convert the command results Through pipe Output to Multiple files middle
  • Put the results of deduplication statistics into a.txt,b.txtc.txt In the file

Command: cat to remove duplicate file names | sort | uniq -c | tee a.txt b.txt c.txt

tr — is used for to replace or Remove characters in the file

The first step: Achieving the replacement effect

Command English

Meaning

Command result | tr replaced character new character translate Realize the effect of replacement
# Lowercase he Replace with uppercase HE
echo "helloworld" | tr 'he' ''HE
# Bundle helloworld Convert to uppercase
echo "helloworld" | tr '[a-z]' 'A-Z'
# Bundle HELLO Convert to lowercase
echo "HELLO" | tr 'A-Z' 'a-z'

Second step: Achieving the deletion effect

Command English Meaning
Command result | tr -d Deleted characters
delete
#Delete the specified characters
# delete abc1d4e5f Numbers in
##echo 'abc1d4ee5f' | tr -d '[0-9]'

Step Three: Word count

# Count the number of occurrences of each word
Sample data:
##[root@node001 opt]
# cat words.txt
hello,world,hadoop
hive,sqoop,flume,hello
kitty,tom,jerry,world
hadoop
Implementation steps:
1
、Change the delimiter “,” Replace with newline character
2
, Sort
3
, remove duplicates
4
、Count
# Order
cat words.txt |tr ',' '\n'|sort |uniq -c
    can be achieved by
  • tr [option] character 1 character 2 Replace and and delete Effect
  • Related recommendations: "
Linux Video Tutorial

"

The above is the detailed content of What is the Linux pipe command?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn