sed / 替换为 _ -e选项 单/双引号 三个\\\ 两个\\ linux shell 分析

来源:互联网 发布:linux grep命令 编辑:程序博客网 时间:2024/06/05 06:57


[root@localhost expert_shell]# LOGCHKS="/var/log/messages:authentication%20failure:rbpeters:warn /var/log/messages:recv%20failure::error"[root@localhost expert_shell]# logfile=` |cut -d: -f1`   或者 logfile=`echo $LOGCHKS |awk -F: '{print $1}'`结果也一样
[root@localhost expert_shell]# echo $logfile/var/log/messages[root@localhost expert_shell]# logfile1=`echo $LOGCHKS |cut -d: -f2`[root@localhost expert_shell]# echo $logfile1authentication%20failure[root@localhost expert_shell]# logfile1=`echo $LOGCHKS |cut -d: -f3`[root@localhost expert_shell]# echo $logfile1rbpeters[root@localhost expert_shell]# logfile1=`echo $LOGCHKS |cut -d: -f4`[root@localhost expert_shell]# echo $logfile1warn /var/log/messages[root@localhost expert_shell]# logfile1=`echo $LOGCHKS |cut -d: -f2`[root@localhost expert_shell]# echo $logfile1authentication%20failure[root@localhost expert_shell]# strings="`echo $logfile1 |sed -e \"s/%20/ /g\"`"[root@localhost expert_shell]# echo $stringsauthentication failure[root@localhost expert_shell]# strings="`echo $logfile1 |sed -e 's/%20/ /g'`"[root@localhost expert_shell]# echo $stringsauthentication failure

代码第21行 (注意到sed -e 后用的是\" g后用的也是\"双引号

[root@localhost expert_shell]# strings="`echo $logfile1 |sed -e \"s/%20/ /g\"`"  双引号

和代码第24行(注意到sed -e 后直接用的是' g后用的也直接是'单引号

[root@localhost expert_shell]# strings="`echo $logfile1 |sed -e 's/%20/ /g'`"  单引号

作用相同,都是将%20替换为空格(s/%20/ /g,/ /之间留有空格)。

或者 用\ 后面加个空格也可以表示空格:

strings="`echo $logfile1 |sed -e 's/%20/\ /g'`"

执行结果一样。

用\ 后面加个空格或者/ /之间留个空格两种方式都可以表示空格。

用\ 后面加个空格或者/ /之间留个空格两种方式都可以表示空格。

用\ 后面加个空格或者/ /之间留个空格两种方式都可以表示空格

如果是

sed -e 's/%20/\/g' 或者sed -e 's/%20//g' 
\ 后面不加空格或者/ /之间不留空格,就变成删除的作用了。



以下分析讨论将/var/log/messages 中的 / 替换为 _ (下划线)_var_log_messages的几种情况

实际上,只需如下即可:

[root@localhost sh]# echo $logfile/var/log/messages[root@localhost sh]# echo $logfile |sed -e 's/\//_/g'_var_log_messages[root@localhost sh]# echo $logfile |sed 's/\//_/g'  去掉了 -e选项_var_log_messages
或者:
[root@localhost sh]# suffi=`echo $logfile |sed "s/\//_/g"`双引号 无-e[root@localhost sh]# echo $suffi_var_log_messages[root@localhost sh]# suffi=`echo $logfile |sed -e "s/\//_/g"`双引号有-e[root@localhost sh]# echo $suffi_var_log_messages
可用-e ,也可不用-e,用\对/进行转义就行了,下面的讨论复杂了,汗……

分:有无-e选项 、 单引号 、双引号、 \\\(三个\\\)和\\(两个\\)的组合进行讨论

[root@localhost expert_shell]# echo $logfile/var/log/messages

[root@localhost expert_shell]# suffix=`echo $logfile |sed -e s/\\\//_/g`[root@localhost expert_shell]# echo $suffix_var_log_messages

去掉了 -e选项,不加单引号和双引号, \\\(三个\\\),执行结果一样

[root@localhost expert_shell]# suffix1=`echo $logfile |sed s/\\\//_/g`   [root@localhost expert_shell]# echo $suffix1_var_log_messages

去掉了 -e选项,加双引号,\\\(三个\\\),执行结果一样

[root@localhost expert_shell]# suffix4=`echo $logfile |sed  "s/\\\//_/g"`[root@localhost expert_shell]# echo $suffix4_var_log_messages

去掉了 -e选项,加单引号,\\\(三个\\\),出错

[root@localhost expert_shell]# suffix5=`echo $logfile |sed 's/\\\//_/g'`sed: -e expression #1, char 7: unknown option to `s'

去掉了 -e选项,但是对 s/\\\//_/g  去掉一个\ ,变成s/\\//_/g (三个\\\变成了两个\\) ,并 用双引号” “或者 单引号‘ '将其 引起,执行结果一样

[root@localhost expert_shell]# suffix8=`echo $logfile |sed 's/\\//_/g'`             ###去掉-e选项 单引号' 两个\\[root@localhost expert_shell]# echo $suffix8_var_log_messages[root@localhost expert_shell]# suffix9=`echo $logfile |sed "s/\\//_/g"` ###去掉-e选项 双引号" 两个\\[root@localhost expert_shell]# echo $suffix9_var_log_messages

仍保留 -e选项,但是对 s/\\\//_/g 用单引号’ ’ 引起,出错

[root@localhost expert_shell]# suffix2=`echo $logfile |sed -e 's/\\\//_/g'`sed: -e expression #1, char 7: unknown option to `s'

仍保留 -e选项,但是对 s/\\\//_/g 用双引号” “ 引起,执行结果一样

[root@localhost expert_shell]# suffix3=`echo $logfile |sed -e "s/\\\//_/g"`[root@localhost expert_shell]# echo $suffix3_var_log_messages

仍保留 -e选项,但是对 s/\\\//_/g  去掉一个\ ,变成s/\\//_/g (三个\\\变成了两个\\) ,并 用双引号” “或者 单引号‘ '将其 引起,执行结果一样

[root@localhost expert_shell]# suffix6=`echo $logfile |sed  -e "s/\\//_/g"`              ###双引号"  两个\\[root@localhost expert_shell]# echo $suffix6_var_log_messages

[root@localhost expert_shell]# suffix7=`echo $logfile |sed  -e 's/\\//_/g'`                   ###单引号'  两个\\[root@localhost expert_shell]# echo $suffix7_var_log_messages

一些思考:

在sed 中有时双引号和单引号作用一样,有时却不一样,这究竟是如何引起的呢?

sed 中手册推荐是用单引号,但是双引号也好使,为什么??????????!?!!!!

在awk 中却只能用单引号,用双引号不好使,例如:

[root@localhost temp]# cat oldboy.txtI am oldboy,my qq is 31333741

[root@localhost temp]# cat oldboy.txt |awk '{print $2}' #######单引号好使am

[root@localhost temp]# cat oldboy.txt |awk "{print $2}"#######双引号不好使,输出全文

I am oldboy,my qq is 31333741

说明双引号和单引号在awk 和sed 中表现出不同的性质,要注意。

	
				
		
原创粉丝点击