I have a string in the following format:
我有以下格式的字符串:
string1:string2:string3:string4:string5
string1:string2相等:string3:沙漠西部边缘一溜排开:string5
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:
我尝试使用sed来分割字符串:并在新行上打印每个子字符串。下面是我所做的:
cat ~/Desktop/myfile.txt | sed s/:/\\n/
猫~ /桌面/ myfile。txt | sed s /:/ \ \ n /
This prints:
这个打印:
string1
string2:string3:string4:string5
How can I get it to split on each delimiter?
如何让它在每个分隔符上拆分?
6 个解决方案
#1
46
To split a string with a delimiter with sed you say:
用sed将一个字符串分隔开,您会说:
sed 's/delimiter/\n/g'
For example, to split using :
as a delimiter:
例如,分割使用:作为分隔符:
$ sed 's/:/\n/g' <<< "he:llo:you"
he
llo
you
In this particular case, you missed the g
after the substitution. Hence, it is just done once. See:
在这个特殊情况下,你错过了替换后的g。因此,它只完成一次。看到的:
$ echo "string1:string2:string3:string4:string5" | sed s/:/\\n/g
string1
string2
string3
string4
string5
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.
g代表全局,也就是说,替换必须在全球范围内进行,也就是说,在任何情况下都必须这样做。看到默认值是1,如果你输入例子2,它会做2次,等等。
All together, in your case you would need to use:
总之,在你的情况下你需要使用:
sed 's/:/\\n/g' ~/Desktop/myfile.txt
Note that you can directly use the sed ... file
syntax, instead of unnecessary piping: cat file | sed
.
注意,您可以直接使用sed…文件语法,而不是不必要的管道:cat文件| sed。
#2
25
Using \n
in sed
is non-portable. The portable way to do what you want with sed
is:
在sed中使用\n是不可移植的。使用sed的便携方式是:
sed 's/:/\
/g' ~/Desktop/myfile.txt
but in reality this isn't a job for sed
anyway, it's the job tr
was created to do:
但事实上,无论如何,这并不是一份工作,这是工作tr所要做的:
tr ':' '
' < ~/Desktop/myfile.txt
#3
#4
3
This might work for you (GNU sed):
这可能对你有用(GNU sed):
sed 'y/:/\n/' file
or perhaps:
或者:
sed y/:/$"\n"/ file
#5
1
This should do it:
这应该这样做:
cat ~/Desktop/myfile.txt | sed s/:/\\n/g
#6
1
If you're using gnu sed then you can use \x0A
for newline:
如果你使用gnu sed,那么你可以使用\x0A来换行:
sed 's/:/\x0A/g' ~/Desktop/myfile.txt
#1
46
To split a string with a delimiter with sed you say:
用sed将一个字符串分隔开,您会说:
sed 's/delimiter/\n/g'
For example, to split using :
as a delimiter:
例如,分割使用:作为分隔符:
$ sed 's/:/\n/g' <<< "he:llo:you"
he
llo
you
In this particular case, you missed the g
after the substitution. Hence, it is just done once. See:
在这个特殊情况下,你错过了替换后的g。因此,它只完成一次。看到的:
$ echo "string1:string2:string3:string4:string5" | sed s/:/\\n/g
string1
string2
string3
string4
string5
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.
g代表全局,也就是说,替换必须在全球范围内进行,也就是说,在任何情况下都必须这样做。看到默认值是1,如果你输入例子2,它会做2次,等等。
All together, in your case you would need to use:
总之,在你的情况下你需要使用:
sed 's/:/\\n/g' ~/Desktop/myfile.txt
Note that you can directly use the sed ... file
syntax, instead of unnecessary piping: cat file | sed
.
注意,您可以直接使用sed…文件语法,而不是不必要的管道:cat文件| sed。
#2
25
Using \n
in sed
is non-portable. The portable way to do what you want with sed
is:
在sed中使用\n是不可移植的。使用sed的便携方式是:
sed 's/:/\
/g' ~/Desktop/myfile.txt
but in reality this isn't a job for sed
anyway, it's the job tr
was created to do:
但事实上,无论如何,这并不是一份工作,这是工作tr所要做的:
tr ':' '
' < ~/Desktop/myfile.txt
#3
20
Using simply tr :
使用简单的tr:
$ tr ':' $'\n' <<< string1:string2:string3:string4:string5
string1
string2
string3
string4
string5
If you really need sed :
如果你真的需要sed:
$ sed 's/:/\n/g' <<< string1:string2:string3:string4:string5
string1
string2
string3
string4
string5
#4
3
This might work for you (GNU sed):
这可能对你有用(GNU sed):
sed 'y/:/\n/' file
or perhaps:
或者:
sed y/:/$"\n"/ file
#5
1
This should do it:
这应该这样做:
cat ~/Desktop/myfile.txt | sed s/:/\\n/g
#6
1
If you're using gnu sed then you can use \x0A
for newline:
如果你使用gnu sed,那么你可以使用\x0A来换行:
sed 's/:/\x0A/g' ~/Desktop/myfile.txt