博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux-SHELL常用命令
阅读量:4187 次
发布时间:2019-05-26

本文共 5188 字,大约阅读时间需要 17 分钟。

文章目录

行截取命令–grep

功能描述:在文件中搜寻字串匹配的行并输出

语法

  • grep [选项] [指定字串] [文件]
  • 命令 | grep [选项] [指定字串]

选项介绍

  • -i 不区分大小写
  • -v 排除指定字串

列截取命令–cut

功能描述:按照分割符分割文件,并输出指定的列。

局限:如果分隔符长度不一致,无法通过cut分割

语法

  • cut [选项] 文件名
  • 命令 | cut [选项] [指定字串]

选项

  • -f 列号: 提取第几列
  • -d 分隔符: 按照指定的分隔符分割列

范例

[root@localhost ~]# vi student.txtID Name gender Mark1 Liming M 862 Sc M 903 Gao M 83[root@localhost ~]# cut -f 2 student.txt[root@localhost ~]# cut -f 2,3 student.txt[root@localhost ~]# cut -d ":" -f 1,3 /etc/passwd  # 根据:分隔,取第1,3行# 获取所有的普通用户。[root@localhost tmp]# cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1cut命令的局限:分隔符不统一,空格数量不确定[root@localhost ~]# df -h | cut -d " " -f 1,3

格式化输出命令(awk基础)-- printf

bash中只有printf 命令,awk除了printf还有print命令,print默认在后面添加换行符。

语法

printf ‘输出类型输出格式’ 输出内容

输出类型:

  • %ns: 输出字符串。n是数字指代输出几个字符
  • %ni: 输出整数。n是数字指代输出几个数字
  • %m.nf: 输出浮点数。m和n是数字,指代输出的整数位数和小数位数。如%8.2f代表共输出8位数,其中2位是小数,6位是整数。

输出格式:

  • \a: 输出警告声音
  • \b: 输出退格键,也就是Backspace键
  • \f: 清除屏幕
  • \n: 换行
  • \r: 回车,也就是Enter键
  • \t: 水平输出退格键,也就是Tab键
  • \v: 垂直输出退格键,也就是Tab键

范例

[root@localhost ~]# printf %s 1 2 3 4 5 6123456[root@localhost ~]# printf %s %s %s 1 2 3 4 5 6%s%s123456[root@localhost ~]# printf '%s %s %s' 1 2 3 4 5 61 2 34 5 6[root@localhost ~]# printf '%s %s %s\n' 1 2 3 4 5 61 2 34 5 6[root@localhost ~]# vi student.txt ID Name  PHP  Linux MySQL Average1 Liming 82   95    86      87.662 Sc     74   96    87      85.663 Gao    99   83    93      91.66#不调整输出格式[root@localhost ~]# printf '%s' $(cat student.txt)IDNamePHPLinuxMySQLAverage1Liming82958687.662Sc74968785.663Gao99839391.66#调整格式输出[root@localhost ~]# printf '%s\t %s\t %s\t %s\t %s\t %s\t \n' $(cat student.txt)ID	 Name	 PHP	 Linux	 MySQL	 Average	 1	 Liming	 82	    95	     86	     87.66	 2	 Sc	     74	    96	     87	     85.66	 3	 Gao	 99	    83	     93	     91.66

awk 命令

  1. 对cut命令的扩展,分隔符长度不固定。
  2. 其他功能…

语法

awk ‘条件1{动作1}条件2{动作2}…’ 文件名

条件

  1. 关系表达式 :x>10 ,x>=10 ,x<10 …
  2. BEGIN:开始执行前操作
  3. END:执行结束后操作
  4. /正则表达式/:对双斜杠内部的字符串进行正则匹配

动作

  1. 格式化输出
  2. 流程控制语句

范例

student.txt文件printf中已介绍。

[root@localhost ~]# awk '{printf $2 "\t" $6 "\n"}' student.txt[root@localhost ~]# awk '{print $2 "\t" $6}' student.txtName	AverageLiming	87.66Sc	85.66Gao	91.66[root@localhost ~]#  df -h | awk '{print $1 "\t" $3}'Filesystem	Useddevtmpfs	0tmpfs	0tmpfs	762Mtmpfs	0/dev/mapper/centos-root	18G# 条件:BEGIN[root@localhost ~]# awk 'BEGIN{printf "This is a transcript \n" } {printf $2 "\t" $6 "\n"}' student.txtThis is a transcript Name	AverageLiming	87.66Sc	85.66Gao	91.66# 设置分隔符 :FS内置变量 ,如果不设置第一行root就会原样输出[root@localhost ~]# cat /etc/passwd | grep "/bin/bash" | \awk 'BEGIN {FS=":"} {printf $1 "\t" $3 "\n"}'root	0git	1000user1	1001# 条件:END[root@localhost ~]# awk 'END{printf "The End \n" } {printf $2 "\t" $6 "\n"}' student.txtName	AverageLiming	87.66Sc	85.66Gao	91.66The End # 条件 :关系运算[root@localhost ~]# cat student.txt | grep -v Name | \awk '$6 >= 87 {printf $2 "\n" }'LimingGao# 条件:正则匹配[root@localhost ~]# df -h /Filesystem               Size  Used Avail Use% Mounted on/dev/mapper/centos-root   97G   18G   80G  19% /[root@localhost ~]# df -h /|awk '/\//{print $4}'80G

sed 命令

sed 是一种几乎包括在所有 UNIX 平台(包括 Linux)的轻量级流编辑器。sed主要是用来将数据进行选取、替换、删除、新增的命令

语法

sed [选项] ‘[动作]’ 文件名

选项

  • -n: 一般sed命令会把所有数据都输出到屏幕,如果加入此选择,则只会把经过sed命令处理的行输出到屏幕
  • -e: 允许对输入数据应用多条sed命令编辑
  • -i: 用sed的修改结果直接修改读取数据的文件,而不是由屏幕输出

动作

  • a \: 追加,在当前行后添加一行或多行。添加多行时,除最后 一行外,每行末尾需要用“\”代表数据未完结。
  • c \: 行替换,用c后面的字符串替换原数据行,替换多行时,除最后一行外,每行末尾需用“\”代表数据未完结。
  • i \: 插入,在当期行前插入一行或多行。插入多行时,除最后 一行
    外,每行末尾需要用“\”代表数据未完结。
  • d: 删除,删除指定的行。
  • p: 打印,输出指定的行。
  • s: 字串替换,用一个字符串替换另外一个字符串。格式为“行范围s/旧字串/新字串/g”(和vim中的替换格式类似)。

范例

student.txt 结构同上

[root@localhost ~]# sed '2p' student.txtID Name PHP Linux MySQL Average1 Liming 82 95 86 87.661 Liming 82 95 86 87.662 Sc 74 96 87 85.663 Gao 99 83 93 91.66# -n一般和p一起使用,代表查看哪一行[root@localhost ~]# sed -n '2p' student.txt1 Liming 82 95 86 87.66# 删除第二行到第四行的数据,但不修改文件本身[root@localhost ~]# sed '2,4d' student.txtID Name PHP Linux MySQL Average# 在第二行后追加hello[root@localhost ~]# sed '2a hello' student.txtID Name PHP Linux MySQL Average1 Liming 82 95 86 87.66hello2 Sc 74 96 87 85.663 Gao 99 83 93 91.66# 在第二行前插入两行数据[root@localhost ~]# sed '2i hello \world' student.txtID Name PHP Linux MySQL Averagehello world1 Liming 82 95 86 87.662 Sc 74 96 87 85.663 Gao 99 83 93 91.66# 数据替换,替换整行[root@localhost tmp]# sed '2c No such person' student.txtID Name PHP Linux MySQL AverageNo such person2 Sc 74 96 87 85.663 Gao 99 83 93 91.66# 数据替换,替换字符串# 在第三行中,把74换成99[root@localhost tmp]# sed '3s/74/99/g' student.txt ID Name PHP Linux MySQL Average1 Liming 82 95 86 87.662 Sc 99 96 87 85.663 Gao 99 83 93 91.66# sed操作的数据直接写入文件: -i参数,一般不建议使用[root@localhost tmp]# sed -i '3s/74/99/g' student.txt 无输出# 同时把“Liming”和“Gao”替换为空:-e:多条sed命令[root@localhost tmp]# sed -e 's/Liming//g ; s/Gao//g' student.txt ID Name PHP Linux MySQL Average1  82 95 86 87.662 Sc 74 96 87 85.663  99 83 93 91.66

排序命–sort

语法

sort [选项] 文件名

选项介绍

  • -f: 忽略大小写
  • -n: 以数值型进行排序,默认使用字符串型排序
  • -r: 反向排序
  • -t: 指定分隔符,默认分隔符是制表符
  • -k n[,m]:按照指定的字段范围排序。 从第n个字段开始,m字段结束(默认到行尾)

范例

# 排序用户信息文件[root@localhost ~]# sort /etc/passwd# 反向排序[root@localhost ~]# sort -r /etc/passwd# 指定分隔符是“:”,用第三字段开头,第三字段结尾排序,就是只用第三字段排序[root@localhost ~]# sort -t ":" -k 3,3 /etc/passwd[root@localhost ~]# sort -n -t ":" -k 3,3 /etc/passwd

统计命令–wc

语法

wc [选项] 文件名

选项介绍

  • -l: 只统计行数
  • -w: 只统计单词数
  • -m: 只统计字符数

转载地址:http://fwsoi.baihongyu.com/

你可能感兴趣的文章
为何不使用spring、struts2、easyjweb等开源框架
查看>>
Eclipse 误删文件怎么办
查看>>
EasyJF开源从网上走到网下
查看>>
让spring帮助你在MVC层解决JPA的缓迟加载问题
查看>>
在EasyJWeb使用spring容器
查看>>
EasyJWeb中灵活的多国语言支持
查看>>
理想·环境·开源
查看>>
EasyJWeb中缺省URL映射转换器揭密
查看>>
EasyJWeb+prototype
查看>>
EasyJF开源重组见闻-1
查看>>
基于Ajax+J2EE的MicroERP源码下载
查看>>
在EasyJWeb中轻松开发Ajax运用
查看>>
EasyJWeb中的代码生成体验
查看>>
EasyDBO-0.9.1版发布
查看>>
开源2007,我们来了
查看>>
EasyJWeb-0.8.0版本发布
查看>>
Velocity中文
查看>>
开源与“黑客”入侵
查看>>
谈新手修练J2EE武功及学SSH的方法
查看>>
一个程序员充实的2006
查看>>