標籤: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進階使用