1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@local ~]# cat /etc/issue |tr "[[:lower:]]" "[[:upper:]]" > /tmp/issue.out [root@local ~]# cat /tmp/issue.out CENTOS RELEASE 6.8 (FINAL) KERNEL \R ON AN \M
2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
[root@local ~]# finger $(whoami) | tr "[[:lower:]]" "[[:upper:]]" > /tmp/who.out [root@local ~]# cat /tmp/who.out LOGIN: ROOT NAME: ROOT DIRECTORY: /ROOT SHELL: /BIN/BASH ON SINCE FRI JUL 29 23:41 (EDT) ON TTY1 6 HOURS 55 MINUTES IDLE ON SINCE SAT JUL 30 05:55 (EDT) ON PTS/1 FROM 10.1.24.1 NO MAIL. NO PLAN.
3、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下: Hello, I am 用户名,the system version is here,pleasehelp me to check it ,thanks! 操作系统版本信息
[root@local ~]# echo "Hello, I am $(whoami),the system version is here,pleasehelp me to check it ,thanks $(cat /etc/issue)" |mail -s "help" gentoo [root@local ~]# su - gentoo [gentoo@local ~]$ mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/gentoo": 2 messages 1 new 1 root Sat Jul 30 05:58 20/670 "help" >N 2 root Sat Jul 30 06:43 19/659 "help" & 2 Message 2: From root@local.linux Sat Jul 30 06:43:54 2016 Return-Path: <root@local.linux> X-Original-To: gentoo Delivered-To: gentoo@local.linux Date: Sat, 30 Jul 2016 06:43:53 -0400 To: gentoo@local.linux Subject: help User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: root@local.linux (root) Status: R Hello, I am root,the system version is here,pleasehelp me to check it ,thanks CentOS release 6.8 (Final) Kernel \r on an \m
4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开
[root@local ~]# ls / >1.txt [root@local ~]# tr '\n' ' ' <1.txt bin boot dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys test.txt test.txt~ tmp usr var
5、file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和
[root@local ~]# echo "1 2 3 4 5 6 7 8 9 10" | xargs | sed -r "s# #+#g" | bc 55 [root@local ~]# echo "1 2 3 4 5 6 7 8 9 10" > 1.txt [root@local ~]# tr " " "+" < 1.txt |bc 55
6、删除Windows文本文件中的'^M'字符
首先在windows操作系统下任意创建一个txt文本文件,在里面任意输入一些字符
然后通过xftp 将txt文本文件导入Linux系统。
[root@local ~]# cat -A test.txt 123123dsa ^M$ dasd^M$ d^M$ asd^M$ as sa^M$ ^M$ dasdasda [root@local ~]# tr -d "^M" <test.txt 123123dsa dasd d asd as sa dasdasda
7、处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
[root@local ~]# echo " xt.,l 1 jr#test.txtmn2 c*/fe3 uz4" | tr -d '[[:punct:]][[:alpha:]]' 1 2 3 4
8、将PATH变量每个目录显示在独立的一行
[root@local ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@local ~]# echo $PATH | tr ":" "\n" /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /root/bin
9、删除指定文件的空行
[root@local ~]# cat > 123.txt << END > 1231231 > > > dasd > dsadx > dcxc > > > dsa > x > dsa > > > > .. > !@#! > > > 12312 > END [root@local ~]# cat -A 1 123.txt 1.txt [root@local ~]# cat -A 123.txt 1231231$ $ $ dasd$ dsadx$ dcxc$ $ $ dsa$ x$ dsa$ $ $ $ ..$ !@#!$ $ $ 12312$ [root@local ~]# tr -d "\n" < 123.txt 1231231 dasd dsadx dcxc dsa x dsa .. !@#! 1231
10、将文件中每个单词(字母)显示在独立的一行,并无空行
[root@local ~]# cat /etc/issue | tr ' ' '\n' | tr -s '\n' CentOS release 6.8 (Final) Kernel \r on an \m
原创文章,作者:fighter,如若转载,请注明出处:http://www.178linux.com/27001