加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用vim,shell或python移动小数点

发布时间:2020-12-15 22:20:38 所属栏目:安全 来源:网络整理
导读:我有一个.pl文件,包含一个非结构化的头文件和一些特定的X Y Z结构.标题没有“.”在那里.您可以在下面找到此文件的片段: Header (in total 29 lines)ILINEVRTX 1 545057.78564453125 3800905.201171875 -15000 VRTX 2 545184.49072265625 3800765.451171875
我有一个.pl文件,包含一个非结构化的头文件和一些特定的X Y Z结构.标题没有“.”在那里.您可以在下面找到此文件的片段:

Header (in total 29 lines)
ILINE
VRTX 1 545057.78564453125 3800905.201171875 -15000 
VRTX 2 545184.49072265625 3800765.451171875 -15000 
VRTX 3 545310.91650390625 3800625.970703125 -15000 
SEG 1 2 
SEG 2 3 
ILINE
VRTX 136 551295.84606933594 3799015.443359375 -15000 
VRTX 137 551293.82849121094 3798841.880859375 -15000 
VRTX 138 551290.57849121094 3798661.892578125 -15000  
SEG 136 137 
SEG 137 138

我想要实现的是将我的X和Y值的小数点移到左边,如下所示:

VRTX 1 5450.5778564453125 38009.05201171875 -15000 
VRTX 2 5451.8449072265625 38007.65451171875 -15000 
VRTX 3 5453.1091650390625 38006.25970703125 -15000

我认为正则表达式可能会有所帮助,但我没有使用Python和Shell的经验.任何帮助表示赞赏.

解决方法

由于问题上有vim标签:

/.
qaqqanx2hi!<esc>@aq
@a
:%s/!/./g

假设没有!在文本中,否则你必须用不同的字符替换它.当然在vim中还有其他解决方案,但这是我现在能想到的最简单的解决方案.

分解:

/.                  " searches every `.` in the file 
                      " (and "marks" them as searchresults)

qaqqanx2hi!<esc>@aq   " Our macro we use:
qaq                   " records an empty macro into register a 
                      " (This ensures the register is empty)
   qa                 " This starts recording a macro into register a again
     nx2hi!<esc>      " `n` jumps to the next search result," `2hxhi!<esc>` inserts a `!` 2 chars left of where
                      " the `.` has been,and exits insert mode
                @a    " calls the macro at register `a`,since we emptied it," nothing happens yet,but once we save this macro it will
                      " be in register `a`. So it will call itself until it 
                      " reaches an abort conditions (no more `.` found in the
                      " text,thats why we replace `.` with `!` here,else
                      " the macro would move the `.` over and over to the left
                  q   " Stops recording the macro and saves it (to register `a`)

@a                    " calls the macro in register `a`

:%s/!/./g             " This just replaces all `!` in the file with `.`

可能更清洁的解决方案:使用nowrapscan(感谢@Francesco)

/.
:set nows
qaqqax3hpn@aq
@a
:set ws

注意:宏稍微改变了顺序.我们必须先改变立场.然后跳转到下一个搜索出现,否则我们将错过第一次出现,因为搜索不再包围arof eof.

注意:最好先保存ws的状态然后恢复它.但这只适用于通用版本.

结论

复杂但灵活的解决方案.如果您不需要灵活,这里的其他vim解决方案更容易(我不会判断其他解决方案)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读