使用正则表达式shell命令

时间:2019-01-14 19:18:27

标签: regex shell perl

我有一个文件,其数据如下。此处的数据为0-64,并分配了特殊字符“ =>”。预期的输出如下所示。

array databse
(
 user_content_1=>0
 no_entry=>1
 user_content_2=>2
 user_content_2=>3`
 left=>4
 ....
 )

我想要输出如下。

array databse
(
  0 user_content_1
  1 no_entry
  2 user_content_2
  3 user_content_2
  4 left
  ....
 )

谢谢

1 个答案:

答案 0 :(得分:1)

这应该做到:

$ perl -pe 's/(\w+)=>(\d+)/$2 $1/'

示例运行测试数据:

$ cat dummy.txt
array databse
(
     user_content_1=>0
     no_entry=>1
     user_content_2=>2
     user_content_2=>3`
     left=>4
     ....
)

$ perl -pe 's/(\w+)=>(\d+)/$2 $1/' dummy.txt
array databse
(
     0 user_content_1
     1 no_entry
     2 user_content_2
     3 user_content_2`
     4 left
     ....
)

如果您的真实数据包含的字符超过示例显示的字符,则必须处理\w+\d+

相关问题