使用SED用分隔符来分割字符串 [英] Using sed to split a string with a delimiter
问题描述
我在以下格式的字符串:
I have a string in the following format:
字符串1:字符串2:STRING3:串,4:STRING5
我试图用 SED
拆就字符串:
并打印在每个子串新队。下面是我在做什么:
I'm trying to use sed
to split the string on :
and print each sub-string on a new line. Here is what I'm doing:
猫〜/桌面/ myfile.txt的| SED S /:/ \\\\ N /
这将打印:
string1
string2:string3:string4:string5
我怎样才能得到它在每个定界符分割?
How can I get it to split on each delimiter?
推荐答案
您替换后错过了先按g
。因此,它只是做一次。参见:
You missed the g
after the substitution. Hence, it is just done once. See:
$ echo "string1:string2:string3:string4:string5" | sed s/:/\\n/g
string1
string2
string3
string4
string5
先按g
为先按g
叶形,并指取代已在全球范围内完成,也就是说,任何发生。看到默认为1,如果你把例如2,它是做2次,等等。
g
stands for g
lobal and means that the substitution has to be done globally, that is, for any occurrence. See that the default is 1 and if you put for example 2, it is done 2 times, etc.
总之,你的情况,你将需要使用:
All together, in your case you would need to use:
sed 's/:/\\n/g' ~/Desktop/myfile.txt
请注意,您可以直接使用,而不是不必要的管道的 SED ...文件
语法:猫文件| SED
。
Note that you can directly use the sed ... file
syntax, instead of unnecessary piping: cat file | sed
.
这篇关于使用SED用分隔符来分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!