awksedpadding

right pad regex with spaces using sed or awk


I have a file with two fields separated with :, both fields are varying length, second field can have all sort of characters(user input). I want the first field to be right padded with spaces to fixed length of 15 characters, for first field I have a working regex @.[A-Z0-9]{4,12}.

sample:

@ABC123:"wild things here"
@7X3Z:"":":@":";:*:-user input:""
@99999X999:"also, imagine: unicode, yay!"

desired output:

@ABC123        :"wild things here"
@7X3Z          :"":":@":";:*:-user input:""
@99999X999     :"also, imagine: unicode, yay!"

There is plenty of examples how to zero pad a number, but surprisingly not a lot about general padding a regex or a field, any help using (preferably) sed or awk?


Solution

  • With perl:

    $ perl -pe 's/^[^:]+/sprintf("%-15s",$&)/e' ip.txt
    @ABC123        :"wild things here"
    @7X3Z          :"":":@":";:*:-user input:""
    @99999X999     :"also, imagine: unicode, yay!"
    

    The e flag allows you to use Perl code in replacement section. $& will have the matched portion which gets formatted by sprintf.


    With awk:

    # should work with any awk
    awk 'match($0, /^[^:]+/){printf "%-15s%s\n", substr($0,1,RLENGTH), substr($0,RLENGTH+1)}'
    
    # can be simplified with GNU awk
    awk 'match($0, /^[^:]+/, m){printf "%-15s%s\n", m[0], substr($0,RLENGTH+1)}'
    # or
    awk 'match($0, /^([^:]+)(.+)/, m){printf "%-15s%s\n", m[1], m[2]}'