本文共 5188 字,大约阅读时间需要 17 分钟。
功能描述:在文件中搜寻字串匹配的行并输出
功能描述:按照分割符分割文件,并输出指定的列。
局限:如果分隔符长度不一致,无法通过cut分割[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
bash中只有printf 命令,awk除了printf还有print命令,print默认在后面添加换行符。
printf ‘输出类型输出格式’ 输出内容
[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{动作1}条件2{动作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 是一种几乎包括在所有 UNIX 平台(包括 Linux)的轻量级流编辑器。sed主要是用来将数据进行选取、替换、删除、新增的命令
sed [选项] ‘[动作]’ 文件名
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 [选项] 文件名
# 排序用户信息文件[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 [选项] 文件名
转载地址:http://fwsoi.baihongyu.com/