Centos command -- sed

Source: Internet
Author: User

1. Merge rows
Zj @ zj :~ /Script/blog_script $ cat test1
1
2
3
4
Merge the upper and lower rows
Zj @ zj :~ /Script/blog_script $ sed '$! N; s/\ n/\ t/'test1
1 2
3 4
Merge matching mode and next line
Zj @ zj :~ /Script/blog_script $ sed '/2/{N; s/\ n/\ t/}' test1
1
2 3
4
Merge all rows
Zj @ zj :~ /Script/blog_script $ sed ': a; N; s/\ n/\ t/; ba; 'test1
1 2 3 4

2. Exchange rows
2.1 Exchange two rows when the row number is known
Zj @ zj :~ /Script/blog_script $ cat test
Baidu music so terrible so bad
Microsoft haha
Yahoo byebye
Google princess so good
Here is the exchange of 1, 4 rows. Of course you can modify it as needed.
Zj @ zj :~ /Script/blog_script $ for (I = 1; I <= 4; I ++); do case $ I in 1) sed-n 4 p test; 4) sed-n 1 p test; *) sed-n $ {I} p test; esac; done
Google princess so good
Microsoft haha
Yahoo byebye
Baidu music so terrible so bad
Continuous saying:
Zj @ zj :~ /Script/blog_script $ sed '1 {h; d}; 2 {G} 'test
Microsoft haha
Baidu music so terrible so bad
Yahoo byebye
Google princess so good

2.2 do not know the row number
If the two rows to be exchanged are consecutive rows:
Zj @ zj :~ /Script/blog_script $ sed '/baidu/{h; d};/microsoft/{G}' test
Microsoft haha
Baidu music so terrible so bad
Yahoo byebye
Google princess so good
Ps: Exchange rows that contain bai and microsoft
The two rows are discontinuous:

Zj @ zj :~ /Script/blog_script $ sed '/baidu/{: a; N;/google /! Ba; s/\ ([^ \ n] * \) \ n \(. * \) \ n \(. * \)/\ 3 \ n \ 2 \ n \ 1/} 'test
Google princess so good
Microsoft haha
Yahoo byebye
Baidu music so terrible so bad
Ps: Exchange rows containing baidu and google

We have studied the following:
Zj @ zj :~ /Script/blog_script $ sed '/baidu/{: a; N;/microsoft /! Ba; /[^ \ n] * baidu [^ \ n] * \ n [^ \ n] * microsoft [^ \ n] * $/{s/\ ([^ \ n] * baidu [^ \ n] * \) \ n \(. * \)/\ 2 \ n \ 1/}; s/\ ([^ \ n] * \) \ n \(. * \) \ n \(. * \)/\ 3 \ n \ 2 \ n \ 1/} 'test
Microsoft haha
Baidu music so terrible so bad
Yahoo byebye
Google princess so good

Zj @ zj :~ /Script/blog_script $ sed '/baidu/{: a; N;/google /! Ba; /[^ \ n] * baidu [^ \ n] * \ n [^ \ n] * google [^ \ n] * $/{s/\ ([^ \ n] * baidu [^ \ n] * \) \ n \(. * \)/\ 2 \ n \ 1/;}; s/\ ([^ \ n] * \) \ n \(. * \) \ n \(. * \)/\ 3 \ n \ 2 \ n \ 1/} 'test
Google princess so good
Microsoft haha
Yahoo byebye
Baidu music so terrible so bad

Ps: the above Code
/Baidu/{...} encounters a line containing baidu and starts to execute the command sequence in {}
: A; N;/google /! Ba reads information cyclically until google is read.
/[^ \ N] * baidu [^ \ n] * \ n [^ \ n] * google [^ \ n] * $/this means that if there is only one \ n, that is, the two rows are continuous: {s/\ ([^ \ n] * baidu [^ \ n] * \) \ n \(. * \)/\ 2 \ n \ 1/;} exchange the two rows

If it does not match the above pattern, it means it is not continuous.
S/\ ([^ \ n] * \) \ n \(. * \) \ n \(. * \)/\ 3 \ n \ 2 \ n \ 1 /}
In the case of matching continuous rows, it is impossible to match the. OK ~~~~~ of the above expression ~~~~

3. Interactive parity
Zj @ zj :~ /Script/blog_script $ sed '$! N; s/\ ([^ \ n] * \) \ n \ ([^ \ n] * \)/\ 2 \ n \ 1/'test
Microsoft haha
Baidu music so terrible so bad
Google princess so good
Yahoo byebye

4. No need to delete the row.
D hello ....

5. Delete empty rows
Sed '/^ $/d' test2
Delete multiple empty rows
Sed '/^ $/{N;/^ \ n * $/D}' test2

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.