linux 中删除文本中所有的换行符
001、tr实现
a、
[root@pc1 test02]# ls a.txt [root@pc1 test02]# cat a.txt ## 测试文件 01 02 03 04 05 06 07 08 09 10 [root@pc1 test02]# cat a.txt | tr "\n" " " ## 删除所有的换行符 01 02 03 04 05 06 07 08 09 10 [root@pc1 test02]# cat a.txt | tr "\n" " " | sed 's/ $//' ## 删除末尾的空格 01 02 03 04 05 06 07 08 09 10[root@pc1 test02]#
b、
[root@pc1 test]# ls a.txt [root@pc1 test]# cat a.txt ## 测试文件 01 02 03 04 05 06 07 08 09 10 [root@pc1 test]# cat a.txt | tr -d "\n" ## 直接删除换行符 01 0203 0405 0607 0809 10[root@pc1 test]#
c、
[root@pc1 test]# ls a.txt [root@pc1 test]# cat a.txt 01 02 03 04 05 06 07 08 09 10 [root@pc1 test]# cat a.txt | tr "\n" "\0" ## 其中\0表示空白字符NULL,或者ASCII码中的0 01 0203 0405 0607 0809 10[root@pc1 test]#
002、awk实现
[root@pc1 test02]# ls a.txt [root@pc1 test02]# cat a.txt ## 测试数据 01 02 03 04 05 06 07 08 09 10 [root@pc1 test02]# awk '{printf("%s ", $0)}' a.txt ## 删除末尾的空格 01 02 03 04 05 06 07 08 09 10 [root@pc1 test02]#
003、sed实现, 会保留最后的一个空格
[root@pc1 test02]# ls a.txt [root@pc1 test02]# cat a.txt ## 测试数据 01 02 03 04 05 06 07 08 09 10 [root@pc1 test02]# sed ':t; N; s/\n/ /; b t' a.txt ## 删除所有的换行符 01 02 03 04 05 06 07 08 09 10
004、awk实现
[root@pc1 test02]# ls a.txt [root@pc1 test02]# cat a.txt ## 测试数据 01 02 03 04 05 06 07 08 09 10 [root@pc1 test02]# awk 'BEGIN{ORS = " "} {print $0}' a.txt ## 直接指定了输出的换行符 01 02 03 04 05 06 07 08 09 10 [root@pc1 test02]#
。
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-09-30 linux 中如何升级glibc 版本
2022-09-30 linux中查看GLIBC版本
2022-09-30 python3 ./gen_ldc_version_info.py > utils/ldc_version_info_.d make: *** [utils/ldc_version_info_.d] Error 1
2022-09-30 centos7中升级make到最新版本
2022-09-30 centos7如何升级GCC编辑器、G++编译器, 安装多个版本及切换
2021-09-30 c primer plus 12 编程练习
2020-09-30 beagle 填充 Exception in thread "main" java.lang.IllegalArgumentException: NaN