`

Bash sed 读和写文件入门

 
阅读更多

Bash sed 读和写文件入门

一、read

‘[position line]r source.txt’ target.txt

Find the line from target.txt through position line, and then read all lines from source.txt, at last put all lines after the line position in target.txt.

If the position line is not found, just put out target.txt

If position line is not supplied, put all lines in source.txt after every line of the target.txt.

[braveyly@m-net ~]$ cat source.txt

a

b

c

[braveyly@m-net ~]$ cat target.txt

1

2

3

[braveyly@m-net ~]$ sed '/2/r target.txt' source.txt

a

b

c

[braveyly@m-net ~]$ sed '/2/r source.txt' target.txt

1

2

a

b

c

3

[braveyly@m-net ~]$ sed 'r source.txt' target.txt

1

a

b

c

2

a

b

c

3

a

b

c

[braveyly@m-net ~]$ sed '1r source.txt' target.txt

1

a

b

c

2

3

二、write

‘[position line]w target.txt’ source.txt

Read lines from source.txt through position line. And then put out all these lines into target.txt.

If the target.txt file is not existed, then create it.

[braveyly@m-net ~]$ cat meeting.txt

9:00 VoIP LING 2

10:00 IAD MEAT 2

9:12 VoIP TYU 3

12:00 VoIP QWE 4

[braveyly@m-net ~]$ sed -e '/VoIP/w VoIP.txt' ./meeting.txt

9:00 VoIP LING 2

10:00 IAD MEAT 2

9:12 VoIP TYU 3

12:00 VoIP QWE 4

[braveyly@m-net ~]$ cat VoIP.txt

9:00 VoIP LING 2

9:12 VoIP TYU 3

12:00 VoIP QWE 4

[braveyly@m-net ~]$ sed -e '/IAD/w IAD.txt' ./meeting.txt

9:00 VoIP LING 2

10:00 IAD MEAT 2

9:12 VoIP TYU 3

12:00 VoIP QWE 4

[braveyly@m-net ~]$ cat IAD.txt

10:00 IAD MEAT 2

分享到:
评论

相关推荐

    shell编程 从入门到精通百度网盘下载地址.

    包括概述、linux文件系统、正则表达式、sed命令和awk编程、文件的排序、合并和分割、变量和引用、操作符、循环与结构化命令、变量的该机用法、I/O重定向、linux/unix shell类型与区别、子shell进程处理、函数、别名...

    Linux Shell编程从初学到精通 第2版

    第4章 sed命令和awk编程 64 第5章 文件的排序、合并和分割 105 第6章 变量和引用 133 第7章 退出、测试、判断及操作符 157 第8章 循环与结构化命令 187 第9章 变量的高级用法 217 第10章 I/O重定向 243 第11...

    HackerRank-The-Linux-Shell-Challenges-Solutions:Linux Shell的完整解决方案和相关教程-Bash,文本处理,Bash中的数组,Grep Sed Awk在HackerRank上的挑战

    重击挑战数字挑战性解决方案1个2个3解决方案4 用数字循环解决方案5 数字世界解决方案6 比较数字解决方案7 有条件入门解决方案8 有关条件的更多信息解决方案9 算术运算解决方案10 计算平均值解决方案11 功能和分形-...

    Shell脚本专家指南

    主要内容包括如何使小到中型的系统管理任务自动化,分析系统数据并编辑配置文件,使用bash和ksh等编写IAnux、Unix和OSX应用程序的脚本文件等。 《Shell脚本专家指南》面向中高级的shell程序员,以及需要解决日常问题...

    redhat linux教材20课程学习文档

    4.2.3 打开文件、保存文件和退出 vi 编辑器 4.3 vi 的三种模式 4.3.1 vi 的输入模式(Insert mode) 4.3.2 vi 的命令模式(Command mode) 4.3.3 vi 的底线模式(Last line mode) 4.3.4 vi 三种模式的切换 4.4 在...

    Linux C 一站式学习

    C语言入门 C语言本质 Linux系统编程 28. 文件与I/O 1. 汇编程序的Hello world 2. C标准I/O库函数与Unbuffered I/O函数 3. open/close 4. read/write 5. lseek 6. fcntl 7. ioctl 8. mmap 29. 文件系统 1. 引言 2. ...

    宋劲彬的嵌入式C语言一站式编程

    2.4. 文件和目录操作的系统函数 3. VFS 3.1. 内核数据结构 3.2. dup和dup2函数 30. 进程 1. 引言 2. 环境变量 3. 进程控制 3.1. fork函数 3.2. exec函数 3.3. wait和waitpid函数 4. 进程间通信 4.1. 管道 4.2. 其它...

    Linux-Learn:从

    CentOS 后网卡信息修改Vim 安装、配置、快捷键列表SSH 免密登录Bash 命令Bash 其他常用命令安装的 rm(删除)Sed 命令Linux 下常用压缩文件的解压、压缩Yum 下载安装包及对应依赖包Zsh 入门日常维护日常监控nmon ...

    sheli:SHell可扩展库可帮助您编写Shell脚本

    您应该已经安装了BASH才能使sheli正常工作,但是如果您不想安装bash,则可以使用以下命令轻松地更改每个lib中的shebang: find . -type f -name ' *.sh ' -exec sed -i -e ' s|#!/bin/.*sh|#!/bin/dash| ' " {}

    MobaXtermv10.5专业版.rar

    支持各种连接SSH,X11,RDP...支持Unix命令(bash,ls,cat,sed,grep,awk,rsync,…) 连接SSH终端后支持SFTP传输文件 各种丰富的插件(git/dig/aria2…) 可运行Windows或软件 原生便携版 内建多标签和多终端分屏

Global site tag (gtag.js) - Google Analytics