以下代码片段使用自定义函数 HexNewLine ,它基本上转换十六进制转储并在字符处中断 0x0a
$ echo -e "\t{
> "dependencies": {
> "prettier": {
> "version": "1.18.2",
> "from": "[email protected]",
> "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz"
> }
> }
> }" | HexNewLine
00000000 09 7b 0a '.{.'
0000000a 20 20 64 65 70 65 6e 64 65 6e 63 69 65 73 3a 20 ' dependencies: '
0000001a 7b 0a '{.'
0000001c 20 20 20 20 70 72 65 74 74 69 65 72 3a 20 7b 0a ' prettier: {.'
0000002c 20 20 20 20 20 20 76 65 72 73 69 6f 6e 3a 20 31 ' version: 1'
0000003c 2e 31 38 2e 32 2c 0a '.18.2,.'
00000043 20 20 20 20 20 20 66 72 6f 6d 3a 20 70 72 65 74 ' from: pret'
00000053 74 69 65 72 40 31 2e 31 38 2e 32 2c 0a '[email protected],.'
00000060 20 20 20 20 20 20 72 65 73 6f 6c 76 65 64 3a 20 ' resolved: '
00000070 68 74 74 70 73 3a 2f 2f 72 65 67 69 73 74 72 79 'https://registry'
00000080 2e 6e 70 6d 6a 73 2e 6f 72 67 2f 70 72 65 74 74 '.npmjs.org/prett'
00000090 69 65 72 2f 2d 2f 70 72 65 74 74 69 65 72 2d 31 'ier/-/prettier-1'
000000a0 2e 31 38 2e 32 2e 74 67 7a 0a '.18.2.tgz.'
000000aa 20 20 20 20 7d 0a ' }.'
000000b0 20 20 7d 0a ' }.'
000000b4 7d 0a '}.'
- 现在我用来
tr
在换行符后添加一个制表符,但没有任何反应!
$ echo -e "\t{
> "dependencies": {
> "prettier": {
> "version": "1.18.2",
> "from": "[email protected]",
> "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz"
> }
> }
> }" | tr '\n' '\n\t'
{
dependencies: {
prettier: {
version: 1.18.2,
from: [email protected],
resolved: https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz
}
}
}
答案1
使用 sed 的解决方案。我使用 bashism$'\t'
用 填充变量\t
,并在使用此变量后使用 sed 。
echo -e "{
"dependencies": {
"prettier": {
"version": "1.18.2",
"from": "[email protected]",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz"
}
}
}" | ( TAB=$'\t' ; sed "s/^/$TAB/" )