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?
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]}'
substr($0,1,RLENGTH)
or m[0]
will give contents of first field. I have used 1
instead of the usual RSTART
here since we are matching start of linesubstr($0,RLENGTH+1)
will give rest of the line contents (i.e. from the first :
)match
function.