消除LINUX中的第一个和最后一个空格或制表符

时间:2017-10-30 14:03:14

标签: linux

我有一个json文件,其元素列出如下:

"URL": [      "R-MMU-72200"    ],

"GENES": [      "4706167874","4706170968"    ],

"NAME": [      "mRNA Editing: C to U Conversion"    ],

"ORGANISM": [      "Mus musculus"    ],

"PATH_MAP": [      "R-MMU-72200"    ]  },

我想消除括号内每个字符串开头和结尾的空格或制表符。我想要这个:

 "URL": ["R-MMU-72200"],

 "GENES": ["4706167874","4706170968"],

 "NAME": ["mRNA Editing: C to U Conversion"],

 "ORGANISM": ["Mus musculus"],

 "PATH_MAP": ["R-MMU-72200"]  },

提前致谢

2 个答案:

答案 0 :(得分:1)

这样的东西?

sed 's/\s\s//g'

结果:

"URL": ["R-MMU-72200"],

答案 1 :(得分:1)

首先,假设你的意思是:

$ cat file
{"URL": [      "R-MMU-72200"    ],
"GENES": [      "4706167874","4706170968"    ],
"NAME": [      "mRNA Editing: C to U Conversion"    ],
"ORGANISM": [      "Mus musculus"    ],
"PATH_MAP": [      "R-MMU-72200"    ]  }

您可以通过JSON解析器运行它以删除空格。

以下是ruby的示例:

$ ruby -r json -e 'p JSON.parse($<.read)' file
{"URL"=>["R-MMU-72200"], "GENES"=>["4706167874", "4706170968"], "NAME"=>["mRNA Editing: C to U Conversion"], "ORGANISM"=>["Mus musculus"], "PATH_MAP"=>["R-MMU-72200"]}

或者,您可以使用jq

$ jq '.' file