Linux Basic-sed+ Regular expressions

Source: Internet
Author: User

1. Delete the first character of each line of the file: Sed-r ' s/^.//g ' passwd

2. Delete the second character of each line of the file: Sed-r ' s/^ (.) (.) /\2/g ' passwd

3. Delete the last character of each line of the file: Sed-r ' s/(.) $//g '

4. Delete the second last character of each line of the file: Sed-r ' s/(.) (.) $/\2/g ' passwd

5. Delete the second word in each line of the file: Sed-r ' s/^ ([A-z]) ([^0-9][^a-z]) (A-Z)/\1\2/g ' passwd

6. Delete the second-to-last word in each line of a file:

Sed-r ' s/([a-z]+) ([^0-9][^a-z]) ([a-z]+] $/\2\3/g ' passwd

7. Delete the last word in each line of the file: Sed-r ' s/([a-z]+) $ ' passwd

8, swaps the first and second characters of each line: Sed-r ' s/^ (.) (.) /\2\1/g ' passwd

9, swap the first character and the second word for each line:

Sed-r ' s/^ (.) ([a-z]+) ([^0-9][^a-z]+) ([a-z]+) ([^0-9][a-z])/\4\2\3\1\5/g ' passwd

10, swap the first word and the last word for each line:

Sed-r ' s/^ ([a-z0-9]+) ([^a-z0-9]+) (. +) ([^a-z0-9]+] ([a-z0-9]+)/\5\2\3\4\1/g ' passwd

11. Delete all the numbers in a file: Sed-r ' s/[0-9]//g ' passwd

12. Remove all spaces at the beginning of each line: Sed-r ' s/^//g ' passwd

13, replace any spaces that appear in the file with tabs.

14, enclose all capital letters in parentheses (): Sed-r ' s/[a-z]/(&)/g ' passwd

15, print 3 times per line: sed ' p;p ' passwd each p represents one print, and if no p is printed again, then no more than one P will print one more time

16, show only the first word of each line: Sed-r ' s/([^0-9a-z]+) (. +)//g ' passwd

17, print the first word and the third word of each line

18, use the command to get the format MM/YY/DD date format, combined with the pipeline, change it to MM;YY;DD format

Date "+%m/%y/%d" | Sed-r ' s/\/\-/g '

Linux Basic-sed+ Regular expressions

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.