Linux--Detailed explanation of text processing commands--sort, uniq, tr


sort command

Overview

  • The sort command is used to sort the contents of a file and print the sorting result to standard output
  • It treats each line of the file as a unit and sorts the content of the file by line unit
  • It can also be sorted according to different data types

Grammar format

sort [选项] [参数]

cat [对象] | sort [选项]

Common options

Common options Explanation
-f When sorting, treat lowercase letters as uppercase letters (that is, ignore case), and by default uppercase letters are sorted first
-n Sort by numerical value
-r Output the sorted results in reverse order
-u Combine rows with the same content, which means that only one row of the same data is displayed, which is equivalent to uniq
-t Specify the field separator, use [Tab] to separate by default
-k Specify sort field
-o <output file> Output the sorted results to the specified file

Example

  • -n、-r
    mark
    mark
    mark
    mark
  • -f
    mark
    mark
  • -u
    mark
    mark
  • -t、-k
    mark
  • -O
    mark
    mark

uniq command

Overview

  • The uniq command is used to report or ignore consecutive repeated lines in a file
  • Often used in conjunction with the sort command

Grammar format

uniq [选项] 参数

cat [对象] | uniq 选项

Common options

Common options Explanation
-c Count and delete repeated lines in the file
-d Show only consecutive repeated rows
-u Show only lines that occur once

Example

mark

  • -c
    mark
  • -d、-u
    mark
  • Used in conjunction with the sort command
  • You can count the objects with the most repetitions, which can be applied to detect hacker attacks
    mark

tr command

Overview

  • The tr(translate) command can be used to replace, compress and delete characters from standard input

Grammar format

tr [选项] [字符集1 字符集2]

##没有选项,则默认将标准输入中所有属于字符集1 的字符替换为字符集2中的字符

Common options

Common options Explanation
-c Characters in character set 1 are reserved, and other characters (including newline \n) are replaced with character set 2
-d Delete all characters belonging to character set 1
-s Compress repetitive strings into one string; replace character set 1 with character set 2
-t Character set 2 replaces character set 1, the same result without options

parameter

  • Character set 1:
    • Specify the original character set to be converted or deleted
    • When performing the conversion operation, you must use the parameter "Character Set 2" to specify the target character set for conversion
    • But when executing the delete operation, the parameter "Character Set 2" is not required:
  • Character set 2:
    • Specify the target character set to be converted

Example

  • Case conversion
    mark
  • -c
    mark
  • -d
    mark
  • -s
    mark
    mark
  • Delete empty lines
    mark
    or
    mark
    mark
  • Replace the ":" in the path variable with a newline character "\n"
    mark
    or
    mark
  • Files transferred from Windows to Linux may have incompatibility issues, and "^M" characters will appear
cat [对象] | tr -s "\r" "\n" > [新文件对象]
or
cat [对象] | tr -d "\r" > [新文件对象]
Linux中遇到换行符("\n")会进行回车+换行的操作,回车符反而只会作为控制字符("^M")显示,不发生回车的操作
而windows中要回车符+换行符("\r\n")才会回车+换行,缺少一个控制符或者顺序不对都不能正确的另起一行

First, create a txt file on the host machine, with any content,
mark
save the file and drag it into xshell and view it
mark
mark
mark

  • Array sort
你看的没错!tr可以用来数组排序!!

mark

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/111650007