删除特定行中的选项卡

remove the tabs in a particular line

我已经尝试过这个特定的问题,但在 SO 中找不到任何地方。 我的输入文件是一个包含所有汇编指令的汇编文件。

我有这个输入文件:

.src_ref 0 "call.s" 24 first
      0x000000    0x5a80 0x0060         BRA.l 0x60
.src_ref 0 "call.s" 30 first
      0x000002    0x1bc5                RETI
.src_ref 0 "call.s" 31 first
      0x000003    0x6840                MOV R0L,R0L
.src_ref 0 "call.s" 35 first
      0x000004    0x1bc5     

当我遇到带有 0x*****

的行时,我只想删除制表符或 spaces

到目前为止我管理了这段代码,但我无法删除它。 尝试了几个选项,但无法正常工作。

预期输出

0x000000 0x5a80 0x0060 BRA.l 0x60
0x000002 0x1bc5 RETI
0x000003 0x6840 MOV R0L,R0L

到目前为止我写了一个代码为

my $filename = 'c:\Desktop\P4x.lst';
my $line = 0;
open(FILE,$filename) or die "Could not read from filename";
my @lines = <FILE>;
chop @lines;    


foreach my $line(@lines) 
    {
        if ($line =~ /      0x*/)
        {
            $line =~ s/[ ]*\|[ ]*\|[ ]*\|[ ]*\|[ ]*/|/g;
            print "$line\n";
        }

    }

所以在这里,如果我遇到带有 0x* 的行,那么我想删除多余的 space。 谁能帮帮我。 谢谢。

if ($line =~ s/^\h+(?=0x)//) {
  $line =~ s/\h+/ /g;
  print $line;
}

或从命令行,

perl -ne 'print if s/^\h+(?=0x)// and s/\h+/ /g' file

怎么样:

if ( $line =~ m/\A\s+0x/ ) {
    $line =~ s/\s+/ /g;
}

我不确定为什么你的正则表达式中有竖线字符 |,因为你的示例数据中有 none。

从表面上看,您只需要 s/^\s+(?=0x)//

在程序中,它看起来像这样

use strict;
use warnings;

my $filename = 'C:\Desktop\P4x.lst';
open my $fh, '<', $filename or die qq{Failed to open "$filename" for input: $!};

while (<$fh>) {
  s/^\s+(?=0x)//;
  print;
}

或作为命令行的一行

perl -pe's/^\s+(?=0x)//'

两种解决方案都将修改后的文本发送到 STDOUT,并且可以使用重定向将其写入文件。


更新

如果您想将所有白色space 更改为单个 space 以及删除前导白色 space,正如 @Сухой27 所理解的,那么您只需要改变

s/^\s+(?=0x)//

s/^\s+(?=0x)// and s/\s+/ /g