重定向与管道技术练习
练习
*注:文档中的/data目录为试练目录
1.将/etc/issue文件中的内容转换为大写后保存到/data/all.log文件中
[19:09:28 1;33mroot@xlh /data]#cat /etc/issue |tr 'a-z' 'A-Z' > /data/all.log
[19:10:09 1;33mroot@xlh /data]#cat all.log
\S
KERNEL \R ON AN \M
2.将当前系统登录用户的信息转换为大写后保存至/data/apple.txt
[19:12:56 1;33mroot@xlh ~]#who
geng tty2 2021-10-27 18:47 (tty2)
root pts/0 2021-10-27 19:11 (10.0.0.1)
[19:13:41 1;33mroot@xlh ~]#who | tr 'a-z' 'A-Z'
GENG TTY2 2021-10-27 18:47 (TTY2)
ROOT PTS/0 2021-10-27 19:11 (10.0.0.1)
3.一个linux用户给root发邮件,要求邮件标题为“help”,邮件正文如下:
hello,I am root(用户名),The systemctl version is here,please help me to check it ,thanks! 操作系统版本信息
[19:24:41 1;33mroot@xlh ~]#cat mail.txt
hello
I am root
The systemctl version is here,please help me to check it , thanks!
[19:25:10 1;33mroot@xlh ~]#mail -s help geng < mail.txt
第二种方法
[19:24:41 1;33mroot@xlh ~]#cat mail.txt
hello
I am root
The systemctl version is here,please help me to check it , thanks!
[19:25:57 1;33mroot@xlh ~]#cat mail.txt | mail -s help geng
4.将/root/下文件列表,显示成一行,并文件名之间用空格隔开。
[19:29:45 1;33mroot@xlh ~]#ls /root | tr '\n' ' '
anaconda-ks.cfg apple.text dead.letter Desktop dir Documents Downloads elsfk.sh home initial-setup-ks.cfg list.sh mail.rc mail.txt Music Pictures Public start.sh stop.sh Templates Videos
5,计算1+2+3+4...+99+100的总和。
第一种方法
[19:31:45 1;33mroot@xlh ~]#echo {1..100} |tr ' ' +|bc
5050
第二种方法
[19:32:50 1;33mroot@xlh ~]#seq -s+ 100 | bc
5050
6.删除windows文件文件中的回车字符,即"\r"
[19:36:54 1;33mroot@xlh ~]#file window.txt
window.txt: ASCII text, with CRLF line terminators
[19:36:58 1;33mroot@xlh ~]#cat window.txt | tr -d '\r' > /data/all.log
[19:38:18 1;33mroot@xlh ~]#cat /data/all.log
a
b
c
[19:38:23 1;33mroot@xlh ~]#file /data/all.log
/data/all.log: ASCII text
7.处理字符串“xt.1| 1 jr #!mn 2 c*/fe 3 uz 4",只保留其中的数字和空格
[19:51:04 1;33mroot@xlh ~]#echo 'xt.1| 1 jr#!mn 2 c*/fe 3 uz 4' | tr -d [:alpha:][:punct:]
1 1 2 3 4
第二种方法
[19:53:10 1;33mroot@xlh ~]#vim /data/all.log
[19:53:40 1;33mroot@xlh ~]#cat /data/all.log
xt.1| 1 jr #!mn 2 c*/fe 3 uz 4
[19:53:49 1;33mroot@xlh ~]#cat /data/all.log | tr -d [:alpha:][:punct:]
1 1 2 3 4
8.将PATH变量每个目录显示在独立的一行
[19:57:19 1;33mroot@xlh ~]#echo $PATH | tr ':' '\n'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin
9.将文件中0-9分别替代成a-j
[20:09:25 1;33mroot@xlh ~]#echo 123456789 |tr '0-9' 'a-j' > /data/all.log
[20:10:12 1;33mroot@xlh ~]#cat /data/all.log
bcdefghij
10.将文件/etc/centos-release中每个单词(又字母组合)显示独立一行,并五空行
[20:18:50 1;33mroot@xlh ~]#cat /etc/centos-release | tr -c [:alpha:] ' ' | tr -s ' ' '\n'
CentOS
Linux
release
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)