博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unix Sed Tutorial4 : How To Write to a File Using Sed
阅读量:2218 次
发布时间:2019-05-08

本文共 2760 字,大约阅读时间需要 9 分钟。

This article is part of Unix Sed Tutorial series. In previous articles, we discussed about  ,  and 

In this article, let us review how to extract part of one file and write it to another file using sed.

Sed provides “w” command to write the pattern space data to a new file.

Sed creates or truncates the given filename before reads the first input line and it writes all the matches to a file without closing and re-opening the file.

Syntax: #sed 'ADDERSSw outputfile' inputfilename#sed '/PATTERN/w outputfile' inputfilename

Sed reads a line and place it in a pattern buffer and writes the pattern buffer to the given output file according to the supplied commands.

Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.

 
# cat thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

Let us review some examples of write command in sed.

1. Write 1st line of the file

In this example, 1 (address) refers the first line of the input and w writes the pattern buffer to the output file “output.txt”

$ sed -n '1w output.txt' thegeekstuff.txt$ cat output.txt1. Linux - Sysadmin, Scripting etc.

2. Write first & last line of the file

In this example, 1 and $ refers first and last line respectively.

$ sed -n -e '1w output.txt' -e '$w output.txt' thegeekstuff.txt$ cat output.txt1. Linux - Sysadmin, Scripting etc.10.Windows- Sysadmin, reboot etc.

3. Write the lines matches with the pattern Storage or Sysadmin

In this example sed command writes the lines which matches the pattern “Storage” or “Sysadmin”.

$ sed -n -e '/Storage/w output.txt' -e '/Sysadmin/w output.txt' thegeekstuff.txt$ cat output.txt1. Linux - Sysadmin, Scripting etc.5. Storage10.Windows- Sysadmin, reboot etc.

4. Write the lines from which the pattern matches to till end of the file

In this example, /Storage/,$ represents line matches from Storage to end of the file.

$ sed -n '/Storage/,$w output.txt' thegeekstuff.txt$ cat output.txt5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

5. Write the lines which matches pattern and next two lines from match

In this example, the send command writes the line matches for “Storage” and two lines next to that.

$ sed -n '/Storage/,+2w output.txt' thegeekstuff.txt$ cat output.txt5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)

转载地址:http://wyffb.baihongyu.com/

你可能感兴趣的文章
【数据结构】动态顺序表
查看>>
Markdown的基础使用
查看>>
Linux基础命令
查看>>
【C语言】交换两个数值的三种方法
查看>>
【数据结构】栈的简单理解以及对栈的基本操作
查看>>
【数据结构】简单不带环迷宫的实现(用栈实现)
查看>>
【C语言】简单的了解递归(求斐波那契,n的阶乘,字符串长度,把一个整型(无符号),转化为字符型并打印出来)
查看>>
【数据结构】动态栈的实现
查看>>
【数据结构】简单的迷宫(用递归实现)
查看>>
【数据结构】队列的基本认识和队列的基本操作
查看>>
【数据结构】循环队列的认识和基本操作
查看>>
【LeetCode】无重复字符的最长子串
查看>>
时间复杂度
查看>>
【C++】动态内存管理 new和delete的理解
查看>>
【Linux】了解根目录下每个文件的作用
查看>>
【Linux】进程的理解(一)
查看>>
【Linux】进程的理解(二)
查看>>
【C语言】深度理解函数的调用(栈帧)
查看>>
【Linux】进程的理解(三)
查看>>
【C++】带头节点的双向线链表的实现
查看>>