shell programming -- common commands

Directory header

1. diff and patch commands

diff usually compares the contents of files, and patch is often used to make patches
< indicates the contents of the first file
>Represents the contents of the second file

a - add
c - change
d - delete

usage method:

diff -u file2 file1 > file2.path  # It means that file1 shall prevail and file2 shall be patched
patch file2 file2.path

patch -b file1 file1.path       # Indicates that the source file is reserved to patch file1, and the file.orig file will be generated

diff -r /etc/ /tmp/              # When diff compares directories, the result is that the files in the directories are different, not the contents of the files.

2. cut command

Usually used to intercept

cut -d : -f 1 passwd           # -d: Specify separator - f: Specifies the column to intercept
cut -d : -f 1,3 passwd         # First to third columns
cut -d : -f 3- passwd
cut -c 1-3 passwd            # Intercept the first three characters, - c specifies the position of the intercepted character
cut -c 1,3 passwd          # Intercept two characters at 1 and 3

3. sort command

Usually used for sorting

sort file		# Sort the first single character of each line
sort -n		# Pure number sorting
sort -r		# Flashback
sort -u		# Remove duplicate numbers
sort -o		# Output to specified file
sort -t		# Specify separator
sort -k		# Specify columns to sort
sort -n -k 2 -t ': ' test
sort -u		# Show unique rows
sort -d		# Show duplicate rows
sort -c		# Display once per line and count the number of duplicate lines

4. uniq command

Deal with repeated characters accordingly

uniq -c		# Count the number of times
uniq -d		# Show duplicate rows
uniq -u		# Show non repeating rows

Practice:
Document content
#Information in each column: name, height, age, salary
lisi 170 25 6000
zjy 156 23 10000
ljl 175 24 15000
wtq 165 26 10000
wae 180 25 8000

Sort by employee name
[root@localhost sort]# sort test
lisi 170 25 6000
ljl 175 24 15000
wae 180 25 8000
wtq 165 26 10000
zjy 156 23 10000
Rank by employee rise:
[root@localhost sort]# sort -k 2 -n test
# Information in each column: name, height, age, salary
zjy 156 23 10000
wtq 165 26 10000
lisi 170 25 6000
ljl 175 24 15000
wae 180 25 8000

5. seq command

Square is an abbreviation of a sequence, which is mainly used to output serialization
Usage: seq [options] Mantissa
Or seq [options] First and last
Or seq [options] First increment mantissa to specify the increment from the first to the last.
-f. – format = format uses printf style dot format
-s. – separator = string uses the specified string to separate numbers (default is: \ n)
-w. – equal width add 0 before the column to make the width the same [auto fill]
– help display this help message and exit
– version displays version information and exits

[root@localhost seq]# seq -s '$' 5
1$2$3$4$5

[root@localhost seq]# seq -w 10
01
02
03
04
05
06
07
08
09
10

[root@localhost seq]# seq -2 2 10
-2
0
2
4
6
8
10


[root@localhost seq]# seq -f "%03g" 99 110
099
100
101
102
103
104
105
106
107
108
109
110

6. join command

Splicing different content (line splicing)

join -t : file1 file2		# Spliced with: separator
join -a2 file1 file2		# Show the matched and unmatched content in file2 (the unmatched content is empty)
join -a1 -a2 file1 file2		#Show all records for file1 and file2
join -o 1.1 file1 file2			#Show the first matching field of the first file
join -o 1.1 2.2 file1 file2		#Display the first field of the first file and the second matching field of the second file
join -t ':' /etc/passwd /etc/shadow		#To: match splicing / etc/passwd and / etc/shadow
join -v 1 -a1 -a2 file1 file2				#Output mismatch row

7. tr command

tr, short for translate
-c. - C, - completionreplace with string in set 1, character set is required to be ASCII
-d. - delete delete string in collection 1 instead of conversion
-s. - squeeze repeats delete all repeated occurrence string sequences and keep only the first one; that is, compress the repeated occurrence string into one string
-t, -truncate-set1 delete more characters in the first character set than in the second character set

[root@localhost seq]# echo "ZJY"|tr "A-Z" "a-z"
zjy

[root@localhost seq]# echo 'hello shell python linux123'|tr -d '0-5'
hello shell python linux

[root@localhost seq]# echo 'hello shell python linux123'|tr -d -c '0-5\n'
123		# Delete characters other than numbers
[root@localhost seq]# echo '1  4    2'|tr -s ' '
1 4 2

[root@localhost seq]# echo westos | tr [:lower:] [:upper:]
WESTOS

Wildcard:

[: digit:] all numbers
 [: graph:] all printable characters excluding spaces
 [: lower:] all lowercase characters
 [: print:] all punctuation characters
 [: punct:] all horizontal or vertical blanks
 [: upper:] all uppercase letters

7. xargs command

                     . Xargs can convert pipe or standard input (stdin) data into command-line parameters, and can also read data from the output of a file. Xargs can convert single line or multi line text input to other formats, such as multi line to single line, single line to multi line. The default command of xargs is echo, which means that the input passed to xargs through the pipeline will contain line breaks and whitespace, but through xargs processing, line breaks and whitespace will be replaced by whitespace.

[root@localhost xargs]# cat test |xargs
a b c d e f g h i j k l m n
[root@localhost xargs]# cat test |xargs -n3
a b c
d e f
g h i
j k l
m n

[root@localhost xargs]# echo 'ZjyZjyZjy'|xargs -dZ
 jy jy jy

[root@localhost xargs]# echo "one two three"|xargs mkdir
[root@localhost xargs]# ls
one  test  three  two

[root@localhost xargs]# echo "xi ha"|xargs -p touch  # -p: Ask if you want to execute
touch xi ha ?...y
[root@localhost xargs]# ls
ha  one  test  three  two  xi

[root@localhost xargs]# echo "xi ha"|xargs -t rm  # Print out the execution command and execute
rm xi ha 
[root@localhost xargs]# ls
one  test  three  two

[root@localhost xargs]# find /etc/ -type f -print0|xargs -0 ls  #print0 is separated by none 

xargs -I name sh -c 'echo name;mkdir name'    #-I means passed to multiple parameters
Published 104 original articles, won praise 1, visited 2287
Private letter follow

Tags: Python shell ascii Linux

Posted on Fri, 14 Feb 2020 06:55:39 -0500 by psych0