Modify a substring without altering the string in Bash
I have this string:
14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)
I want to parse it to delete minutes and seconds from the hour. The result would be the next:
14-Nov-2018 10 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)
I get this string from a file, so It would be perfect if It is achieved with a pipe. My current line is as follows:
cat $dns_logs | grep "$date" | HERE WOULD BE YOUR SOLUTION > $temp_queries
Thanks!
linux bash shell awk grep
add a comment |
I have this string:
14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)
I want to parse it to delete minutes and seconds from the hour. The result would be the next:
14-Nov-2018 10 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)
I get this string from a file, so It would be perfect if It is achieved with a pipe. My current line is as follows:
cat $dns_logs | grep "$date" | HERE WOULD BE YOUR SOLUTION > $temp_queries
Thanks!
linux bash shell awk grep
add a comment |
I have this string:
14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)
I want to parse it to delete minutes and seconds from the hour. The result would be the next:
14-Nov-2018 10 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)
I get this string from a file, so It would be perfect if It is achieved with a pipe. My current line is as follows:
cat $dns_logs | grep "$date" | HERE WOULD BE YOUR SOLUTION > $temp_queries
Thanks!
linux bash shell awk grep
I have this string:
14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)
I want to parse it to delete minutes and seconds from the hour. The result would be the next:
14-Nov-2018 10 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)
I get this string from a file, so It would be perfect if It is achieved with a pipe. My current line is as follows:
cat $dns_logs | grep "$date" | HERE WOULD BE YOUR SOLUTION > $temp_queries
Thanks!
linux bash shell awk grep
linux bash shell awk grep
asked Nov 14 '18 at 9:19
Miguel.GMiguel.G
9810
9810
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'
Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.
– Miguel.G
Nov 14 '18 at 9:49
add a comment |
If you are ok with awk
then could you please try following.
echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" |
awk '
match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
next
}
1'
1
Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!
– Miguel.G
Nov 14 '18 at 9:50
@Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.
– RavinderSingh13
Nov 15 '18 at 1:40
add a comment |
The whole line can be done as
awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53296679%2fmodify-a-substring-without-altering-the-string-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'
Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.
– Miguel.G
Nov 14 '18 at 9:49
add a comment |
sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'
Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.
– Miguel.G
Nov 14 '18 at 9:49
add a comment |
sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'
sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'
edited Nov 14 '18 at 9:57
answered Nov 14 '18 at 9:26
Martin HeraleckýMartin Heralecký
3,06721034
3,06721034
Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.
– Miguel.G
Nov 14 '18 at 9:49
add a comment |
Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.
– Miguel.G
Nov 14 '18 at 9:49
Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.
– Miguel.G
Nov 14 '18 at 9:49
Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.
– Miguel.G
Nov 14 '18 at 9:49
add a comment |
If you are ok with awk
then could you please try following.
echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" |
awk '
match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
next
}
1'
1
Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!
– Miguel.G
Nov 14 '18 at 9:50
@Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.
– RavinderSingh13
Nov 15 '18 at 1:40
add a comment |
If you are ok with awk
then could you please try following.
echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" |
awk '
match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
next
}
1'
1
Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!
– Miguel.G
Nov 14 '18 at 9:50
@Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.
– RavinderSingh13
Nov 15 '18 at 1:40
add a comment |
If you are ok with awk
then could you please try following.
echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" |
awk '
match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
next
}
1'
If you are ok with awk
then could you please try following.
echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" |
awk '
match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
next
}
1'
answered Nov 14 '18 at 9:31
RavinderSingh13RavinderSingh13
27.3k41538
27.3k41538
1
Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!
– Miguel.G
Nov 14 '18 at 9:50
@Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.
– RavinderSingh13
Nov 15 '18 at 1:40
add a comment |
1
Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!
– Miguel.G
Nov 14 '18 at 9:50
@Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.
– RavinderSingh13
Nov 15 '18 at 1:40
1
1
Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!
– Miguel.G
Nov 14 '18 at 9:50
Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!
– Miguel.G
Nov 14 '18 at 9:50
@Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.
– RavinderSingh13
Nov 15 '18 at 1:40
@Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.
– RavinderSingh13
Nov 15 '18 at 1:40
add a comment |
The whole line can be done as
awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"
add a comment |
The whole line can be done as
awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"
add a comment |
The whole line can be done as
awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"
The whole line can be done as
awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"
edited Nov 15 '18 at 6:20
answered Nov 14 '18 at 18:08
kvantourkvantour
8,63731330
8,63731330
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53296679%2fmodify-a-substring-without-altering-the-string-in-bash%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