我有一个.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的状态然后恢复它.但这只适用于通用版本.
结论
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。