Shell sort, uniq, tr, cut, commands and regular expressions

One, sort command

Sort the contents of the files by row, or according to different data types

语法格式:
sort 选项 参数
cat file | sort 选项
常用选项:
 -f:忽略大小写,默认会大写字母排在前面
 -b:忽略每行前面的空格
 -n:按照数字进行排序
 -r:反向排序
 -u:等同uniq,表示相同的数据仅显示一行,去重
 -t:指定字段分隔符,默认使用tab键分隔
 -k:指定排序字段
 -o <输出文件>:将排序后的结果转存至指定文件

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

sort -t ':' -k 3  -n /etc/passwd

Insert picture description here

用于排查占用磁盘较大的文件
du -a | sort -nr -o du.txt

Insert picture description here

Two, uniq command

Used to report or ignore consecutive repeated lines in the file, often used in conjunction with the sort command

语法格式:
uniq 选项 参数
cat file | uniq 选项

常用选项:
-c:进行计数,并删除文件中重复出现的行
-d:仅显示连续的重复行
-u:仅显示出现一次的行

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Three, tr command

Commonly used to replace, compress and delete characters from standard input

语法格式:
tr 选项 参数

常用选项:
① -c:保留字符集1的字符,其他的字符(包括换行符\n)用字符集2替换
② -d:删除所有属于字符集1的字符
③ -s:将重复出现的字符串压缩为一个字符串,用字符集2 替换 字符集1-t:字符集2 替换 字符集1,不加选项同结果

参数:
字符集1:指定要转换或删除的原字符集。当执行转换操作时,必须使用参数”字符集2“指定转换操作时,必须使用参数”字符集2“指定转换的目标字符集。但执行删除操作时,不需要参数”字符集2“

字符集2:指定要转换成的目标字符集
echo "abc" | tr 'a-z' 'A-Z'

Insert picture description here

echo abc | tr -c "ab\n" "0"

Insert picture description here

echo "abc" | tr -d "ab"

Insert picture description here

echo "abbbbbbbbbbccccccc" | tr -s "bc"

Insert picture description here

删除空行
echo -e "aa\n\n\n\n\nbb" | tr -s "\n"

Insert picture description here

把路径变量中的冒号":",替换成换行符"\n"
echo $PATH | tr -s ":" "\n"

Insert picture description here

Four, cut command

Display the specified part of the line, delete the specified field in the file

语法格式:
cut [选项] 参数
cat file | cut 选项

常用选项:
-f:通过指定哪一个字段进行提取。cut命令使用"TAB"作为默认的字符分隔符

-d:"TAB"是默认的分隔符,使用此选项可以更改为其他分隔符

–complement:此选项用于排除所指定的字段

–out-delimiter:更改输出内容的分隔符

Insert picture description here

Five, regular expressions

Usually used in judgment statements to check whether a string meets a certain format

  • Regular expressions are composed of ordinary characters and metacharacters
  • Common characters include uppercase and lowercase letters, numbers, punctuation marks and some other symbols
  • Metacharacters refer to special characters with special meaning in regular expressions. They can be used to specify the appearance of the leading character (the character before the metacharacter) in the target object.

1. Basic regular expressions

基础正则表达式常见元字符:(支持的工具:grep、egrep、sed、awk)
\ :转义字符,用于取消特殊符号的含义,例:\!、\n、\$等

^ :匹配字符串开始的位置,例:^a、^the、^#、^[a-z]
 
$ :匹配字符串结束的位置,例:word$、^$匹配空行

. :匹配除\n之外的任意的一个字符,例:go.d、g..d

* :匹配前面子表达式0次或者多次,例:goo*d、go.*d

[list] :匹配list列表中的一个字符,例:go[ola]d,[abc][a-z][a-z0-9][0-9]匹配任意一位数字

[^list] :匹配任意非list列表中的一个字符,例:[^0-9][^A-Z0-9][^a-z]匹配任意一位非小写字母

\{
    
    n\} :匹配前面的子表达式n次,例:go\{
    
    2\}d、'[0-9]\{2\}'匹配两位数字

\{
    
    n,\} :匹配前面的子表达式不少于n次,例:go\{
    
    2,\}d、'[0-9]\{2,\}'匹配两位及两位以上数字

\{
    
    n,m\} :匹配前面的子表达式n到m次,例:go\{
    
    2,3\}d、'[0-9]\{2,3\}'匹配两位到三位数字

注:egrep、awk使用{
    
    n}{
    
    n,}{
    
    n,m}匹配时“{
    
    }”前不用加“\”

2. Extended regular expressions

扩展正则表达式元字符:(支持的工具:egrep、awk)
+ :匹配前面子表达式1次以上,例:go+d,将匹配至少一个o,如god、good、goood等

? :匹配前面子表达式0次或者1次,例:go?d,将匹配gd或god

() :将括号中的字符串作为一个整体,例1:g(oo)+d,将匹配oo整体1次以上,如good、gooood等

| :以或的方式匹配字条串,例:g(oo|la)d,将匹配good或者glad

Guess you like

Origin blog.csdn.net/IHBOS/article/details/114744193