sed, replace globally a delimiter with the first part of the line

后端 未结 3 722
无人及你
无人及你 2021-01-25 13:15

Lets say I have the following lines:

1:a:b:c
2:d:e:f
3:a:b
4:a:b:c:d:e:f

how can I edit this with sed (or perl) in order to read:

1a1b1c
2d2e2f
         


        
3条回答
  •  后悔当初
    2021-01-25 13:42

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    while () {
        if ( s/^([^:]+)// ) {
            my $delim = $1;
            s/:/$delim/g;
        }
        print;
    }
    
    __DATA__
    1:a:b:c
    2:d:e:f
    3:a:b
    4:a:b:c:d:e:f
    

提交回复
热议问题