0
点赞
收藏
分享

微信扫一扫

数据排序算法

泠之屋 03-22 16:30 阅读 1

linux基本命令命令

date

date [-d yyyyMMdd] [format]

[root@root]# date +%Y-%m-%d\\ %H:%M:%S
2021-05-11 12:27:42

#新建一个文件以当前时间命名的文件
touch $(date +'%Y-$m-%d_%H-%M-%S').txt

#从NTP服务器同步时间
ntpdate cn.pool.ntp.org

ls

选项信息
-a显示所有文件,包含隐藏文件
-l长列表显示,每行显示文件或目录信息
-h使用字节,千字节,兆字节等显示文件大小
-X根据文件的扩展名对文件进行排序
-R递归显示子目录的内容
-S文件大小排序
# 用于查看文件大小
ls -lh
# 用于显示所有文件
ls -al
# 用于对文件名称排序
ls -Xl
# 按上次修改时间顺序排序
ls -ltr
# 查找目录下大于1MB但是小于1GB的文件
ls -lhR | egrep "[1-9]*\\.[0-9]*M" | grep ^\\-
# 根据文件大小排序,最大优先
ls -lhS

注意:ll是alias ll=‘ls -l --color=auto’
别名通过alias查看

more,less,cat,tac,head,tail

  • more用屏幕显示一个或多个文件内容,使用enter逐行移动,使用space逐页移动,q退出
  • less用于显示一个或多个文件内容,使用enter逐行移动,使用space逐页移动,g和G移动到文件首页或尾页,q退出
  • cat将多个文件的内容连接起来,且将结果显示在标准输出上
  • tac作用几乎与 cat 命令相反。 它从文件末尾开始显示文件的内容
  • head 命令用来显示文件的开头部分,-n 100表示显示100行,默认是10
  • tail用来显示文件的结尾部分,-n 100表示显示100行,默认是10,-f实时显示文件变化
# 首先显示尾行来显示日志文件
tac access.log | less

sort

它允许您以给定的顺序对命令的结果或文件的内容进行排序,包括按数字、字母顺序、按大小(KB、MB、GB)或相反的顺序

选项说明
-k指定要分隔的列,可以指定多个列
-n要求进行数字排序
-o file将排序结果保存到指定文件
-t指定分隔符,要求相应文件的内容必须以规则分隔的列内容,否则无法正确排序
-r将结果的顺序颠倒
-u排序后删除重复项。相当于sort file

grep

搜索文件中的字符串

选项说明
-i忽略搜索字符串的大小写
-v排除包含字符串的行
-w搜索准确单词

grep 命令返回包含您要查找的字符串的完整行。

  • ^ 特殊字符用于搜索行首的字符串。
  • 特殊字符 $ 用来搜索行尾的字符串。

uniq

uniq 要求在运行之前对输入文件进行排序,因为它只比较连续的行。

选项说明
-u显示只出现一次的行
-d显示至少出现2次的行
-D去除只出现一次的行
-c计算每次出现的行数
[root@VM-12-8-opencloudos logs]# cat test.log 
antoine
xavier
steven
patrick
xavier
antoine
antoine
steven
[root@VM-12-8-opencloudos logs]# sort test.log | uniq
antoine
patrick
steven
xavier
[root@VM-12-8-opencloudos logs]# sort test.log | uniq -u
patrick
[root@VM-12-8-opencloudos logs]# sort test.log | uniq -d
antoine
steven
xavier
[root@VM-12-8-opencloudos logs]# sort test.log | uniq -D
antoine
antoine
antoine
steven
steven
xavier
xavier
[root@VM-12-8-opencloudos logs]# sort test.log | uniq -c
      3 antoine
      1 patrick
      2 steven
      2 xavier
[root@VM-12-8-opencloudos logs]# sort test.log | uniq -cd
      3 antoine
      2 steven
      2 xavier

watch

选项说明
-n 5表示每隔几秒显示
# 监控文件夹的文件数
watch -n 1 'ls -l | wc -l'

# 监控文件最后三行的内容
watch -n 1 tail -n 3 access.log

awk

常见命令如下:

# 以:分割,输出第一个,   -F "xxx"
awk -F ":" '{print $1}' /etc/passwd

# 以/分割,输出第二个
tail -n 5 /etc/services | awk -F "\/" '{print $2}'

# 以tcp或udp分割,输出第一个
tail -n 5 /etc/services | awk -F "(tcp)|(udp)" '{print $1}'

# 设置变量a=123,第一行打印a,接下来的每行打印按照空格或者tab分割
tail -n 5 /etc/services | awk -v a=123 'BEGIN{print a}{print $1}'

# 设置变量
abc=1
echo ${abc}
tail -n 5 /etc/services | awk -v a=${abc} 'BEGIN{print a}{print $1}'

# 以123作为首行,以456作为尾行,BEGIN END
tail -n 5 /etc/services | awk 'BEGIN{print 123}{print $1}END{print 456}'

# 正则匹配某行并且打印,正则匹配是/开头 /结尾
cat /etc/services | awk '/[^0-9a-zA-Z]1[1-9]{2}\/tcp/ {print $0}'

# 多个与正则匹配,使用&&
cat /etc/services | awk '/[^0-9a-zA-Z]1[1-9]{2}\/tcp/ && /175/ {print $0}'

# 多个或正则匹配,使用||
cat /etc/services | awk '/[^0-9a-zA-Z]9[1-9]{2}\/tcp/ || /91{2}\/tcp/ {print $0}'

# 非正则匹配,使用!
cat /etc/services | awk '!/(tcp)|(udp)/ {print $0}'

# 多个正则匹配,为连续行,使用逗号隔开
cat /etc/services | awk '/^ntp/,/^netbios/ {print $0}'

# 设置分割分,FS表示输入的域分隔符,类似于-F ,如下三个命令输出一模一样
cat /etc/passwd | awk -F ':' '{print $1}'
cat /etc/passwd | awk 'BEGIN{FS=":"}{print $1}'
cat /etc/passwd | awk -v FS=":" '{print $1}'

# 设置以//作为记录分隔符,以%%作为输出记录分隔符。RS表示控制记录分隔符,ORS表示输出记录分隔符
echo -e "https://example.com/books/index.html\ntitle//tcp" | awk 'BEGIN{RS="\/\/";ORS="%%"}{print $0}'

# 输出浏览记录记录的域的个数。NF
head -n 5 /etc/passwd | awk -F ":" 'BEGIN{RS="\n";ORS="\n"} {print NF}'  每行输出7

# 打印倒数第二个域的字符
head -n 5 /etc/passwd | awk -F ":" 'BEGIN{RS="\n";ORS="\n"} {print $(NF-2)}'

# 打印倒数最后一个域的字符
head -n 5 /etc/passwd | awk -F ":" 'BEGIN{RS="\n";ORS="\n"} {print $NF}'

# 打印记录数(类似于序号),NR,表示已读的记录数
tail -n 5 /etc/services | awk '{print NR,$0}'
# 打印所有的行数
cat /etc/services | awk 'END{print NR}'
# 打印第200行
cat /etc/services | awk 'NR==200'
# 打印小于等于10
cat /etc/services | awk 'BEGIN{RS="\n";ORS="\n"} NR<=10 {print NR,$0}'
# 打印命令行参数个数,ARGC
awk 'BEGIN{print ARGC}' log dump long  #3
# 打印命令行的第几个参数,ARGV
awk 'BEGIN{print ARGV[0]}' log dump long # awk
awk 'BEGIN{print ARGV[1]}' log dump long # log 
awk 'BEGIN{print ARGV[2]}' log dump long # dump
# 打印内容签名加上一个ID表示第几个文件,使用ARGIND
awk '{print ARGIND,$0}' /etc/hostname /etc/resolv.conf
# 打印对应的文件名称,FILENAME
awk '{print ARGIND,FILENAME"---"$0}'
# 表示后面的正则表达式忽略大小写,IGNORECASE=1表示忽略,IGNORECASE=0表示不忽略
awk 'BEGIN{IGNORECASE=1;RS="\n";ORS="\n"} /^(SSH)|^(ftp)/ {print $0}' /etc/services

awk的内置变量如下:

ARGC               命令行参数个数
ARGV               命令行参数排列
ENVIRON            支持队列中系统环境变量的使用
FILENAME           awk浏览的文件名
FNR                浏览文件的记录数
FS                 设置输入域分隔符,等价于命令行 -F选项
NF                 浏览记录的域的个数
NR                 已读的记录数
OFS                输出域分隔符
ORS                输出记录分隔符
RS                 控制记录分隔符
(…)Grouping
$nField reference第几个字段
+±-IncrementalDecreasing自增,自减
±!Mathematical plus signMathematical minus signNegation加减非
*/%Mathematical multiplication signMathematical division signModulo operation乘除取模
inElements in an array在数组中
&&
?:Abbreviation of conditional expressions条件表达式
~Another representation of regular expressions正则表达式的另一种表示方法
!~Reverse Regular Expression反向正则表达式
ifif (condition) statement [ else statement ]
whilewhile (condition) statement
forfor (expr1; expr2; expr3) statement
break,continue
exitreturn value in the range of [0,255]
# 如下为if,else-if,else用法

# 如果是第110个域,则打印
cat /etc/services | awk '{if(NR==110) print $0}'
# 如果正则匹配
cat /etc/services | awk '{if(/^(ftp)\s|^(ssh)\s/) print $0}'
seq 1 10 | awk '{if($0==10) print $0 ; else print "False"}'
cat /etc/services | awk '{ if($1~/netbios/) {print $0} else if($2~/175/) {print "175"} else if($2~/137/) {print "137"} else {print "no"} }'
cat /etc/services | awk '{ \ 
if($1~/netbios/) 
    {print $0} 
else if($2~/175/) 
    {print "175"} 
else if($2~/137/) 
    {print "137"} 
else {print "no"} 
}'
# 如下是while案例
tail -n 2 /etc/services | awk '{ i=1;while(i<=NF){print $i;i++}}'

# 如下是for循环案例
tail -n 2 /etc/services | awk '{ for(i=1;i<=NF;i++) print $i}'
awk 'BEGIN{ for(i=1;i<=10;i++){if(i==3) {break};print i}}'
awk 'BEGIN{ for(i=1;i<=10;i++){if(i==3) {continue};print i}}'

seq 1 10 | awk '{if($0~/5/) exit "135"}'
int(expr)Truncate as an integer
sqrt(expr)Square root
rand()Returns a random number N with a range of (0,1). The result is not that every run is a random number, but that it remains the same.
srand([expr])Use “expr” to generate random numbers. If “expr” is not specified, the current time is used as the seed by default, and if there is a seed, the generated random number is used.
asort(a,b)The elements of the array “a” are reordered (lexicographically) and stored in the new array “b”, with the subscript in the array “b” starting at 1. This function returns the number of elements in the array.
asorti(a,b)Reorder the subscript of the array “a” and store the sorted subscript in the new array “b” as an element, with the subscript of the array “b” starting at 1.
sub(r,s[,t])Use the “r” regular expression to match the input records, and replace the matching result with “s”. “t” is optional, indicating a replacement for a certain field. The function returns the number of replacements - 0 or 1. Similar to sed s//
gsub(r,s[,t])Global replacement. “t” is optional, indicating the replacement of a certain field. If “t” is ignored, it indicates global replacement. Similar to sed s///g
gensub(r,s,h[,t])The “r” regular expression matches the input records and replaces the matching result with “s”. “t” is optional, indicating a replacement for a certain field. “h” represents replacing the specified index position
index(s,t)Returns the index position of the string “t” in the string “s” (the string index starts from 1). If the function returns 0, it means it does not exist
length([s])Returns the length of “s”
match(s,r[,a])Test whether the string “s” contains the string “r”. If included, return the index position of “r” within it (string index starting from 1). If not, return 0
split(s,a[,r[,seps]])Split string “s” into an array “a” based on the delimiter “seps”. The subscript of the array starts with 1.
substr(s,i[,n])Intercept the string. “s” represents the string to be processed; “i” indicates the index position of the string; “n” is the length. If you do not specify “n”, it means to intercept all remaining parts
tolower(str)Converts all strings to lowercase
toupper(str)Converts all strings to uppercase
systime()Current timestamp
strftime([format[,timestamp[,utc-flag]]])Format the output time. Converts the timestamp to a string

函数

# int函数:截断为整数。如下打印为0,123,0,123,100,-155
echo -e "qwer123\n123\nabc\n123abc123\n100.55\n-155.27" | awk '{print int($1)}'
# sqrt函数,打印为3
awk 'BEGIN{print sqrt(9)}'
# rand函数,打印0-1的随机数
awk 'BEGIN{print rand()}'
awk 'BEGIN{srand() ; print int(rand()*100)}'
#
cat /etc/passwd | awk -F ":" '{a[NR]=$1} END{anu=asort(a,b) ; for(i=1;i<=anu;i++) print i,b[i]}'

cat /etc/services | awk '/netbios/ {sub(/tcp/,"test") ; print $0 }'

sed

常见命令

###########如下是打印匹配相关命令  n
# 打印以 netbios 字符串开头的行
cat /etc/services | sed -n '/^netbios/p'
# 打印第23-26行
cat -n /etc/services | sed -n '23,26p'
# 打印奇数行
cat -n /etc/services | sed -n '1~2p'
# 打印第10行到最后一行
cat -n /etc/services | sed -n '10,$p'
# 不打印第10行到最后一行
cat -n /etc/services | sed -n '10,$!p'
# 打印匹配字符串的行号和内容
sed -n -e '/netbios/=' -e '/netbios/p' /etc/services
# 匹配字符串范围并打印
cat  /etc/services | sed -n '/^netbios/,/^imap/p'
# 打印匹配行到最后一行
cat /etc/services | sed -n '/^netbios/,$p'

#匹配并且删除  d
# 删除所有匹配udp,#开头的和所有的空行的行
sed -e '/udp/d' -e '/^#/d' -e '/^$/d' /etc/services
# 删除第10行到最后
cat -n /etc/services | sed '10,$d'
# 删除正则匹配的行
cat  /etc/services | sed -r '/(tcp)|(udp)|(^#)|(^$)/d'

# 替换 sed 's/string/replace/g' FILENAME
# 替换第44和45行ssh替换成SSH
sed -n '44,45s/ssh/SSH/gp' /etc/services
# 使用"&"符号引用字符串,在ssh后拼接-SSH
sed -n '44,45s/ssh/&-SSH/gp' /etc/services
# 使用字符串查找一行或多行,并在行范围内替换指定的字符串
sed '/ssh/s/tcp/TCP/gp' -n  /etc/services
# 连续行的字符串替换
sed '10,30s/tcp/TCP/g' /etc/services
# 多重匹配和替换
cat /etc/services | sed 's/netbios/test1/g ; s/^#//d ; s/dhcp/&t2/g'
# 用大写字母W替换单词中的一个小写字母w
echo -e "hello,world\nPOSIX" | sed -r 's/(.*)w/\1W/g'

# 使用 -e 选项多次执行
tail -n 10 /etc/services | sed  -e '1,3d' -e '/cloud/s/ping/PING/g'
tail -n 10 /etc/services | sed  '1,3d ; /cloud/s/ping/PING/g' 

# 在特定行的上方或下方添加内容(i 和 a)
# 在第三行号上方添加内容
cat /root/test.txt | sed '3i 123'
# 在指定第五行号下添加内容
cat /root/test.txt | sed '5a 123'
# 在指定匹配行上方添加行
cat /root/test.txt | sed '/tcp/iTCP'

# 替换行c
# 替换ser的行为TMP1
cat /root/test.txt | sed '/ser/c\TMP1'
# 替换第七行
cat /root/test.txt | sed '7c REPLACE'
# 替换连续文本行
cat /root/test.txt | sed '2,$c REPLACE1'
# 替换偶数行
cat /root/test.txt | sed '2~2c replace'

选项说明
-n将仅由 sed 命令处理的文本行输出到屏幕上
-e对输入的文本行数据应用多个 sed 操作命令
-f调用并执行 sed 脚本命令文件
-i修改原始文件
-r正则表达式
  • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
  • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
  • d :删除,因为是删除啊,所以 d 后面通常不接任何东东;
  • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
  • s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正则表达式!例如 1,20s/old/new/g 就是啦!

其他

mkdir /a/b #如果/a不存在则报错
mkdir -p /a/b #如果/a不存在则会创建

centos的包管理工具yum

#更新yum源方法:
wget -O /etc/yum.repos.d/CentOS-Base.repo <http://mirrors.aliyun.com/repo/Centos-7.repo>
yum clean all
yum makecache
yum -y update

#常用命令
yum install softwarename  #安装
yum remove softwarename #卸载软件
yum list softwarename #查看软件源中是否有此软件
yum list all #列出所有软件名称
yum list installed #列出已经安装的软件名称
yum list available #列出可以用yum安装的软件
yum clean all #清空yum缓存
yum search softwareinfo #根据软件信息搜索软件名字(如,使用search web搜索web浏览器)
yum whatprovides filename #在yum源中查找包含filename文件的软件包(如,whatprovides rm搜索汉含rm的软件,命令实质上是文件)

yum update #更新软件,会存在未知问题,一般不对服务器升降级
yum history #查看系统软件改变历史
yum reinstall softwarename #重新安装
yum info softwarename #查看软件信息
yum groups list #查看软件组信息
yum groups info softwarename #查看软件组内包含的软件
yum groups install softwarename #安装组件
yum groups remove softwarename #卸载组件

ubuntu的apt管理包工具

# 更新索引文件
apt-get update
# 下载安装包
apt-get install nginx
apt-get install -y nginx
apt-get install --reinstall vim # 重新下载
# 升级已安装的包
apt-get upgrade 
# 卸载软件
apt-get remove vim # 卸载软件,保留配置文件
apt-get  purge vim # 卸载软件删除配置文件
apt-get --purge remove vim # 卸载软件删除配置文件
apt-get autoremove # 删除自动安装的软件包
apt-get remove --autoremove vim # 删除时同时删除无用的依赖包
apt-get remove --autoremove --purge vim
# 清除缓存
apt-get clean
apt-get autoclean

常见的运维命令

# 查询IP地址
ifconfig
# 查询跨AZ,跨区域,机器之间延时。查看DNS解析
ping ip
# 查看CPU
top
# 为某个文件添加权限
chmod 755 aaa.txt
# 查看tcp网络状态
netstat -ntlp
# 查看磁盘空间
df -h
举报

相关推荐

0 条评论