Usage of sed Parameters

Source: Internet
Author: User

Usage of sed Parameters

Use sed to copy/etc/passwd to/root/test.txt and print all rows with sed.
Print 3 to 10 lines of test.txt
Print the row test.txt contains 'root'
Delete 15 rows of test.txt and all subsequent rows
Delete the line containing 'bash 'from test.txt.
Replace 'root' in test.txt with 'toor'
In test.txt, '/sbin/nologin' is'/bin/login'
Delete all numbers in rows 5 to 10 from test.txt.
Delete all special characters (except numbers and uppercase/lowercase letters) from test.txt)
Place the first and last words in test.txt
Replace the first number and the last word in test.txt
Move the first number in test.txt to the end of the row.
Add 'aaa: 'At the beginning of test.txt 20 rows to the end of the last row :'

My answer:

Sed-n'1, $ 'P test.txt
Sed-n'3, 10' p test.txt
Sed-n'/root/'P test.txt
Sed '15, $ 'd test.txt
Sed '/bash/'d test.txt
Sed-r's/(root)/toor/G'-n test.txt
Sed-r's @ (/sbin/nologin) @/bin/login @ G' test.txt
Sed-r '5, 10 s/[0-9] // G' test.txt
Sed-r's/[^ a-zA-Z0-9] // G' test.txt
Sed-r's @ (^ [^:] +) (:. * :) ([^:] + $) @ \ 3 \ 2 \ 1 @ 'test.txt
Sed-r's @ ([0-9] +) (. * :) ([^:] + $) @ \ 3 \ 2 \ 1 @ 'test.txt
Sed-r's @ ([0-9] +) (. *) ($) @ \ 2 \ 3 \ 1 @ 'test.txt
Sed-r '20, $ s @ (^. * $) @ aaa: \ 1 @ 'test.txt

Introduce shell variable in sed command

Shell programming in Linux -- basic usage of sed command

Sed for Unix text processing tools

Sed advanced usage

Sed command details and examples

This article permanently updates the link address:

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.