1. diff和patch命令
diff通常比较文件的内容,patch常用来打补丁
<
表示第一个文件中的内容
>
表示第二个文件中的内容
a—表示添加add
c—表示更改change
d—表示删除delete
使用方法:
diff -u file2 file1 > file2.path # 表示以file1为准,给符file2打补丁
patch file2 file2.path
patch -b file1 file1.path # 表示保留源文件给file1打补丁,会生成file.orig文件
diff -r /etc/ /tmp/ # diff比较目录的时候,比较的结果是目录中文件的不同,而不是文件内容的不同。
2. cut命令
通常用来截取
cut -d : -f 1 passwd # -d:指定分隔符 -f:指定要截取的列
cut -d : -f 1,3 passwd # 第一列到第三列
cut -d : -f 3- passwd
cut -c 1-3 passwd # 截取前三个字符,-c指定截取的字符的位置
cut -c 1,3 passwd # 截取位置为1和3的两个字符
3. sort命令
通常用来排序
sort file # 每行的第一个单个字符排序
sort -n # 纯数字排序
sort -r # 倒叙
sort -u # 去掉重复数字
sort -o # 输出到指定文件中
sort -t # 指定分隔符
sort -k # 指定要排序的列
sort -n -k 2 -t ‘:’ test
sort -u # 显示唯一的行
sort -d # 显示重复的行
sort -c # 每行显示一次并统计重复行数
4. uniq命令
对重复字符做相应的处理
uniq -c # 统计出现的次数
uniq -d # 显示重复的行
uniq -u # 显示不重复的行
练习:
文件内容
#每列的信息:姓名 身高 年龄 工资
lisi 170 25 6000
zjy 156 23 10000
ljl 175 24 15000
wtq 165 26 10000
wae 180 25 8000
按照员工姓名进行排序
[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
按照员工升高进行排序:
[root@localhost sort]# sort -k 2 -n test
# 每列的信息:姓名 身高 年龄 工资
zjy 156 23 10000
wtq 165 26 10000
lisi 170 25 6000
ljl 175 24 15000
wae 180 25 8000
5. seq命令
squeue是一个序列的缩写,主要用来输出序列化的东西
用法:seq [选项]…尾数
或 seq [选项]…首数 尾数
或seq [选项]…首数 增量 尾数 ,以指定增量从首数开始打印数字到尾数。
-f,–format=格式 使用printf样式的符点格式
-s,–separator=字符串 使用指定字符串分隔数字(默认使用:\n)
-w,–equal-width 在列前添加0使得宽度相同【自动补位】
–help 显示此帮助信息并退出
–version 显示版本信息并退出
[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命令
拼接不同的内容(行拼接)
join -t : file1 file2 # 以:分隔符拼接
join -a2 file1 file2 # 显示file2中的匹配的内容和没有匹配的内容(没有匹配的内容为空)
join -a1 -a2 file1 file2 #显示file1和file2的所有记录
join -o 1.1 file1 file2 #显示第一个文件的第一个匹配的字段
join -o 1.1 2.2 file1 file2 #显示第一个文件的第一个字段和第二个文件的第二个匹配字段
join -t ‘:’ /etc/passwd /etc/shadow #以:匹配拼接/etc/passwd和/etc/shadow
join -v 1 -a1 -a2 file1 file2 #输出不匹配行
7. tr命令
tr,translate的简写
-c,-C,-complement 用集合1中的字符串替换,要求字符集为ASCII
-d,-delete 删除集合1中的字符串而不是转换
-s,-squeeze-repeats 删除所有重复出现字符串序列,只保留第一个;即将重复出现字符串压缩为一个字符串
-t,-truncate-set1 先删除第一个字符集较第二个字符集多出的字符
[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 # 删除除数字外的字符
[root@localhost seq]# echo '1 4 2'|tr -s ' '
1 4 2
[root@localhost seq]# echo westos | tr [:lower:] [:upper:]
WESTOS
通配符:
[:digit:]所有的数字
[:graph:]所有可打印字符 不包括空格
[:lower:]所有的小写字符
[:print:]所有的标点字符
[:punct:]所有的横向或纵向的空白
[:upper:]所有大写字母
7. xargs命令
xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。xargs可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。xargs可以将单行或多行文本输入转换为其它格式,例如多行变单行,单行变多行。xargs默认的命令时echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。
[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:表示询问你是否执行
touch xi ha ?...y
[root@localhost xargs]# ls
ha one test three two xi
[root@localhost xargs]# echo "xi ha"|xargs -t rm # 打印出执行命令,然后执行
rm xi ha
[root@localhost xargs]# ls
one test three two
[root@localhost xargs]# find /etc/ -type f -print0|xargs -0 ls #print0表示以none分隔
xargs -I name sh -c ‘echo name;mkdir name’ #-I表示传递给多个参数