我需要使用sed来更改以下行
rec.autos.sport.f1 0000001427 0000000001 y
rec.autos.sport.misc 0000000177 0000000001 y
rec.music.bluenote.blues 0000000531 0000000001 y
sci.med.transcription 0000000187 0000000001 m
test.test.222 0000000000 00001 m
进入这个
addgroup rec.autos.sport.f1 y
addgroup rec.autos.sport.misc y
addgroup rec.music.bluenote.blues y
addgroup sci.med.transcription m
addgroup test.test.222 m
我用
打开了addgroup部分s/\(^[a-zA-Z]\)/addgroup \1/g
我是使用sed的新手,所以我不知道如何实现这一目标。如果有人回答你所做的快速解释会有很多帮助。
答案 0 :(得分:0)
在你的后面添加第二个替换命令:
$ sed -e 's/\(^[a-zA-Z]\)/addgroup \1/' -e 's/\( [^[:blank:]][^[:blank:]]*\)\{2\}\( [^[:blank:]]*\)$/\2/g' file
addgroup rec.autos.sport.f1 y
addgroup rec.autos.sport.misc y
addgroup rec.music.bluenote.blues y
addgroup sci.med.transcription m
addgroup test.test.222 m
或者,将它们全部合并到一个s
命令中:
$ sed -e 's/\(.*\)\( [^[:blank:]][^[:blank:]]*\)\{2\}\( [^[:blank:]][^[:blank:]]*\)$/addgroup \1 \3/g' file
addgroup rec.autos.sport.f1 y
addgroup rec.autos.sport.misc y
addgroup rec.music.bluenote.blues y
addgroup sci.med.transcription m
addgroup test.test.222 m
使用扩展的正则表达式,我们可以避免所有这些反斜杠。使用GNU sed:
$ sed -r 's/(.*)( [^[:blank:]]+){2}( [^[:blank:]]+)$/addgroup \1 \3/g' file
addgroup rec.autos.sport.f1 y
addgroup rec.autos.sport.misc y
addgroup rec.music.bluenote.blues y
addgroup sci.med.transcription m
addgroup test.test.222 m
$ awk '{$(NF-2)=$NF; NF-=2; printf "addgroup %s\n",$0;} ' file
addgroup rec.autos.sport.f1 y
addgroup rec.autos.sport.misc y
addgroup rec.music.bluenote.blues y
addgroup sci.med.transcription m
addgroup test.test.222 m
答案 1 :(得分:0)
使用tr
标准化空格的另一个awk解决方案
awk '{$1="addgroup "$1;$2=$3=""}1' file | tr -s ' '
答案 2 :(得分:0)
sed 's/^\([^ ]\+\)[ 0-9]\+\(.\)$/addgroup \1 \2/'