cut
cut是以每一行为一个处理对象的 1.取出第三列 [root@hadoop1 shell]# cat 1.txt waefaeg wegargserg awegagaeg wegaergwg waegae awegargew5y5rh [root@hadoop1 shell]# cat 1.txt |cut -b 3 e g e g e e [root@hadoop1 shell]# 2.取出3-5列 [root@hadoop1 shell]# cat 1.txt |cut -b 3-5 efa gar ega gae ega ega [root@hadoop1 shell]#
sort
sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出 1.去重文件内容 [root@hadoop1 shell]# sort -u 1.txt 123 144 168 436 564 890 awegagaeg awegargew5y5rh waefaeg waegae wegaergwg wegargserg 哈哈哈 [root@hadoop1 shell]# 2.将文件内容倒序 [root@hadoop1 shell]# sort -r 1.txt 哈哈哈 哈哈哈 哈哈哈 wegargserg wegaergwg waegae waefaeg awegargew5y5rh awegagaeg 890 564 436 168 144 123 3.将文件内容升序 [root@hadoop1 shell]# sort -n 1.txt 123 144 168 436 564 890 awegagaeg awegargew5y5rh waefaeg waegae wegaergwg wegargserg 哈哈哈 哈哈哈 哈哈哈 [root@hadoop1 shell]#
uniq
uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。
1.去重
[root@hadoop1 shell]#
[root@hadoop1 shell]# uniq 1.txt
waefaeg
wegargserg
awegagaeg
wegaergwg
waegae
awegargew5y5rh
哈哈哈
436
890
123
144
168
564
[root@hadoop1 shell]#
2.显示重复次数
[root@hadoop1 shell]# uniq -c 1.txt
1 waefaeg
1 wegargserg
1 awegagaeg
1 wegaergwg
1 waegae
1 awegargew5y5rh
3 哈哈哈
1 436
1 890
1 123
1 144
1 168
1 564
[root@hadoop1 shell]#
3.只显示重复的行,并显示重复的次数
[root@hadoop1 shell]# uniq -cd 1.txt
3 哈哈哈
[root@hadoop1 shell]#
4.显示所有重复的行
[root@hadoop1 shell]# uniq -D 1.txt
哈哈哈
哈哈哈
哈哈哈
[root@hadoop1 shell]#
head
取前面行,取值 1.取出前5行 [root@hadoop1 shell]# head -n 5 1.txt waefaeg wegargserg awegagaeg wegaergwg waegae [root@hadoop1 shell]# 2.或者 [root@hadoop1 shell]# head -5 1.txt waefaeg wegargserg awegagaeg wegaergwg waegae [root@hadoop1 shell]# 3.输出前25个字符 [root@hadoop1 shell]# head -c25 1.txt waefaeg wegargserg awegag[root@hadoop1 shell]# 4.默认输出前十行 [root@hadoop1 shell]# head 1.txt waefaeg wegargserg awegagaeg wegaergwg waegae awegargew5y5rh 哈哈哈 哈哈哈 哈哈哈 436 [root@hadoop1 shell]#
tail
取后面行 1.默认取后10行 [root@hadoop1 shell]# tail 1.txt awegargew5y5rh 哈哈哈 哈哈哈 哈哈哈 436 890 123 144 168 564 [root@hadoop1 shell]# 2.-f实时读取,循环读取 [root@hadoop1 shell]# tail -f 1.txt awegargew5y5rh 哈哈哈 哈哈哈 哈哈哈 436 890 123 144 168 564 3.取后5行 [root@hadoop1 shell]# tail -n5 1.txt 890 123 144 168 564 [root@hadoop1 shell]# 4.从第五行开始读文件 [root@hadoop1 shell]# tail -n +5 1.txt waegae awegargew5y5rh 哈哈哈 哈哈哈 哈哈哈 436 890 123 144 168 564 [root@hadoop1 shell]#
grep
grep是用来处理行数据的基本命令集,简单来说它的使用意义就在于当你想要找到某个文件中的某个字符串以及它相关联的信息时(比如说行数),那么你就需要这个命令了。
1.过滤出包含5的行
[root@hadoop1 shell]# grep '5' 1.txt
awegargew5y5rh
564
[root@hadoop1 shell]#
2.过滤出包含5的行,并显示行号
[root@hadoop1 shell]# cat -n 1.txt |grep 5
5 waegae
6 awegargew5y5rh
15 564
[root@hadoop1 shell]#
3.过滤出包含a的行,并忽略大小写
[root@hadoop1 shell]# cat -n 1.txt |grep -i a
1 waefaeg
2 wegargserg
3 awegagaeg
4 wegaergwg
5 waegae
6 awegargew5y5rh
16 Aareger
17 aegAa
[root@hadoop1 shell]#
4.反选,输出不匹配的行
[root@hadoop1 shell]# cat -n 1.txt |grep -v a
7 哈哈哈
8 哈哈哈
9 哈哈哈
10 436
11 890
12 123
13 144
14 168
15 564
[root@hadoop1 shell]#
5.计算匹配成功的行数
[root@hadoop1 shell]# grep -c 'a' 1.txt
8
[root@hadoop1 shell]#
6.算出a字符串出现的次数
[root@hadoop1 shell]# grep -o 'a' 1.txt |wc -l
14
[root@hadoop1 shell]#
7.查找当前目录下所有文件内容包含1的文件
[root@hadoop1 shell]# grep -r '1' .
./1.txt:123
./1.txt:144
./1.txt:168
./2.txt:111
[root@hadoop1 shell]#
8.搜索包含890或者436的行
[root@hadoop1 shell]# cat -n 1.txt|grep -e 890 -e 436
10 436
11 890
[root@hadoop1 shell]#
9.匹配任意以txt结尾的文件,查找内容包含1的文件
[root@hadoop1 shell]# grep -r '1' *.txt
1.txt:123
1.txt:144
1.txt:168
2.txt:111
[root@hadoop1 shell]#
wc
利用wc指令我们可以计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。 1.显示行数 [root@hadoop1 shell]# wc -l 1.txt 17 1.txt [root@hadoop1 shell]# 2.显示详细信息,第一个17代表行数,第二个代表单词数,第三个代表字节数 [root@hadoop1 shell]# wc 1.txt 17 17 129 1.txt [root@hadoop1 shell]# 3.两个文件同时显示统计信息 [root@hadoop1 shell]# wc 1.txt 2.txt 17 17 132 1.txt 1 1 4 2.txt 18 18 136 总用量 [root@hadoop1 shell]#
split
该指令将大文件分割成较小的文件,在默认情况下将按照每1000行切割成一个小文件。 1.将文件每6行拆分成一个文件 [root@hadoop1 shell]# split -6 1.txt [root@hadoop1 shell]# ls 1.txt 2.txt xaa xab xac [root@hadoop1 shell]#
输入一个数字打印输出
#!/bin/bash read -p "请输入一个数字" a echo "您刚才输入的数字为$a"
if判断
#!/bin/bash read -p "INPUT YOUR NUMBER: " a if [ $a -le 10 ];then echo "输入正确" else echo "输入错误" fi
if 多次判断
#!/bin/bash read -p "INPUT YOUR NUMBER: " a if [ $a -gt 100 ];then echo "输入有误请重新输入!" sh /root/4.sh elif [ $a -gt 85 ] && [ $a -le 100 ];then echo "优秀" elif [ $a -gt 70 ] && [ $a -le 85 ];then echo "最好" elif [ $a -ge 60 ] && [ $a -le 70 ];then echo "及格" else echo "不及格" fi