Replace special characters in specific field
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
how to replace special character in specific field?
INPUT
xxxx,11/2019,xxx
OUTPUT
xxxx,11,2019,xxx
As you see that am looking to replace /
with ,
in $2
only.
So kindly explain for me your command to achieve that.
Regards,
linux awk sed regular-expression cut
add a comment |Â
up vote
-1
down vote
favorite
how to replace special character in specific field?
INPUT
xxxx,11/2019,xxx
OUTPUT
xxxx,11,2019,xxx
As you see that am looking to replace /
with ,
in $2
only.
So kindly explain for me your command to achieve that.
Regards,
linux awk sed regular-expression cut
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
how to replace special character in specific field?
INPUT
xxxx,11/2019,xxx
OUTPUT
xxxx,11,2019,xxx
As you see that am looking to replace /
with ,
in $2
only.
So kindly explain for me your command to achieve that.
Regards,
linux awk sed regular-expression cut
how to replace special character in specific field?
INPUT
xxxx,11/2019,xxx
OUTPUT
xxxx,11,2019,xxx
As you see that am looking to replace /
with ,
in $2
only.
So kindly explain for me your command to achieve that.
Regards,
linux awk sed regular-expression cut
asked Dec 24 '17 at 8:40
ñÃÂñýàñüÃÂÃÂùcñ÷
407418
407418
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
awk -F, -v OFS=, 'gsub(///,",",$2); print'
This uses awk
's gsub()
function to do a global regexp search and replace on field 2.
If you want to replace only the first occurence of /
in $2, use sub()
rather than gsub()
.
NOTE: the default output field separator OFS
is a space. You need to set it to ,
(same as the input field separator FS
), otherwise the print
will output all the fields with spaces separating them.
From the man page for GNU awk
:
gsub(r, s [, t])
For each substring matching the regular expression
r
in the string
t
, substitute the strings
, and return the number of substitutions.
If
t
is not supplied, use $0.
An
&
in the replacement text is replaced with the text that was
actually matched.
Use
&
to get a literal &. (This must be typed as\&
; see GAWK:
Effective AWK Programming for a fuller discussion of the rules for
&'s and backslashes in the replacement text ofsub()
,gsub()
, and
gensub()
.)
add a comment |Â
up vote
0
down vote
The command given in the previous answer can be much simpler. Tested and working fine.
Since changes need to be done in second column, I have added the same content in a second column and tested.
echo "echo "xxxx,11/2019,xxx xxxx,11/2019,xxx" " |
awk 'print gsub("/",",",$2);print $0'
Output:
xxxx,11/2019,xxx xxxx,11,2019,xxx
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
awk -F, -v OFS=, 'gsub(///,",",$2); print'
This uses awk
's gsub()
function to do a global regexp search and replace on field 2.
If you want to replace only the first occurence of /
in $2, use sub()
rather than gsub()
.
NOTE: the default output field separator OFS
is a space. You need to set it to ,
(same as the input field separator FS
), otherwise the print
will output all the fields with spaces separating them.
From the man page for GNU awk
:
gsub(r, s [, t])
For each substring matching the regular expression
r
in the string
t
, substitute the strings
, and return the number of substitutions.
If
t
is not supplied, use $0.
An
&
in the replacement text is replaced with the text that was
actually matched.
Use
&
to get a literal &. (This must be typed as\&
; see GAWK:
Effective AWK Programming for a fuller discussion of the rules for
&'s and backslashes in the replacement text ofsub()
,gsub()
, and
gensub()
.)
add a comment |Â
up vote
2
down vote
accepted
awk -F, -v OFS=, 'gsub(///,",",$2); print'
This uses awk
's gsub()
function to do a global regexp search and replace on field 2.
If you want to replace only the first occurence of /
in $2, use sub()
rather than gsub()
.
NOTE: the default output field separator OFS
is a space. You need to set it to ,
(same as the input field separator FS
), otherwise the print
will output all the fields with spaces separating them.
From the man page for GNU awk
:
gsub(r, s [, t])
For each substring matching the regular expression
r
in the string
t
, substitute the strings
, and return the number of substitutions.
If
t
is not supplied, use $0.
An
&
in the replacement text is replaced with the text that was
actually matched.
Use
&
to get a literal &. (This must be typed as\&
; see GAWK:
Effective AWK Programming for a fuller discussion of the rules for
&'s and backslashes in the replacement text ofsub()
,gsub()
, and
gensub()
.)
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
awk -F, -v OFS=, 'gsub(///,",",$2); print'
This uses awk
's gsub()
function to do a global regexp search and replace on field 2.
If you want to replace only the first occurence of /
in $2, use sub()
rather than gsub()
.
NOTE: the default output field separator OFS
is a space. You need to set it to ,
(same as the input field separator FS
), otherwise the print
will output all the fields with spaces separating them.
From the man page for GNU awk
:
gsub(r, s [, t])
For each substring matching the regular expression
r
in the string
t
, substitute the strings
, and return the number of substitutions.
If
t
is not supplied, use $0.
An
&
in the replacement text is replaced with the text that was
actually matched.
Use
&
to get a literal &. (This must be typed as\&
; see GAWK:
Effective AWK Programming for a fuller discussion of the rules for
&'s and backslashes in the replacement text ofsub()
,gsub()
, and
gensub()
.)
awk -F, -v OFS=, 'gsub(///,",",$2); print'
This uses awk
's gsub()
function to do a global regexp search and replace on field 2.
If you want to replace only the first occurence of /
in $2, use sub()
rather than gsub()
.
NOTE: the default output field separator OFS
is a space. You need to set it to ,
(same as the input field separator FS
), otherwise the print
will output all the fields with spaces separating them.
From the man page for GNU awk
:
gsub(r, s [, t])
For each substring matching the regular expression
r
in the string
t
, substitute the strings
, and return the number of substitutions.
If
t
is not supplied, use $0.
An
&
in the replacement text is replaced with the text that was
actually matched.
Use
&
to get a literal &. (This must be typed as\&
; see GAWK:
Effective AWK Programming for a fuller discussion of the rules for
&'s and backslashes in the replacement text ofsub()
,gsub()
, and
gensub()
.)
edited Dec 24 '17 at 8:54
answered Dec 24 '17 at 8:43
cas
37.7k44394
37.7k44394
add a comment |Â
add a comment |Â
up vote
0
down vote
The command given in the previous answer can be much simpler. Tested and working fine.
Since changes need to be done in second column, I have added the same content in a second column and tested.
echo "echo "xxxx,11/2019,xxx xxxx,11/2019,xxx" " |
awk 'print gsub("/",",",$2);print $0'
Output:
xxxx,11/2019,xxx xxxx,11,2019,xxx
add a comment |Â
up vote
0
down vote
The command given in the previous answer can be much simpler. Tested and working fine.
Since changes need to be done in second column, I have added the same content in a second column and tested.
echo "echo "xxxx,11/2019,xxx xxxx,11/2019,xxx" " |
awk 'print gsub("/",",",$2);print $0'
Output:
xxxx,11/2019,xxx xxxx,11,2019,xxx
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The command given in the previous answer can be much simpler. Tested and working fine.
Since changes need to be done in second column, I have added the same content in a second column and tested.
echo "echo "xxxx,11/2019,xxx xxxx,11/2019,xxx" " |
awk 'print gsub("/",",",$2);print $0'
Output:
xxxx,11/2019,xxx xxxx,11,2019,xxx
The command given in the previous answer can be much simpler. Tested and working fine.
Since changes need to be done in second column, I have added the same content in a second column and tested.
echo "echo "xxxx,11/2019,xxx xxxx,11/2019,xxx" " |
awk 'print gsub("/",",",$2);print $0'
Output:
xxxx,11/2019,xxx xxxx,11,2019,xxx
edited Jan 5 at 13:41
grg
1857
1857
answered Dec 24 '17 at 11:50
Praveen Kumar BS
1,010128
1,010128
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%2f412780%2freplace-special-characters-in-specific-field%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