M20 – 1- 第二周(2):課堂練習與作業

課堂練習:

1、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現至少一位數字的文件和目錄

[root@centos6 ~]# ls /var/l*[[:digit:]]*[[:lower:]]
ls: cannot access /var/l*[[:digit:]]*[[:lower:]]: No such file or directory
[root@centos6 ~]# touch /var/l5x
[root@centos6 ~]# ls /var/l*[[:digit:]]*[[:lower:]]
/var/l5x

2、顯示/etc目錄下以任意一位數字開頭,且以非數字結尾的文件或目錄

[root@centos6 ~]# ls /etc/[[:digit:]]*[^[:digit:]]
ls: cannot access /etc/[[:digit:]]*[^[:digit:]]: No such file or directory
[root@centos6 ~]# touch /etc/7abc
[root@centos6 ~]# ls /etc/[[:digit:]]*[^[:digit:]]
/etc/7abc

3、顯示/etc目錄下以非字母開頭,后面跟了一個字母及其它任意長度任意字符的文件和目錄

[root@centos6 ~]# ls -d /etc/[^[:alpha:]][[:alpha:]]*
/etc/7abc

4、顯示/etc目錄下所有以m開頭以非數字結尾的文件或目錄

[root@centos6 ~]# ls -d /etc/m*[^0-9]
/etc/magic    /etc/mail.rc    /etc/man.config  /etc/mime.types   /etc/modprobe.d  /etc/mtab         /etc/my.cnf
/etc/mailcap  /etc/makedev.d  /etc/maven       /etc/mke2fs.conf  /etc/motd        /etc/mtools.conf

5、顯示/etc目錄下,所有以.d結尾的文件或目錄

[root@centos6 ~]# ls -d /etc/*.d
/etc/bash_completion.d  /etc/init.d         /etc/modprobe.d      /etc/rc0.d  /etc/rc6.d          /etc/setuptool.d
/etc/chkconfig.d        /etc/latrace.d      /etc/oddjobd.conf.d  /etc/rc1.d  /etc/rc.d           /etc/statetab.d
/etc/cron.d             /etc/ld.so.conf.d   /etc/pam.d           /etc/rc2.d  /etc/request-key.d  /etc/sudoers.d
/etc/depmod.d           /etc/logrotate.d    /etc/popt.d          /etc/rc3.d  /etc/rsyslog.d      /etc/sysctl.d
/etc/dnsmasq.d          /etc/lsb-release.d  /etc/prelink.conf.d  /etc/rc4.d  /etc/rwtab.d        /etc/xinetd.d
/etc/dracut.conf.d      /etc/makedev.d      /etc/profile.d       /etc/rc5.d  /etc/sane.d         /etc/yum.repos.d

6、顯示/etc目錄下,所有以.conf結尾,且以m,n,r,p開頭的文件或目錄

[root@centos6 ~]# ls -d /etc/[mnrp]*.conf
/etc/mke2fs.conf    /etc/nsswitch.conf  /etc/pm-utils-hd-apm-restore.conf  /etc/readahead.conf    /etc/rsyslog.conf
/etc/mtools.conf    /etc/ntp.conf       /etc/pnm2ppa.conf                  /etc/request-key.conf
/etc/nfsmount.conf  /etc/pbm2ppa.conf   /etc/prelink.conf                  /etc/resolv.conf

作業:

1、將/etc/issue文件中的內容轉換為大寫后保存至/tmp/issue.out文件中

[root@centos6 ~]# cat /etc/issue | tr 'a-z' 'A-Z' > /tmp/issue.out
[root@centos6 ~]# cat !$
cat /tmp/issue.out
CENTOS RELEASE 6.8 (FINAL)
KERNEL \R ON AN \M
TTY IS \L
DATE IS \D
HOSTNAME IS \N

2、將當前系統登錄用戶的信息轉換為大寫后保存至/tmp/who.out文件中

[root@centos6 ~]# who | tr 'a-z' 'A-Z' > /tmp/who.out
[root@centos6 ~]# cat !$
cat /tmp/who.out
ROOT     PTS/0        2016-07-30 19:26 (172.18.19.236)

3、一個linux用戶給root發郵件,要求郵件標題為“help”,郵件正文如下:

Hello,I am 用戶名,the system version is here,pleasehelp me to check it,thanks!

[root@centos6 ~]# mail -s help wang
Hello, I am wang, the system version is here, pleasehelp me to check it, thanks!
.
EOT
[root@centos6 ~]# su - wang
[wang@centos6 ~]$ mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/wang": 2 messages 2 new
>N  1 root                  Sat Jul 30 20:59  18/662   "help"
 N  2 root                  Sat Jul 30 20:59  18/662   "help"
& 1
Message  1:
From root@centos6.localdomain  Sat Jul 30 20:59:41 2016
Return-Path: <root@centos6.localdomain>
X-Original-To: wang
Delivered-To: wang@centos6.localdomain
Date: Sat, 30 Jul 2016 20:59:41 +0800
To: wang@centos6.localdomain
Subject: help
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: root@centos6.localdomain (root)
Status: R

Hello, I am wang, the system version is here, pleasehelp me to check it, thanks!

&

4、將/root/下文件列表,顯示成一行,并以文件名之間用空格隔開

[root@centos6 ~]# ls -a | tr '\n' ' '
. .. .abrt anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc bc .cache .config .cshrc .dbus dead.letter 
Desktop Documents Downloads edrawmax-cn.exe .esd_auth file1 .gconf .gconfd .gnome2 .gnote .gnupg .gtk-bookmarks .gvfs .ICEauthority .imsettings.log install.log install.log.syslog .local Music .nautilus Pictures Public .pulse .pulse-cookie root.out .ssh .tcshrc Templates tr Videos .viminfo [root@centos6 ~]#

5、file文件的內容為:“1 2 3 4 5 6 7 8 9 10” 計算出所有數字的總和

[root@centos6 ~]# cat file1 
1 2 3 4 5 6 7 8 9 10
[root@centos6 ~]# cat file1 |xargs|sed 's/ /+/g'|bc
55
[root@centos6 ~]# cat file1
1 2 3 4 5 6 7 8 9 10
[root@centos6 ~]# cat file1|tr ' ' '+' |bc
55

6、刪除Windows文本文件中的"^M"字符

[root@centos6 ~]# cat -A file4.txt 
a^M$
b^M$
c^M$
d^M$
f^M$
g[root@centos6 ~]# tr -d '\r' <  file4.txt > file5.txt
[root@centos6 ~]# cat -A file5.txt 
a$
b$
c$
d$
f$
g[root@centos6 ~]#

7、處理字符串“xt.,1 1 jr#!$mn2 c*/fe3 uz4 ”,只保留其中的數字和空格

[root@centos6 ~]# echo "xt.,1 1 jr#bcmn2 c*/fe3 uz4" | tr -d '[[:alpha:]][[:punct:]]'
1 1 2 3 4

8、將PATH變量每個目錄顯示在獨立的一行

[root@centos6 ~]# echo $PATH |tr ':' '\012'
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/root/bin

9、刪除指定文件的空行

[root@centos6 ~]# cat file2
a

b

c
         
[root@centos6 ~]# sed -i '/^$/d' file2
[root@centos6 ~]# cat file2
a
b
c

10、將文件中每個單詞(字母)顯示在獨立的一行,并無空行

[root@centos6 ~]# cat file6
how are you! how old are you!
[root@centos6 ~]# cat file6 |tr ' ' '\n'
how
are
you!
how
old
are
you!

原創文章,作者:Aleen,如若轉載,請注明出處:http://www.www58058.com/26882

(0)
AleenAleen
上一篇 2016-08-02 10:58
下一篇 2016-08-02 10:58

相關推薦

  • ?實時數據同步方案{ rsync; inotify; }

    實時數據同步方案{ rsync; inotify; } rsync 傳輸數據 安裝 rsyncd 服務 CentOS 6 CentOS 7 配置 rsyncd 服務 配置 rsync 手動同步示例     啟動 rsyncd 服務 CentOS 6 CentOS 7 rsync 客戶端命令 rsync 數據傳輸功能 基…

    Linux干貨 2016-10-29
  • ubuntu kylin虛擬機安裝KVM

    1.確定物理機CPU是否支持虛擬化    查看方法1:計算機->屬性->處理器(Inter(R) Core(TM) i5-2450M),然后去網上查看自己的電腦CPU是否支持虛擬化,    地址http://ark.intel.com/zh-cn/找到自己CPU的信息,虛擬化技術后是否顯示為yes,如果是n…

    Linux干貨 2016-10-15
  • 馬哥教育網絡班22期第一周課程練習1-3天

    1.描述計算機組成及其功能 CPU:  運算器  控制器  寄存器  緩存 存儲器:  RAM(隨機訪問存儲器)  I/O設備:               i:下指令,提供數據 如:鍵鼠     &…

    Linux干貨 2016-08-15
  • 文本處理工具之awk

                    文本處理工具之AWK 一、linux文本處理工具三劍客:grep 、sed、 awk。其中grep是一種文本過濾器,sed是文本行編輯器,而awk是一種報表生成器,就是對文件進行…

    2017-05-21
  • 用戶組和權限管理的相關命令

    1,useradd  新建用戶 useradd -u uid username 創建用戶時指定uid(如不指定則Centos:500-60000, Centos7: 1000-60000) -o -u uid username  創建用戶時指定uid(可以與已存在用戶的uid相同) -g groupname username 創…

    2017-07-25
  • PHP異常處理詳解

    PHP異常處理詳解          異常處理(又稱為錯誤處理)功能提供了處理程序運行時出現的錯誤或異常情況的方法。   異常處理通常是防止未知錯誤產生所采取的處理措施。異常處理的好處是你不用再絞盡腦汁去考慮各種錯誤,這為處理某一類錯誤提供了一個很有效的方法,使編程效率大大提高。當異常被觸發時,通常會發生:&…

    Linux干貨 2015-06-02
欧美性久久久久