Search code examples
perlparsingdelimiter

How to remove first part of every line?


Say I have a text file that is of the format:

S1:I am line one.
S2:I am line two.
S3:I am line three.
S4:I am line four.

How can I remove the ID part of each line (so that I'm only left with I am line one. and so on)? Is it better to change the input record separator $/ or use a regex that removes ID?


Solution

  • Like this perhaps? It uses a regex that matches everything up to and including the first colon : and removes it

    perl -pe 's/^[^:]*://' myfile > newfile
    

    Within a program:

    open my $fh, '<', $filename or die qq{Unable to open "$filename" for input: $!};
    
    while ( <$fh> ) {
    
        s/^[^:]*://;
    
        # Use remainder of line left in $_
    }