how to remove first N-1 lines up to a given input string
I'm trying to remove the first N-1 lines up to the first match of a specified string
I am very new to scripting, but from my googling I believe I have the two commands I need, but don't know how to use them together:
grep -n -m 1 'STRING' file.txt |sed 's/([0-9]*).*/1/'
will give me the line N
sed -e '1,Nd' file.txt > file2.txt
will remove the first N lines from file.txt and place them in file2.txt
am I able to use '1,N-1d'?
Further, how can I get all of the above into one line, as I am executing this from ANSYS and it doesn't carry parameters from line to line
any help would be greatly appreciated
Thank you!
grep sed
add a comment |
I'm trying to remove the first N-1 lines up to the first match of a specified string
I am very new to scripting, but from my googling I believe I have the two commands I need, but don't know how to use them together:
grep -n -m 1 'STRING' file.txt |sed 's/([0-9]*).*/1/'
will give me the line N
sed -e '1,Nd' file.txt > file2.txt
will remove the first N lines from file.txt and place them in file2.txt
am I able to use '1,N-1d'?
Further, how can I get all of the above into one line, as I am executing this from ANSYS and it doesn't carry parameters from line to line
any help would be greatly appreciated
Thank you!
grep sed
add a comment |
I'm trying to remove the first N-1 lines up to the first match of a specified string
I am very new to scripting, but from my googling I believe I have the two commands I need, but don't know how to use them together:
grep -n -m 1 'STRING' file.txt |sed 's/([0-9]*).*/1/'
will give me the line N
sed -e '1,Nd' file.txt > file2.txt
will remove the first N lines from file.txt and place them in file2.txt
am I able to use '1,N-1d'?
Further, how can I get all of the above into one line, as I am executing this from ANSYS and it doesn't carry parameters from line to line
any help would be greatly appreciated
Thank you!
grep sed
I'm trying to remove the first N-1 lines up to the first match of a specified string
I am very new to scripting, but from my googling I believe I have the two commands I need, but don't know how to use them together:
grep -n -m 1 'STRING' file.txt |sed 's/([0-9]*).*/1/'
will give me the line N
sed -e '1,Nd' file.txt > file2.txt
will remove the first N lines from file.txt and place them in file2.txt
am I able to use '1,N-1d'?
Further, how can I get all of the above into one line, as I am executing this from ANSYS and it doesn't carry parameters from line to line
any help would be greatly appreciated
Thank you!
grep sed
grep sed
edited Jan 3 at 15:50
George Udosen
20.6k94467
20.6k94467
asked Jan 3 at 15:46
tricha122tricha122
111
111
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Sure you can use grep
and sed
and bash arithmetic expansion, e.g.:
N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
sed "1,$(( N - 1 ))d" file1.txt > file2.txt
You can get the same effect with sed
, e.g.:
sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt
add a comment |
You could use awk
(both the default mawk
and the GNU gawk
version work for this) to filter your file with a single command:
awk '/STRING/{y=1} y' file1.txt > file2.txt
Here, /STRING/{y=1}
scans each input line for the regular expression pattern STRING
and if it matches, set a variable y
to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.
Then, also for each line, after performing the first check for that line, it evaluates the expression y
. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk
performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.
That way, the variable gets set when our STRING
pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f1106621%2fhow-to-remove-first-n-1-lines-up-to-a-given-input-string%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
Sure you can use grep
and sed
and bash arithmetic expansion, e.g.:
N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
sed "1,$(( N - 1 ))d" file1.txt > file2.txt
You can get the same effect with sed
, e.g.:
sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt
add a comment |
Sure you can use grep
and sed
and bash arithmetic expansion, e.g.:
N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
sed "1,$(( N - 1 ))d" file1.txt > file2.txt
You can get the same effect with sed
, e.g.:
sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt
add a comment |
Sure you can use grep
and sed
and bash arithmetic expansion, e.g.:
N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
sed "1,$(( N - 1 ))d" file1.txt > file2.txt
You can get the same effect with sed
, e.g.:
sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt
Sure you can use grep
and sed
and bash arithmetic expansion, e.g.:
N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
sed "1,$(( N - 1 ))d" file1.txt > file2.txt
You can get the same effect with sed
, e.g.:
sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt
answered Jan 3 at 16:14
ThorThor
2,5231620
2,5231620
add a comment |
add a comment |
You could use awk
(both the default mawk
and the GNU gawk
version work for this) to filter your file with a single command:
awk '/STRING/{y=1} y' file1.txt > file2.txt
Here, /STRING/{y=1}
scans each input line for the regular expression pattern STRING
and if it matches, set a variable y
to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.
Then, also for each line, after performing the first check for that line, it evaluates the expression y
. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk
performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.
That way, the variable gets set when our STRING
pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.
add a comment |
You could use awk
(both the default mawk
and the GNU gawk
version work for this) to filter your file with a single command:
awk '/STRING/{y=1} y' file1.txt > file2.txt
Here, /STRING/{y=1}
scans each input line for the regular expression pattern STRING
and if it matches, set a variable y
to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.
Then, also for each line, after performing the first check for that line, it evaluates the expression y
. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk
performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.
That way, the variable gets set when our STRING
pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.
add a comment |
You could use awk
(both the default mawk
and the GNU gawk
version work for this) to filter your file with a single command:
awk '/STRING/{y=1} y' file1.txt > file2.txt
Here, /STRING/{y=1}
scans each input line for the regular expression pattern STRING
and if it matches, set a variable y
to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.
Then, also for each line, after performing the first check for that line, it evaluates the expression y
. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk
performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.
That way, the variable gets set when our STRING
pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.
You could use awk
(both the default mawk
and the GNU gawk
version work for this) to filter your file with a single command:
awk '/STRING/{y=1} y' file1.txt > file2.txt
Here, /STRING/{y=1}
scans each input line for the regular expression pattern STRING
and if it matches, set a variable y
to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.
Then, also for each line, after performing the first check for that line, it evaluates the expression y
. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk
performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.
That way, the variable gets set when our STRING
pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.
edited Jan 3 at 19:14
answered Jan 3 at 16:37
Byte CommanderByte Commander
63.7k26173292
63.7k26173292
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1106621%2fhow-to-remove-first-n-1-lines-up-to-a-given-input-string%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