[英]How to modify the file content using sed
IP file - 1.txt
IP文件
1.txt
$ cat 1.txt
'&1'
'&2'
'&3'
if I want the o/p as 如果我想将o / p作为
:1
:2
:3
using sed, (or any command for that matter), how do I achieve? 使用sed(或与此相关的任何命令),我如何实现?
awk '{gsub(/'"^'"'|'"'$"'/,"");gsub(/&/,":")}1' input
:1
:2
:3
Here , awk
's gsub
function is used to replace single quotes to blank and another gsub is used to replace &
to :
. 在这里,
awk
的gsub
函数用于将单引号替换为空白,而另一个gsub用于替换&
来:
1
means,print in awk. 1
表示以awk打印。
You can get your o/p by using below 2 commands. 您可以使用以下2条命令获得o / p。
--> sed -e '/1/b' -e '/2/b' -e '/3/b' -ed 1.txt or --> gsed '/1:2:3/!d' 1.txt -> sed -e'/ 1 / b'-e'/ 2 / b'-e'/ 3 / b'-ed 1.txt或-> gsed'/ 1:2:3 /!d'1 。文本
Is this fine ? 这样好吗
sed -e 's/.*[^0-9]\([0-9]\+\)[^0-9]*$/:\1/' 1.txt
:1
:2
:3
$ cat 1.txt
Monday {'&1', 'Morning', 'Breakfast'}
Tuesday {'&2', 'Noon', 'Lunch'}
Wednesday {'&3', 'Evening', 'Supper'}
Thursday {'&4', 'Night', 'Dinner'}
$ sed -E 's/\x27&([0-9]+)\x27/:\1/' 1.txt
Monday {:1, 'Morning', 'Breakfast'}
Tuesday {:2, 'Noon', 'Lunch'}
Wednesday {:3, 'Evening', 'Supper'}
Thursday {:4, 'Night', 'Dinner'}
\\x27&
match single quote and &
(See ASCII set for reference) \\x27&
匹配单引号和&
(请参阅ASCII设置以供参考) ([0-9]+)
capture digits for back-referencing ([0-9]+)
捕获数字以进行反向引用 \\x27
match single quote \\x27
匹配单引号 :\\1
replace matched pattern with :
and captured digits :\\1
将匹配的模式替换为:
和捕获的数字 sed
versions use -r
instead of -E
for extended regex option sed
版本使用-r
代替-E
来扩展正则表达式选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.