Some tips of sed

sed 'y/string/STRING/g' # Replace s -> S, t -> T, r -> R ‥‥

sed 's/string/STRING/g' # Replace "string" -> "STRING".

sed 's/string/STRING/' # Only replace the first "string" to "STRING" in a row.

sed 's/^string/STRING/' # Only replace the "string" to "STRING" if it starts with the line head.

sed 's/string$/STRING/' # Only replace the "string" to "STRING" if it's end with the tail of a line.

sed 's/string//g' # Remove "string" from a row.

sed 's/^$//g' # Remove empty row.

sed 's/^.....//' # Remove 5 characters from the line head.

sed -n '/string/p' # Print a row which is including "string".

sed '/string/d' # Delete a row which is including "string".

sed '5,35s/string$/STRING/' # Only process from line 5th to line 35th.

sed '5,$s/string$/STRING/' # Process from line 5th.

sed -n '/^I/p' # Only print a row started with "I".

sed '$d' # Delete the last row.

sed '$-5,$d' # Delete the last 5 rows.

sed -e "s/\.txt/.doc/" # Replace ".txt" to ".doc".

$ file_name="linux.txt" 
$ echo $file_name | sed -e "s/\.txt/.doc/" 
linux.doc

sed -e "s/\.[^.]*$//g" # Remove ".${anyString}".

$ file_name="linux.txt" 
$ echo $file_name | sed -e "s/\.[^.]*$//g" 
linux
$ file_name="linux..txt" 
$ echo $file_name | sed -e "s/\.[^.]*$//g" 
linux.

sed -e "s/.*\.\([^.]*\)\$/\1/g" # Print special string such as a filename extension.

$ file_name="linux.txt" 
$ echo $file_name | sed -e "s/.*\.\([^.]*\)\$/\1/g" 
txt

 

 

转载于:https://my.oschina.net/cofecafe1/blog/799881

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值