Edit comma seperated fields
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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
awk sed regular-expression
add a comment |Â
up vote
0
down vote
favorite
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
awk sed regular-expression
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
awk sed regular-expression
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
awk sed regular-expression
asked Jul 3 at 20:56
ñÃÂñýàñüÃÂÃÂùcñ÷
396418
396418
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
With GNU Awk, using patsplit
to split the second comma delimited field into subfields of at most two decimal digits:
$ gawk 'BEGINOFS=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(dd?)-?(d+)sprintf "%02d/%d", $1, $2e;
$_ = join ",", @F
' file
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
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
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
With GNU Awk, using patsplit
to split the second comma delimited field into subfields of at most two decimal digits:
$ gawk 'BEGINOFS=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(dd?)-?(d+)sprintf "%02d/%d", $1, $2e;
$_ = join ",", @F
' file
add a comment |Â
up vote
1
down vote
accepted
With GNU Awk, using patsplit
to split the second comma delimited field into subfields of at most two decimal digits:
$ gawk 'BEGINOFS=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(dd?)-?(d+)sprintf "%02d/%d", $1, $2e;
$_ = join ",", @F
' file
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
With GNU Awk, using patsplit
to split the second comma delimited field into subfields of at most two decimal digits:
$ gawk 'BEGINOFS=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(dd?)-?(d+)sprintf "%02d/%d", $1, $2e;
$_ = join ",", @F
' file
With GNU Awk, using patsplit
to split the second comma delimited field into subfields of at most two decimal digits:
$ gawk 'BEGINOFS=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(dd?)-?(d+)sprintf "%02d/%d", $1, $2e;
$_ = join ",", @F
' file
edited Jul 3 at 21:38
answered Jul 3 at 21:20
steeldriver
30.9k34877
30.9k34877
add a comment |Â
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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
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
edited Jul 6 at 6:48
answered Jul 6 at 5:34
Nisha
12
12
add a comment |Â
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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
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
answered Jul 6 at 8:18
Nisha
12
12
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453308%2fedit-comma-seperated-fields%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password