1.4. 2017-11-09-练习-head-tr-管道

1.4.1. 练习1-tr,cut,sort,tail

1、 将/etc/passwd 文件的前5行内容转化为大写后保存到/tmp/passwd.out文件

[root@centos6 dirtest]# head -n 5 /etc/passwd |tr 'a-z' 'A-Z' > /tmp/passwd.out

2、 将登陆到当前系统的用户信息的后3行信息转化为大写保存到/tmp/who.out文件中

[root@centos6 dirtest]# who |tail -n 3  | tr 'a-z' 'A-Z' > /tmp/who.out

3、 取出/etc/passwd 文件中的第6行至第10行,并将这些信息按第3个字段数值进行排序,最后显示进显示第一个字段

[root@centos6 ~]# head -n 10 /etc/passwd | tail -n 5 | sort -t ":" -k 3 | cut -d ":" -f 1

1.4.2. 练习2-cat,who,seq

1、将/etc/issue 文件中的内容转换为大写后保存至/tmp/issue.out 文件中

[root@centos7 app]# cat /etc/issue |tr "a-z" "A-Z" >/tmp/issue.out

2 、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out 文件中

[root@centos7 app]# who| tr "a-z" "A-Z" > /tmp/who.out

3 、一个linux 用户给root 发邮件,要求邮件标题为”help” ,邮件正文如下:Hello, I am 用户名,The system version is here,please help me tocheck it ,thanks! 操作系统版本信息

[root@centos7 app]# echo "hello,I am $USER, The system version is $(uname -r), please help me to check it ,thanks" | mail -s "help" root

4 、将/root/ 下文件列表,显示成一行,并文件名之间用空格隔开

[root@centos7 app]# ls -1 /root  |tr "\n" " "

5 、计算1+2+3+..+99+100 的总和

[root@centos7 app]# seq -s + 1 100 |bc
5050
[root@centos7 app]# echo {1..100} |tr " " "+" |bc
5050

6 、删除Windows 文本文件中的‘^M’ 字符

[root@centos7 app]# cat -A win.txt
zhaojiedi ^M$
hello world^M$
end^M$
[root@centos7 app]# dos2unix win.txt
dos2unix: converting file win.txt to Unix format ...
[root@centos7 app]# cat -A win.txt
zhaojiedi $
hello world$
end$

7 、处理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4 ”,只保留其中的数字和空格

[root@centos7 app]# echo 'xt.,l 1 jr#win.txtmn 2 c*/fe 3 uz 4 ' | tr -cd "[0-9] "

8 、将PATH 变量每个目录显示在独立的一行

[root@centos7 app]# echo $PATH |tr ":" "\n"
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin

9、 将 指定文件中0-9 分别替代成a-j

[root@centos7 app]# cat /etc/fstab |tr [0-9] [a-j]
#
# /etc/fstab
# Created by anaconda on Tue Nov  a aa:aa:aa aaaa
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(a), findfs(a), mount(a) and/or blkid(a) for more info
#
UUID=aacceaaa-acae-abbc-aeaf-afadbadcfaea /                       exta    defaults        a a
UUID=faeaaaea-bcde-aaaa-adca-caabaaaaeaaf /app                    exta    noatime        a a
UUID=adacbeaa-ffba-aadf-bacf-aabeafaaaaaa /boot                   exta    defaults        a a
UUID=bacaaafa-aeea-abac-aeaa-edaacbaacaaa swap                    swap    defaults        a a

10 、将/etc/centos-release文件中每个单词(由字母组成)显示在独立的一行,并无空行

[root@centos7 app]# cat /etc/centos-release | tr -sc "a-zA-Z " "\n"