Remove some text with shell scripts?
I wanted to get some help on how I can use cygwin, which has bash to shell script the following:
I have a txt file that contains the following:
action "action1"
reset
type xformbin
http-method-limited POST
http-method-limited2 POST
exit
action "action2"
reset
admin-state disabled
type results
http-method-limited POST
http-method-limited2 POST
exit
action "action3"
reset
admin-state disabled
type setvar
http-method-limited POST
http-method-limited2 POST
exit
I was hoping a shell script could be written to remove the block where admin-state = disabled?
So, I'm hoping I can iterate through the txt file and if admin-state = disabled, remove everything between "action" and "exit" from that particular block.
I would expect the following final results from the sample text:
action "action1"
reset
type xformbin
http-method-limited POST
http-method-limited2 POST
exit
Thank you.
shell scripting cygwin
add a comment |
I wanted to get some help on how I can use cygwin, which has bash to shell script the following:
I have a txt file that contains the following:
action "action1"
reset
type xformbin
http-method-limited POST
http-method-limited2 POST
exit
action "action2"
reset
admin-state disabled
type results
http-method-limited POST
http-method-limited2 POST
exit
action "action3"
reset
admin-state disabled
type setvar
http-method-limited POST
http-method-limited2 POST
exit
I was hoping a shell script could be written to remove the block where admin-state = disabled?
So, I'm hoping I can iterate through the txt file and if admin-state = disabled, remove everything between "action" and "exit" from that particular block.
I would expect the following final results from the sample text:
action "action1"
reset
type xformbin
http-method-limited POST
http-method-limited2 POST
exit
Thank you.
shell scripting cygwin
sed '/disabled/,/exit/ d' infile
? Maybe write the output to another location until you're confident it's doing what you want. I'm leery of jumping straight to-i
.
– Paul Hodges
Nov 26 '18 at 21:12
thank you @PaulHodges although with that, I get: action "action2" reset action "action2" reset admin-state enabled type xform http-method GET http-method-limited POST http-method-limited2 POST exit action "action3" reset ... any idea to get rid of the residual "action" to "reset" parts?
– cerebraldecypher
Nov 27 '18 at 13:51
When I run the command I get exactly what you list above as the result you said you wanted. Please edit your post to clarify what you hoped to get.
– Paul Hodges
Nov 27 '18 at 14:52
@PaulHodges Ah, I see what happened... my file has line breaks ... let me update the post.
– cerebraldecypher
Nov 27 '18 at 15:19
add a comment |
I wanted to get some help on how I can use cygwin, which has bash to shell script the following:
I have a txt file that contains the following:
action "action1"
reset
type xformbin
http-method-limited POST
http-method-limited2 POST
exit
action "action2"
reset
admin-state disabled
type results
http-method-limited POST
http-method-limited2 POST
exit
action "action3"
reset
admin-state disabled
type setvar
http-method-limited POST
http-method-limited2 POST
exit
I was hoping a shell script could be written to remove the block where admin-state = disabled?
So, I'm hoping I can iterate through the txt file and if admin-state = disabled, remove everything between "action" and "exit" from that particular block.
I would expect the following final results from the sample text:
action "action1"
reset
type xformbin
http-method-limited POST
http-method-limited2 POST
exit
Thank you.
shell scripting cygwin
I wanted to get some help on how I can use cygwin, which has bash to shell script the following:
I have a txt file that contains the following:
action "action1"
reset
type xformbin
http-method-limited POST
http-method-limited2 POST
exit
action "action2"
reset
admin-state disabled
type results
http-method-limited POST
http-method-limited2 POST
exit
action "action3"
reset
admin-state disabled
type setvar
http-method-limited POST
http-method-limited2 POST
exit
I was hoping a shell script could be written to remove the block where admin-state = disabled?
So, I'm hoping I can iterate through the txt file and if admin-state = disabled, remove everything between "action" and "exit" from that particular block.
I would expect the following final results from the sample text:
action "action1"
reset
type xformbin
http-method-limited POST
http-method-limited2 POST
exit
Thank you.
shell scripting cygwin
shell scripting cygwin
edited Nov 29 '18 at 6:43
cerebraldecypher
asked Nov 26 '18 at 20:52
cerebraldecyphercerebraldecypher
163
163
sed '/disabled/,/exit/ d' infile
? Maybe write the output to another location until you're confident it's doing what you want. I'm leery of jumping straight to-i
.
– Paul Hodges
Nov 26 '18 at 21:12
thank you @PaulHodges although with that, I get: action "action2" reset action "action2" reset admin-state enabled type xform http-method GET http-method-limited POST http-method-limited2 POST exit action "action3" reset ... any idea to get rid of the residual "action" to "reset" parts?
– cerebraldecypher
Nov 27 '18 at 13:51
When I run the command I get exactly what you list above as the result you said you wanted. Please edit your post to clarify what you hoped to get.
– Paul Hodges
Nov 27 '18 at 14:52
@PaulHodges Ah, I see what happened... my file has line breaks ... let me update the post.
– cerebraldecypher
Nov 27 '18 at 15:19
add a comment |
sed '/disabled/,/exit/ d' infile
? Maybe write the output to another location until you're confident it's doing what you want. I'm leery of jumping straight to-i
.
– Paul Hodges
Nov 26 '18 at 21:12
thank you @PaulHodges although with that, I get: action "action2" reset action "action2" reset admin-state enabled type xform http-method GET http-method-limited POST http-method-limited2 POST exit action "action3" reset ... any idea to get rid of the residual "action" to "reset" parts?
– cerebraldecypher
Nov 27 '18 at 13:51
When I run the command I get exactly what you list above as the result you said you wanted. Please edit your post to clarify what you hoped to get.
– Paul Hodges
Nov 27 '18 at 14:52
@PaulHodges Ah, I see what happened... my file has line breaks ... let me update the post.
– cerebraldecypher
Nov 27 '18 at 15:19
sed '/disabled/,/exit/ d' infile
? Maybe write the output to another location until you're confident it's doing what you want. I'm leery of jumping straight to -i
.– Paul Hodges
Nov 26 '18 at 21:12
sed '/disabled/,/exit/ d' infile
? Maybe write the output to another location until you're confident it's doing what you want. I'm leery of jumping straight to -i
.– Paul Hodges
Nov 26 '18 at 21:12
thank you @PaulHodges although with that, I get: action "action2" reset action "action2" reset admin-state enabled type xform http-method GET http-method-limited POST http-method-limited2 POST exit action "action3" reset ... any idea to get rid of the residual "action" to "reset" parts?
– cerebraldecypher
Nov 27 '18 at 13:51
thank you @PaulHodges although with that, I get: action "action2" reset action "action2" reset admin-state enabled type xform http-method GET http-method-limited POST http-method-limited2 POST exit action "action3" reset ... any idea to get rid of the residual "action" to "reset" parts?
– cerebraldecypher
Nov 27 '18 at 13:51
When I run the command I get exactly what you list above as the result you said you wanted. Please edit your post to clarify what you hoped to get.
– Paul Hodges
Nov 27 '18 at 14:52
When I run the command I get exactly what you list above as the result you said you wanted. Please edit your post to clarify what you hoped to get.
– Paul Hodges
Nov 27 '18 at 14:52
@PaulHodges Ah, I see what happened... my file has line breaks ... let me update the post.
– cerebraldecypher
Nov 27 '18 at 15:19
@PaulHodges Ah, I see what happened... my file has line breaks ... let me update the post.
– cerebraldecypher
Nov 27 '18 at 15:19
add a comment |
2 Answers
2
active
oldest
votes
So you want the whole block ignored if it has 'disabled' in the middle, but printed if it does not.
sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' x
This will do nothing unless in a block from action
to exit
. In those -
If a line has action
, store it and delete the pattern space to trigger the next read.
Otherwise append the line to the stored hold space.
If the line had exit
,
- swap the hold space into the pattern space
- if the collected pattern has
disabled
, delete it to trigger reading the next record; - if not, print it, then delete it to trigger reading the next record.
The output:
$: sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' infile
action "action2"
reset
admin-state enabled
type xform
http-method GET
http-method-limited POST
http-method-limited2 POST exit
Hope that helps.
Thank you @PaulHodges Really appreciate it :)
– cerebraldecypher
Dec 3 '18 at 18:26
You're quite welcome.
– Paul Hodges
Dec 3 '18 at 18:42
add a comment |
do you have perl?
local $/="exit";
while (<>) {
print if not /admin-state disabled/;
}
Should that beprint "$_exit"
or some such? Won't it lose the "exit" otherwise?
– Paul Hodges
Dec 3 '18 at 18:44
no, it prints matching paragraph (which includes the "exit"), did you run it?
– nlsdkd
Dec 3 '18 at 19:06
1
You didn't strip it. My bad. :) Been a while since I've written perl, getting out of practice, lol
– Paul Hodges
Dec 4 '18 at 14:45
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%2f53488876%2fremove-some-text-with-shell-scripts%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
So you want the whole block ignored if it has 'disabled' in the middle, but printed if it does not.
sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' x
This will do nothing unless in a block from action
to exit
. In those -
If a line has action
, store it and delete the pattern space to trigger the next read.
Otherwise append the line to the stored hold space.
If the line had exit
,
- swap the hold space into the pattern space
- if the collected pattern has
disabled
, delete it to trigger reading the next record; - if not, print it, then delete it to trigger reading the next record.
The output:
$: sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' infile
action "action2"
reset
admin-state enabled
type xform
http-method GET
http-method-limited POST
http-method-limited2 POST exit
Hope that helps.
Thank you @PaulHodges Really appreciate it :)
– cerebraldecypher
Dec 3 '18 at 18:26
You're quite welcome.
– Paul Hodges
Dec 3 '18 at 18:42
add a comment |
So you want the whole block ignored if it has 'disabled' in the middle, but printed if it does not.
sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' x
This will do nothing unless in a block from action
to exit
. In those -
If a line has action
, store it and delete the pattern space to trigger the next read.
Otherwise append the line to the stored hold space.
If the line had exit
,
- swap the hold space into the pattern space
- if the collected pattern has
disabled
, delete it to trigger reading the next record; - if not, print it, then delete it to trigger reading the next record.
The output:
$: sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' infile
action "action2"
reset
admin-state enabled
type xform
http-method GET
http-method-limited POST
http-method-limited2 POST exit
Hope that helps.
Thank you @PaulHodges Really appreciate it :)
– cerebraldecypher
Dec 3 '18 at 18:26
You're quite welcome.
– Paul Hodges
Dec 3 '18 at 18:42
add a comment |
So you want the whole block ignored if it has 'disabled' in the middle, but printed if it does not.
sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' x
This will do nothing unless in a block from action
to exit
. In those -
If a line has action
, store it and delete the pattern space to trigger the next read.
Otherwise append the line to the stored hold space.
If the line had exit
,
- swap the hold space into the pattern space
- if the collected pattern has
disabled
, delete it to trigger reading the next record; - if not, print it, then delete it to trigger reading the next record.
The output:
$: sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' infile
action "action2"
reset
admin-state enabled
type xform
http-method GET
http-method-limited POST
http-method-limited2 POST exit
Hope that helps.
So you want the whole block ignored if it has 'disabled' in the middle, but printed if it does not.
sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' x
This will do nothing unless in a block from action
to exit
. In those -
If a line has action
, store it and delete the pattern space to trigger the next read.
Otherwise append the line to the stored hold space.
If the line had exit
,
- swap the hold space into the pattern space
- if the collected pattern has
disabled
, delete it to trigger reading the next record; - if not, print it, then delete it to trigger reading the next record.
The output:
$: sed -n '
/action/,/exit/ {
/action/ { x; d; }
H;
/exit/ { x;
/disabled/ d;
p; d;
}
}' infile
action "action2"
reset
admin-state enabled
type xform
http-method GET
http-method-limited POST
http-method-limited2 POST exit
Hope that helps.
answered Nov 27 '18 at 15:45
Paul HodgesPaul Hodges
3,2411422
3,2411422
Thank you @PaulHodges Really appreciate it :)
– cerebraldecypher
Dec 3 '18 at 18:26
You're quite welcome.
– Paul Hodges
Dec 3 '18 at 18:42
add a comment |
Thank you @PaulHodges Really appreciate it :)
– cerebraldecypher
Dec 3 '18 at 18:26
You're quite welcome.
– Paul Hodges
Dec 3 '18 at 18:42
Thank you @PaulHodges Really appreciate it :)
– cerebraldecypher
Dec 3 '18 at 18:26
Thank you @PaulHodges Really appreciate it :)
– cerebraldecypher
Dec 3 '18 at 18:26
You're quite welcome.
– Paul Hodges
Dec 3 '18 at 18:42
You're quite welcome.
– Paul Hodges
Dec 3 '18 at 18:42
add a comment |
do you have perl?
local $/="exit";
while (<>) {
print if not /admin-state disabled/;
}
Should that beprint "$_exit"
or some such? Won't it lose the "exit" otherwise?
– Paul Hodges
Dec 3 '18 at 18:44
no, it prints matching paragraph (which includes the "exit"), did you run it?
– nlsdkd
Dec 3 '18 at 19:06
1
You didn't strip it. My bad. :) Been a while since I've written perl, getting out of practice, lol
– Paul Hodges
Dec 4 '18 at 14:45
add a comment |
do you have perl?
local $/="exit";
while (<>) {
print if not /admin-state disabled/;
}
Should that beprint "$_exit"
or some such? Won't it lose the "exit" otherwise?
– Paul Hodges
Dec 3 '18 at 18:44
no, it prints matching paragraph (which includes the "exit"), did you run it?
– nlsdkd
Dec 3 '18 at 19:06
1
You didn't strip it. My bad. :) Been a while since I've written perl, getting out of practice, lol
– Paul Hodges
Dec 4 '18 at 14:45
add a comment |
do you have perl?
local $/="exit";
while (<>) {
print if not /admin-state disabled/;
}
do you have perl?
local $/="exit";
while (<>) {
print if not /admin-state disabled/;
}
answered Nov 26 '18 at 22:02
nlsdkdnlsdkd
12115
12115
Should that beprint "$_exit"
or some such? Won't it lose the "exit" otherwise?
– Paul Hodges
Dec 3 '18 at 18:44
no, it prints matching paragraph (which includes the "exit"), did you run it?
– nlsdkd
Dec 3 '18 at 19:06
1
You didn't strip it. My bad. :) Been a while since I've written perl, getting out of practice, lol
– Paul Hodges
Dec 4 '18 at 14:45
add a comment |
Should that beprint "$_exit"
or some such? Won't it lose the "exit" otherwise?
– Paul Hodges
Dec 3 '18 at 18:44
no, it prints matching paragraph (which includes the "exit"), did you run it?
– nlsdkd
Dec 3 '18 at 19:06
1
You didn't strip it. My bad. :) Been a while since I've written perl, getting out of practice, lol
– Paul Hodges
Dec 4 '18 at 14:45
Should that be
print "$_exit"
or some such? Won't it lose the "exit" otherwise?– Paul Hodges
Dec 3 '18 at 18:44
Should that be
print "$_exit"
or some such? Won't it lose the "exit" otherwise?– Paul Hodges
Dec 3 '18 at 18:44
no, it prints matching paragraph (which includes the "exit"), did you run it?
– nlsdkd
Dec 3 '18 at 19:06
no, it prints matching paragraph (which includes the "exit"), did you run it?
– nlsdkd
Dec 3 '18 at 19:06
1
1
You didn't strip it. My bad. :) Been a while since I've written perl, getting out of practice, lol
– Paul Hodges
Dec 4 '18 at 14:45
You didn't strip it. My bad. :) Been a while since I've written perl, getting out of practice, lol
– Paul Hodges
Dec 4 '18 at 14:45
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%2f53488876%2fremove-some-text-with-shell-scripts%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
sed '/disabled/,/exit/ d' infile
? Maybe write the output to another location until you're confident it's doing what you want. I'm leery of jumping straight to-i
.– Paul Hodges
Nov 26 '18 at 21:12
thank you @PaulHodges although with that, I get: action "action2" reset action "action2" reset admin-state enabled type xform http-method GET http-method-limited POST http-method-limited2 POST exit action "action3" reset ... any idea to get rid of the residual "action" to "reset" parts?
– cerebraldecypher
Nov 27 '18 at 13:51
When I run the command I get exactly what you list above as the result you said you wanted. Please edit your post to clarify what you hoped to get.
– Paul Hodges
Nov 27 '18 at 14:52
@PaulHodges Ah, I see what happened... my file has line breaks ... let me update the post.
– cerebraldecypher
Nov 27 '18 at 15:19