mac osx sed 替换模式中使用换行符
要把一json格式的字符串中的数据导入到sqlite数据库里面。
因为json只有一行,所以想在转换后的sql语句中分行显示。
需要在”;”和”insert”中间插入以换行符。
查找模式中可以使用”\n”来匹配换行符。但是替换模式中则不可以这样用。文档中有明确的说明。
The escape sequence \n matches a newline character embedded in the
1
2
3 > pattern space. You cannot, however, use a literal newline character
> in an address or in the substitute command.
>
文档中也明确说明可以在替换匹配模式中使用换行符。
A line can be split by substituting a newline character into it.
To specify a newline character in the replacement string, precede
it with a backslash.
但是文档中没有明确换行符应该怎么写。
经测试写法如下:
imac$ echo “a,b,c” |sed ‘s/,/
/g’
a b
c
mac osx上的sed还是比较弱,很多新特性不支持。
包含中文字符时,需要设置语言选项,否则会出现“illegal byte sequence”
tr命令也存在类似问题。
1 | export LC_CTYPE=C |