sed進階使用

來源:互聯網
上載者:User

標籤:sed進階使用

1、名reverse指令碼用sed編輯器指令碼反轉資料流的文本行

                h 將模式空間複製到保持空間
                H 將模式空間附加到保持空間
                g 將保持空間複製到模式空間
                G 將保持空間附加到模式空間
                x  交換模式空間和保持空間的內容

                p  列印模式空間

 

                n  提取資料流的下一行

    

[[email protected] tmp]# vim reverse.sh#!/bin/bash#sed -n ‘{1!Gh$p}‘ $1~       [[email protected] tmp]# cat data1the quick brown fox jumps over the lazy dog1a quick brown fox jumps over the lazy dog2the quick brown fox jumps over the lazy dog3the quick brown fox jumps over the lazy dog4the quick brown fox jumps over the lazy dog5the quick brown fox jumps over the lazy dog6[[email protected] tmp]# sh reverse.sh data1the quick brown fox jumps over the lazy dog6the quick brown fox jumps over the lazy dog5the quick brown fox jumps over the lazy dog4the quick brown fox jumps over the lazy dog3a quick brown fox jumps over the lazy dog2the quick brown fox jumps over the lazy dog1

2、加位行間距

[[email protected] tmp]# sed ‘G‘ data1the quick brown fox jumps over the lazy dog1a quick brown fox jumps over the lazy dog2 the quick brown fox jumps over the lazy dog3the quick brown fox jumps over the lazy dog4the quick brown fox jumps over the lazy dog5the quick brown fox jumps over the lazy dog6

如果末尾不想這個空白行

[[email protected] tmp]# sed ‘$!G‘ data1the quick brown fox jumps over the lazy dog1a quick brown fox jumps over the lazy dog2 the quick brown fox jumps over the lazy dog3the quick brown fox jumps over the lazy dog4the quick brown fox jumps over the lazy dog5the quick brown fox jumps over the lazy dog6

3、刪除已有空白行

[[email protected] tmp]# sed ‘/^$/d‘ data1the quick brown fox jumps over the lazy dog1a quick brown fox jumps over the lazy dog2the quick brown fox jumps over the lazy dog3the quick brown fox jumps over the lazy dog4the quick brown fox jumps over the lazy dog5the quick brown fox jumps over the lazy dog6

4、給檔案中的行編號

 [[email protected] tmp]# sed ‘=‘ data11the quick brown fox jumps over the lazy dog12a quick brown fox jumps over the lazy dog234the quick brown fox jumps over the lazy dog35the quick brown fox jumps over the lazy dog46the quick brown fox jumps over the lazy dog57the quick brown fox jumps over the lazy dog6

這樣可觀性不是很好

[[email protected] tmp]# sed ‘=‘ data1 | sed ‘N;s/\n/ /‘1 the quick brown fox jumps over the lazy dog12 a quick brown fox jumps over the lazy dog23 the quick brown fox jumps over the lazy dog34 the quick brown fox jumps over the lazy dog45 the quick brown fox jumps over the lazy dog56 the quick brown fox jumps over the lazy dog6

5、刪除空白行

 [[email protected] tmp]# sed ‘/^$/d‘ data1the quick brown fox jumps over the lazy dog1a quick brown fox jumps over the lazy dog2the quick brown fox jumps over the lazy dog3the quick brown fox jumps over the lazy dog4the quick brown fox jumps over the lazy dog5the quick brown fox jumps over the lazy dog6[[email protected] tmp]#

本文出自 “散人” 部落格,請務必保留此出處http://zouqingyun.blog.51cto.com/782246/1695822

sed進階使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.