1. 程式人生 > >謝煙客---------Linux之 sed工具使用

謝煙客---------Linux之 sed工具使用

linux

sed

Stream EDitor 文本流編輯器, 編輯器


sed工作方式

技術分享

技術分享

技術分享

技術分享

技術分享


sed基本使用

格式一:

sed [OPTION ...] [SCRIPT] [FILE...]

多個文件處理: 處理完一個,繼續處理第二個。
sed script file1 --> sed script file2

使用方法基本同vim命令
1)vim % --> sed 不用指明
2)vim @@ ## // --> sed || ,, @@ //

技術分享


格式二:

sed  ‘地址定界command‘  FILE ...

1)地址定界

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享



2)command

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

例如:
將文件中的內容插入至指定文件中
# sed ‘/^UUID/r /etc/issue‘ /etc/fstab
# sed ‘1,3r /etc/centos-release‘ /etc/issue

技術分享

技術分享

例如:
取一個文件的基名、目錄名
基名: echo "/etc/sysconfig/useradd/" | sed ‘s,/$,,‘ | sed -r ‘s|(.*/)([^/]+)|\2|‘ 
目錄名: echo "/etc/sysconfig/useradd/" | sed ‘s,/$,,‘ | sed -r ‘s|(.*/)([^/]+)|\2|‘

技術分享

技術分享

例如:
# echo "how are you" | sed -r ‘s/(how)(.*)(you)/\3\2\1/‘
# echo "how are you" | sed -r ‘[email protected](how)(.*)(you)@\3\2\[email protected]‘
# echo "how are you" | sed -r ‘s#(how)(.*)(you)#\3\2\1#‘
# echo "how are you" | sed -r ‘s,(how)(.*)(you),\3\2\1,‘
# echo "how are you" | sed -r ‘s|(how)(.*)(you)|\3\2\1|‘

技術分享

例如:
[[email protected] bin]# head -n 1 < /etc/passwd | sed ‘s,root,ROOT,‘
ROOT:x:0:0:root:/root:/bin/bash
[[email protected] bin]# head -n 1 < /etc/passwd | sed ‘s,root,ROOT,g‘
ROOT:x:0:0:ROOT:/ROOT:/bin/bash

技術分享

例如:
[[email protected] bin]# head -n 1 < /etc/passwd | sed ‘s,rOOt,ROOT,i‘
ROOT:x:0:0:root:/root:/bin/bash
[[email protected] bin]# head -n 1 < /etc/passwd | sed ‘s,rOOt,ROOT,gi‘
ROOT:x:0:0:ROOT:/ROOT:/bin/bash

技術分享

例如:
[[email protected] bin]# cat < /etc/passwd | sed -n ‘s,rOOt,ROOT,ip‘
ROOT:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/ROOT:/sbin/nologin

技術分享

# sed ‘s,,,w /PATH/TO/SOMEFILE‘ FILE
# sed ‘s,,,‘ FILE > /PTH/TO/SOMEFILE


練習1:刪除/boot/grub/grub.conf文本中所有以空白開頭的行行首的空白字符

練習2:刪除/etc/fstab文件中所有以#開頭,後面至少跟一個空白字符的行的行首的#和空白字符

練習3:echo 一個絕對路徑給sed命令:取出其基名,取出目錄名


練習1:刪除/boot/grub/grub.conf文本中所有以空白開頭的行行首的空白字符

CentOS 6:
    # cp /boot/grub/grub.conf /tmp/grub.conf
    # sed -i ‘s,^[[:space:]]\+,,‘ /tmp/grub.conf
CentOS 7:
    # cp /etc/grub2.cfg /tmp/grub2.cfg
    # sed -i ‘s,^[[:space:]]\+,,‘ /tmp/grub2.cfg

練習2:刪除/etc/fstab文件中所有以#開頭,後面至少跟一個空白字符的行的行首的#和空白字符

# cp /etc/fstab /tmp/a.file
# sed -i -r ‘s|^#[[:space:]]+||‘ /tmp/a.file

擴展:刪除以#開頭,後面沒有跟空白字符的行的行首的#
	# cp /etc/fstab /tmp/fstab
	# echo "#hello sed" >> /tmp/fstab
	# sed -i -r ‘[email protected]^#([^[:space:]].*)@\[email protected]‘ /etc/fstab


練習3:echo 一個絕對路徑給sed命令:取出其基名,取出目錄名

測試取出文件名中存在 useradd的絕對路徑
	# locate -b useradd
		...
		/usr/share/man/zh_CN/man8/useradd.8.gz
		/usr/share/man/zh_TW/man8/useradd.8.gz
		...

取出/usr/share/man/zh_TW/man8/useradd.8.gz的基名
	# echo "/usr/share/man/zh_TW/man8/useradd.8.gz/" | sed ‘s,/$,,‘ |  sed -r ‘s#(.*/)([^/]+$)#\2#‘

取出/usr/share/man/zh_TW/man8/useradd.8.gz的目錄名
	# echo "/usr/share/man/zh_TW/man8/useradd.8.gz" | sed ‘s,/$,,‘ |  sed -r ‘s#(.*/)([^/]+$)#\1#‘


格式三:

sed ‘地址定界command;地址定界command;地址定界command;...‘

技術分享

技術分享


本文出自 “Reading” 博客,請務必保留此出處http://sonlich.blog.51cto.com/12825953/1964161

謝煙客---------Linux之 sed工具使用