0

Input:

1,012018,111
2,1-2018,111
3,10-2018,111

Output:

1,01/2018,111
2,01/2018,111
3,10/2018,111

How can i achieve that which meaning edit of month and year to be mm/yyyy

1

With GNU Awk, using patsplit to split the second comma delimited field into subfields of at most two decimal digits:

$ gawk 'BEGIN{OFS=FS=","} {patsplit($2,a,/[0-9][0-9]?/); $2 = sprintf("%02d/%d%d", a[1], a[2], a[3])} 1' file
1,01/2018,111
2,01/2018,111
3,10/2018,111

If Perl is an option

perl -F, -lpe '
  $F[1] =~ s{(\d\d?)-?(\d+)}{sprintf "%02d/%d", $1, $2}e; 
  $_ = join ",", @F
' file
0

sed -e 's/^(.),(.),(.)/\2/g' -e 's/([0-9]{1,2})([0-9]{4})/\1-\2/g' file*

s/^(.),(.),(.*)/\2/g - splits based on commas and gets 2nd value s/([0-9]{1,2})([0-9]{4})/\1-\2/g - splits and inserts '-' between year and month

0

sed -e 's/^(.),([0-9]{1,2})([0-9]{4}),(.)/\1,\2-\3,\4/g' test.txt

  • ^(.*), = capture until first ',' and stored in \1
  • ([0-9]{1,2}) = capture 1 or 2 digit number and store in \2
  • ([0-9]{4}) = capture 4 didit no and store in \3
  • ,(.*) \4 = capture the rest from ',' and store it in \4
  • And replace accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.