1. 程式人生 > 實用技巧 >5標準IO和管道-案例分析

5標準IO和管道-案例分析

1.將/etc/issue檔案中的內容轉換為大寫後儲存至/tmp/issue.out檔案中

[root@localhost etc]# tr a-z A-Z </etc/issue >/tmp/issue
[root@localhost etc]# cat /tmp/issue 
\S
KERNEL \R ON AN \M

2.將當前系統登入使用者的資訊轉換為大寫後儲存至/tmp/who.out檔案中

[root@localhost etc]# w
 17:36:52 up 3 days, 22 min,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      Mon17    3days  0.06s  0.06s -bash
root     pts/0    10.0.0.110       Tue14    4.00s  1.43s  0.29s w
[root@localhost etc]# w|tr a-z A-Z >/tmp/who.out
[root@localhost etc]# cat /tmp/who.out 
 17:38:27 UP 3 DAYS, 24 MIN,  2 USERS,  LOAD AVERAGE: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ROOT     TTY1                      MON17    3DAYS  0.06S  0.06S -BASH
ROOT     PTS/0    10.0.0.110       TUE14    3.00S  1.15S  0.00S W

3.一個linux使用者給root發郵件,要求郵件標題為”help” ,郵件正文如下:

Hello, I am 使用者名稱,The system version is here,please help me to check it ,thanks!

作業系統版本資訊

[root@localhost data]# su linux
[linux@localhost data]$ mail -s 'help' root@localhost << EOF
> Hello,I am $USER,The system version is here,please help me to check it,thanks!
> `uname -a`
> EOF
[linux@localhost data]$ exit
exit
[root@localhost data]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 2 messages 2 new
>N  1 [email protected]  Thu Aug  6 17:50  19/697   "help"
 N  2 [email protected]  Thu Aug  6 17:51  19/802   "help"
& 2
Message  2:
From [email protected]  Thu Aug  6 17:51:06 2020
Return-Path: <[email protected]>
X-Original-To: root@localhost
Delivered-To: [email protected]
Date: Thu, 06 Aug 2020 17:51:06 +0800
To: [email protected]
Subject: help
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected]
Status: R

Hello,I am linux,The system version is here,please help me to check it,thanks!
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

& exit

4.將/root/下檔案列表,顯示成一行,並檔名之間用空格隔開

[root@localhost ~]# ls -a|tr '\n' ' '
. .. acl.txt anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc .lesshst .tcshrc .viminfo .vimrc 

5.計算1+2+3+...+99+100的總和

[root@localhost ~]# echo {1..100}|tr ' ' +|bc
5050

6.刪除Windows文字檔案中的回車字元 ,即“\r”

[root@localhost data]# hexdump -C f4.txt 
00000000  61 61 61 0d 0a 62 62 62  0d 0a 62 62 62 0d 0a 63  |aaa..bbb..bbb..c|
00000010  76 63 63 0d 0a 64 64 64  0d 0a 77 65 6c 63 6f 6d  |vcc..ddd..welcom|
00000020  65 20 74 6f 20 68 69 6e  73 61 6e 67              |e to hinsang|
0000002c
[root@localhost data]# file f4.txt 
f4.txt: ASCII text, with CRLF line terminators
[root@localhost data]# tr -d '\r' < f4.txt 
aaa
bbb
bbb
cvcc
ddd
welcome to hinsang

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

[root@localhost data]# echo 'xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4'|tr -dc '[[:digit:]][[:space:]]'
 1  2  3  4

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

[root@localhost ~]# echo $PATH|tr ':' '\n'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin

9.將指定檔案中0-9分別替代成a-j

[root@localhost data]# seq 0 9 > f1.txt
[root@localhost data]# cat f1.txt
0
1
2
3
4
5
6
7
8
9
[root@localhost data]# cat f1.txt|tr '0-9' 'a-j'
a
b
c
d
e
f
g
h
i
j

10.將檔案/etc/centos-release中每個單詞(由字母組成)顯示在獨立一行,並無空行

[root@localhost ~]# cat /etc/centos-release|tr -d '\n'| tr ' ' '\n'
CentOS
Linux
release
7.6.1810
(Core)