Search code examples
perlperl-module

remove the tabs in a particular line


This particular question I have tried and could not get anywhere in SO. My input file is a asembly file which has all the assembly instruction.

I have this input file :

.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     

All I want to do is remove the tab or spaces when I encounter lines with 0x*****

So far I managed this code, but i am unable to remove it. Tried several options and could not get it working.

Expected output

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

So far I wrote a code as

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";
        }

    }

So here if I encounter the lines with 0x* then I want to remove the extra space. Can anyone help me please. Thank you.


Solution

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

    or from command line,

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