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

后端 未结 3 721
无人及你
无人及你 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:30

    'Tis a tad tricky, but it can be done with sed (assuming the file data contains the sample input):

    $ sed '/^\(.\):/{
    s//\1/
    : retry
    s/^\(.\)\([^:]*\):/\1\2\1/
    t retry
    }' data
    1a1b1c
    2d2e2f
    3a3b
    4a4b4c4d4e4f
    $
    

    You may be able to flatten the script to one line with semi-colons; sed on MacOS X is a bit cranky at times and objected to some parts, so it is split out into 6 lines. The first line matches lines starting with a single character and a colon and starts a sequence of operations for when that is recognized. The first substitute replaces, for example, '1:' by just '1'. The : retry is a label for branching too - a key part of this. The next substitution copies the first character on the line over the first colon. The t retry goes back to the label if the substitute changed anything. The last line delimits the entire sequence of operations for the initially matched line.

    0 讨论(0)
  • 2021-01-25 13:40
    use feature qw/ say /;
    use strict;
    use warnings;
    while( <DATA> ) {
        chomp;
        my @elements = split /:/;
        my $interject = shift @elements;
        local $" = $interject;
        say $interject, "@elements";
    }
    
    __DATA__
    1:a:b:c
    2:d:e:f
    3:a:b
    4:a:b:c:d:e:f
    

    Or on the linux shell command line:

    perl -aF/:/ -pe '$i=shift @F;$_=$i.join $i,@F;' infile.txt

    0 讨论(0)
  • 2021-01-25 13:42
    #!/usr/bin/perl
    use warnings;
    use strict;
    
    while (<DATA>) {
        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
    
    0 讨论(0)
提交回复
热议问题