我很难用sed执行此任务。
示例输入:
X(996002) dsafsafdsfdsfds dsfdsfds dsfd ds
Y(300342) dsfdsfhdsfhsa dsf;dsjfds f 15 16 .
"(1200479) dsfkjdsfhjfds dsfdsj sd "
示例输出:
996002 TAB ) dsafsafdsfdsfds dsfdsfds dsfd ds
300342 TAB ) dsfdsfhdsfhsa dsf;dsjfds f 15 16 .
1200479 TAB ) dsfkjdsfhjfds dsfdsj sd "
到目前为止,我有以下内容与sed:
sed -e 's/^.*\([0-9]*\)\(.*?\)$/\1 \t \2/gp' textfile.txt
但我认为我并不了解让它发挥作用的东西。
答案 0 :(得分:3)
您可以使用:
sed -i.bak 's/^[^[:digit:]]*\([[:digit:]]\+\)/\1\t /' file
996002 ) dsafsafdsfdsfds dsfdsfds dsfd ds
300342 ) dsfdsfhdsfhsa dsf;dsjfds f 15 16 .
1200479 ) dsfkjdsfhjfds dsfdsj sd "
<强>解释强>
-i.bak
- 内嵌编辑,使用.bak
扩展名保存原始文件^
- 匹配linse start ^[^[:digit:]]\+
- 在开始时匹配1个或多个非数字\(...\)
- 将所附内容分组到捕获的组#1 \([[:digit:]]\+\)
- 捕获组#1中的1位或更多位数\1\t
- 替换为组#1,后跟选项卡和空格答案 1 :(得分:1)
这对你有用吗?
sed 's/.*(\([^)]*\)/\1\t/' file
输入文件:
kent$ cat f
X(996002) dsafsafdsfdsfds dsfdsfds dsfd ds
Y(300342) dsfdsfhdsfhsa dsf;dsjfds f 15 16 .
"(1200479) dsfkjdsfhjfds dsfdsj sd "
kent$ sed 's/.*(\([^)]*\)/\1\t/' f
996002 ) dsafsafdsfdsfds dsfdsfds dsfd ds
300342 ) dsfdsfhdsfhsa dsf;dsjfds f 15 16 .
1200479 ) dsfkjdsfhjfds dsfdsj sd "
答案 2 :(得分:0)
使用awk
awk -F".[(]" '{sub(/)/,"\t)", $2);print $2}' file
996002 ) dsafsafdsfdsfds dsfdsfds dsfd ds
300342 ) dsfdsfhdsfhsa dsf;dsjfds f 15 16 .
1200479 ) dsfkjdsfhjfds dsfdsj sd "