How Unix sed adds a row to the file

Source: Internet
Author: User
Tags first row

Sed is one of the most important editors in Unix, note that there is one. Support a variety of editing tasks, this article will implement the functional example of the topic

Suppose we have a text file called Empfile that contains the employee name and employee ID, as follows:

Hilesh, 1001
Bharti, 1002
Aparna, 1003
Harshal, 1004
Keyur, 1005


1. How to add a header line to a file through sed-"Employee, EmpId"


$ sed ' 1i Employee, EmpId ' Empfile
Employee, EmpId
Hilesh, 1001
Bharti, 1002
Aparna, 1003
Harshal, 1004
Keyur, 1005

Explanation: The number 1, is to say that only the first line to perform operations, I represent in the insert (familiar with VIM students should know, I will be inserted in front of the current character, A is inserted in the back), therefore, 1i means that after the employee, Empid inserted into the first row,

Then, the file with the header row will only output to the standard output, the source file content will not change, if you need to update the source file, you can use the redirection output to a temporary file, and then move to the original file. If the UNIX system's SED is gun version, SED will have a-i option, can be directly implemented to update the source file (how to view the version, the terminal under the input sed-version can see) The following first execution, then look at the file, found that more than the title line

$ Sed-i ' 1i Employee, EmpId ' Empfile
$ cat Empfile
Employee, EmpId
Hilesh, 1001
Bharti, 1002
Aparna, 1003
Harshal, 1004
Keyur, 1005

2. How to add a line after the header row, which is the original first row-"——-"

$ Sed-i ' 1a ———————-' empfile
$ cat Empfile

Employee, EmpId

Hilesh, 1001
Bharti, 1002
Aparna, 1003
Harshal, 1004
Keyur, 1005

The same 1, 1 means the first line, a means append (append), that is, when you read the first line and then add a row after it, if you use 2i as the command is also correct, that is, when reading the second line, before it inserts a row.

3. How to add a row at the end of a file

$ Sed-i ' $a ———————-' empfile
$ cat Empfile

Employee, EmpId

Hilesh, 1001
Bharti, 1002
Aparna, 1003
Harshal, 1004
Keyur, 1005
———————-

To insert a row at the end of a file, if you use the previous method you need to know how many rows are in total, and the $ symbol directly indicates the last line, so $a says that when you read the last line, insert a row behind

4. How to insert a new record after the specified records

Suppose the contents of our example file are now:

Employee, EmpId

Hilesh, 1001
Harshal, 1004
Keyur, 1005
———————-

If I want to insert information about Bharti employees after Hilesh this employee, I do this:

$ Sed-i '/hilesh/a Bharti, 1002 ' Empfile
$ cat Empfile

Employee, EmpId

Hilesh, 1001
Bharti, 1002
Harshal, 1004
Keyur, 1005
———————-

Watch out, we are no longer using numbers or other signs that indicate line numbers, we use a pattern (friends who know the regular expressions are familiar and can be understood to be some sort of rule-/hilesh/a This command represents the content that is read for each line, and if you find/hilesh/this match, After the line is inserted, that is, if there are two lines in the file that are Hilesh employees, then the above command will be followed by two lines of content, where you can think about the working mode of SED, perform command condition detection for each row, and find the match.

5. How do i insert a record before I specify a record, for example, I want to insert a Aparna record before harshal this record?

$ Sed-i '/harshal/i Aparna, 1003 ' empfile
$ cat Empfile

Employee, EmpId

Hilesh, 1001
Bharti, 1002
Aparna, 1003
Harshal, 1004
Keyur, 1005
———————-

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.