How the Unix sed editor reads and writes file operations

Source: Internet
Author: User

This article will show you how to read the contents of a file into the SED output and how to write part of a file to another file

I. File Read

It is assumed that there are two documents, File1 and File2, respectively, as follows:

$ cat File1
1apple
1banana
1mango

$ cat File2
2orange
2strawberry

SED has two options for reading and writing files

R FileName: Reads the file contents specified by filename
W FileName: Write content to file specified by filename

See Example:

1. Read the contents of File2 after reading every line in File1


$ sed ' r file2 ' file1
1apple
2orange
2strawberry
1banana
2orange
2strawberry
1mango
2orange
2strawberry

R File2 Read all the contents of File2, so R didn't know that line number or match before, so with the above output, remember, the working mechanism of SED, each read the File1 line, and then execute the command

2. How to read file2 after reading the first line of File1

$ sed ' 1r file2 ' file1
1apple
2orange
2strawberry
1banana
1mango

Just add a 1 to the front.

3. When file1 a line matches the pattern, read the file2

$ sed '/banana/r file2 ' file1
1apple
1banana
2orange
2strawberry
1mango

Sed reads the file1 line by row, and then determines whether the row matches banana, and if so, attends the File2

4. When read file1 read into file2, in fact, is to merge two files

$ sed ' $r file2 ' file1
1apple
1banana
1mango
2orange
2strawberry

Here is just a demonstration, in fact, cat file1 File2 can complete the merge

two. File Write

Use a file1 file that reads as follows:

$ cat File1
Apple
Banana
Mango
Orange
Strawberry

1. Write 2-4 lines of file1 to File2


$ Sed-n ' 2,4w file2 ' file1

2,4W is to write 2-4 lines of meaning, that-n?
By default, SED outputs the processed output of the read file to the standard output, which is the terminal, and in order not to use the default output,-N comes in handy, and execution of the command terminal will not have any output

$ cat File2
Banana
Mango
Orange

View file2 content, found to have been written successfully

2. Write all file2 from the third line

$ Sed-n ' 3, $w file2 ' file1

$ cat File2
Mango
Orange
Strawberry
Onno more explanations.

3. What if it is a regular?


$ Sed-n '/apple/,/mango/w file2 ' file1

$ cat File2
Apple
Banana
Mango
The command reads a line-by-row into the file1 and then determines whether the row matches Apple and, if it matches, acts as the starting line, continues reading, determines whether to match the mango, and, if so, terminates the row, and then writes the middle content to the File2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.