Regular expression to remove all punctuation except commas in regex awk
I have a variable
local= "[ 'service center','New' ]"
I have used the following awk code to remove the square braces and single quotes,
local=gensub(/[[]']+/, "", "g", local);
Expected O/P is
local =" service center,New "
The code I wrote is not working
shell awk regular-expression
add a comment |
I have a variable
local= "[ 'service center','New' ]"
I have used the following awk code to remove the square braces and single quotes,
local=gensub(/[[]']+/, "", "g", local);
Expected O/P is
local =" service center,New "
The code I wrote is not working
shell awk regular-expression
1
Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.
– RoVo
Feb 1 at 14:53
Can you see my updated question
– mittu
Feb 1 at 14:58
Still no context. And if it's all insideawk
, you might want to remove theshell
tag from the question.
– RoVo
Feb 1 at 15:08
What exactly is not working. The AWK substitutionlocal=gensub(/[[]']+/, "", "g", local);
produces exactly the expected value. The expression/[^,[:punct:][:space:]]/
you removed with your edit does not match "([:punct:]
or[:space:]
) and not,
" but "not ([:punct:]
or[:space:]
or,
)".
– Bodo
Feb 1 at 15:09
1
@mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.
– ilkkachu
Feb 1 at 15:36
add a comment |
I have a variable
local= "[ 'service center','New' ]"
I have used the following awk code to remove the square braces and single quotes,
local=gensub(/[[]']+/, "", "g", local);
Expected O/P is
local =" service center,New "
The code I wrote is not working
shell awk regular-expression
I have a variable
local= "[ 'service center','New' ]"
I have used the following awk code to remove the square braces and single quotes,
local=gensub(/[[]']+/, "", "g", local);
Expected O/P is
local =" service center,New "
The code I wrote is not working
shell awk regular-expression
shell awk regular-expression
edited Feb 1 at 14:58
mittu
asked Feb 1 at 14:01
mittumittu
475
475
1
Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.
– RoVo
Feb 1 at 14:53
Can you see my updated question
– mittu
Feb 1 at 14:58
Still no context. And if it's all insideawk
, you might want to remove theshell
tag from the question.
– RoVo
Feb 1 at 15:08
What exactly is not working. The AWK substitutionlocal=gensub(/[[]']+/, "", "g", local);
produces exactly the expected value. The expression/[^,[:punct:][:space:]]/
you removed with your edit does not match "([:punct:]
or[:space:]
) and not,
" but "not ([:punct:]
or[:space:]
or,
)".
– Bodo
Feb 1 at 15:09
1
@mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.
– ilkkachu
Feb 1 at 15:36
add a comment |
1
Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.
– RoVo
Feb 1 at 14:53
Can you see my updated question
– mittu
Feb 1 at 14:58
Still no context. And if it's all insideawk
, you might want to remove theshell
tag from the question.
– RoVo
Feb 1 at 15:08
What exactly is not working. The AWK substitutionlocal=gensub(/[[]']+/, "", "g", local);
produces exactly the expected value. The expression/[^,[:punct:][:space:]]/
you removed with your edit does not match "([:punct:]
or[:space:]
) and not,
" but "not ([:punct:]
or[:space:]
or,
)".
– Bodo
Feb 1 at 15:09
1
@mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.
– ilkkachu
Feb 1 at 15:36
1
1
Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.
– RoVo
Feb 1 at 14:53
Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.
– RoVo
Feb 1 at 14:53
Can you see my updated question
– mittu
Feb 1 at 14:58
Can you see my updated question
– mittu
Feb 1 at 14:58
Still no context. And if it's all inside
awk
, you might want to remove the shell
tag from the question.– RoVo
Feb 1 at 15:08
Still no context. And if it's all inside
awk
, you might want to remove the shell
tag from the question.– RoVo
Feb 1 at 15:08
What exactly is not working. The AWK substitution
local=gensub(/[[]']+/, "", "g", local);
produces exactly the expected value. The expression /[^,[:punct:][:space:]]/
you removed with your edit does not match "([:punct:]
or [:space:]
) and not ,
" but "not ([:punct:]
or [:space:]
or ,
)".– Bodo
Feb 1 at 15:09
What exactly is not working. The AWK substitution
local=gensub(/[[]']+/, "", "g", local);
produces exactly the expected value. The expression /[^,[:punct:][:space:]]/
you removed with your edit does not match "([:punct:]
or [:space:]
) and not ,
" but "not ([:punct:]
or [:space:]
or ,
)".– Bodo
Feb 1 at 15:09
1
1
@mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.
– ilkkachu
Feb 1 at 15:36
@mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.
– ilkkachu
Feb 1 at 15:36
add a comment |
2 Answers
2
active
oldest
votes
You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:
awk -v local="[ 'service center','New' ]" '
BEGIN {
gsub(/,/, SUBSEP, local)
gsub(/[[:punct:]]/, "", local)
gsub(SUBSEP, ",", local)
printf("local="%s"n", local)
}'
local=" service center,New "
Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:
The default value of
SUBSEP
is the string"34"
, which contains a nonprinting character that is unlikely to appear in an awk program or in most input data
Thank you so much
– mittu
Feb 3 at 2:27
add a comment |
Please find below command and it worked fine
cat filename
local= "[ 'service center','New' ]"
sed "s/[^a-zA-Z,= " ]//g" filename
output: local= " service center,New "
Command: sed "s/[^a-zA-Z,= " ]//g" filename
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498139%2fregular-expression-to-remove-all-punctuation-except-commas-in-regex-awk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:
awk -v local="[ 'service center','New' ]" '
BEGIN {
gsub(/,/, SUBSEP, local)
gsub(/[[:punct:]]/, "", local)
gsub(SUBSEP, ",", local)
printf("local="%s"n", local)
}'
local=" service center,New "
Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:
The default value of
SUBSEP
is the string"34"
, which contains a nonprinting character that is unlikely to appear in an awk program or in most input data
Thank you so much
– mittu
Feb 3 at 2:27
add a comment |
You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:
awk -v local="[ 'service center','New' ]" '
BEGIN {
gsub(/,/, SUBSEP, local)
gsub(/[[:punct:]]/, "", local)
gsub(SUBSEP, ",", local)
printf("local="%s"n", local)
}'
local=" service center,New "
Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:
The default value of
SUBSEP
is the string"34"
, which contains a nonprinting character that is unlikely to appear in an awk program or in most input data
Thank you so much
– mittu
Feb 3 at 2:27
add a comment |
You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:
awk -v local="[ 'service center','New' ]" '
BEGIN {
gsub(/,/, SUBSEP, local)
gsub(/[[:punct:]]/, "", local)
gsub(SUBSEP, ",", local)
printf("local="%s"n", local)
}'
local=" service center,New "
Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:
The default value of
SUBSEP
is the string"34"
, which contains a nonprinting character that is unlikely to appear in an awk program or in most input data
You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:
awk -v local="[ 'service center','New' ]" '
BEGIN {
gsub(/,/, SUBSEP, local)
gsub(/[[:punct:]]/, "", local)
gsub(SUBSEP, ",", local)
printf("local="%s"n", local)
}'
local=" service center,New "
Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:
The default value of
SUBSEP
is the string"34"
, which contains a nonprinting character that is unlikely to appear in an awk program or in most input data
answered Feb 1 at 20:29
glenn jackmanglenn jackman
51.5k572111
51.5k572111
Thank you so much
– mittu
Feb 3 at 2:27
add a comment |
Thank you so much
– mittu
Feb 3 at 2:27
Thank you so much
– mittu
Feb 3 at 2:27
Thank you so much
– mittu
Feb 3 at 2:27
add a comment |
Please find below command and it worked fine
cat filename
local= "[ 'service center','New' ]"
sed "s/[^a-zA-Z,= " ]//g" filename
output: local= " service center,New "
Command: sed "s/[^a-zA-Z,= " ]//g" filename
add a comment |
Please find below command and it worked fine
cat filename
local= "[ 'service center','New' ]"
sed "s/[^a-zA-Z,= " ]//g" filename
output: local= " service center,New "
Command: sed "s/[^a-zA-Z,= " ]//g" filename
add a comment |
Please find below command and it worked fine
cat filename
local= "[ 'service center','New' ]"
sed "s/[^a-zA-Z,= " ]//g" filename
output: local= " service center,New "
Command: sed "s/[^a-zA-Z,= " ]//g" filename
Please find below command and it worked fine
cat filename
local= "[ 'service center','New' ]"
sed "s/[^a-zA-Z,= " ]//g" filename
output: local= " service center,New "
Command: sed "s/[^a-zA-Z,= " ]//g" filename
answered Feb 1 at 18:07
Praveen Kumar BSPraveen Kumar BS
1,474138
1,474138
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498139%2fregular-expression-to-remove-all-punctuation-except-commas-in-regex-awk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.
– RoVo
Feb 1 at 14:53
Can you see my updated question
– mittu
Feb 1 at 14:58
Still no context. And if it's all inside
awk
, you might want to remove theshell
tag from the question.– RoVo
Feb 1 at 15:08
What exactly is not working. The AWK substitution
local=gensub(/[[]']+/, "", "g", local);
produces exactly the expected value. The expression/[^,[:punct:][:space:]]/
you removed with your edit does not match "([:punct:]
or[:space:]
) and not,
" but "not ([:punct:]
or[:space:]
or,
)".– Bodo
Feb 1 at 15:09
1
@mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.
– ilkkachu
Feb 1 at 15:36