Swap a line with another
I have the file
Line 1
Line 2 MATCH
Line 3
Line 4
Line 1
Line 2 MATCH
Line 3
Line 4
And I want to swap the line with "MATCH" and "Line 1" for every case. I tried to search in other questions, but those move the line with the match to the last line and I don't understand so well the code to remake my version for a final output like:
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
text-processing sed
New contributor
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I have the file
Line 1
Line 2 MATCH
Line 3
Line 4
Line 1
Line 2 MATCH
Line 3
Line 4
And I want to swap the line with "MATCH" and "Line 1" for every case. I tried to search in other questions, but those move the line with the match to the last line and I don't understand so well the code to remake my version for a final output like:
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
text-processing sed
New contributor
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
15 hours ago
@Sparhawk Yes, it is.
– TheAsker
15 hours ago
@don_crissti but in the answer provided there, they only swap the line N with the line below. For swap line N with the line above, they swap the Line N-1 with the line below. There is some way to swap with the line above based on a pattern?
– TheAsker
15 hours ago
add a comment |
I have the file
Line 1
Line 2 MATCH
Line 3
Line 4
Line 1
Line 2 MATCH
Line 3
Line 4
And I want to swap the line with "MATCH" and "Line 1" for every case. I tried to search in other questions, but those move the line with the match to the last line and I don't understand so well the code to remake my version for a final output like:
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
text-processing sed
New contributor
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have the file
Line 1
Line 2 MATCH
Line 3
Line 4
Line 1
Line 2 MATCH
Line 3
Line 4
And I want to swap the line with "MATCH" and "Line 1" for every case. I tried to search in other questions, but those move the line with the match to the last line and I don't understand so well the code to remake my version for a final output like:
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
text-processing sed
text-processing sed
New contributor
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 15 hours ago
TheAskerTheAsker
182
182
New contributor
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
TheAsker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
15 hours ago
@Sparhawk Yes, it is.
– TheAsker
15 hours ago
@don_crissti but in the answer provided there, they only swap the line N with the line below. For swap line N with the line above, they swap the Line N-1 with the line below. There is some way to swap with the line above based on a pattern?
– TheAsker
15 hours ago
add a comment |
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
15 hours ago
@Sparhawk Yes, it is.
– TheAsker
15 hours ago
@don_crissti but in the answer provided there, they only swap the line N with the line below. For swap line N with the line above, they swap the Line N-1 with the line below. There is some way to swap with the line above based on a pattern?
– TheAsker
15 hours ago
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
15 hours ago
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
15 hours ago
@Sparhawk Yes, it is.
– TheAsker
15 hours ago
@Sparhawk Yes, it is.
– TheAsker
15 hours ago
@don_crissti but in the answer provided there, they only swap the line N with the line below. For swap line N with the line above, they swap the Line N-1 with the line below. There is some way to swap with the line above based on a pattern?
– TheAsker
15 hours ago
@don_crissti but in the answer provided there, they only swap the line N with the line below. For swap line N with the line above, they swap the Line N-1 with the line below. There is some way to swap with the line above based on a pattern?
– TheAsker
15 hours ago
add a comment |
5 Answers
5
active
oldest
votes
If the idea is to swap the MATCH line with the immediately preceding one, then something like this would do:
$ awk '!/MATCH/ { if (NR > 1) print prev; prev=$0}
/MATCH/ {print $0;}
END {print prev}' < file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The script holds the previous line in prev, printing and updating it on the non-matching lines. On lines matching the pattern, it prints the current line, leaving the previous in the variable to be printed next.
Special cases for the first line (NR==1) when there's no previous line to print, and for the END when we print the held line.
add a comment |
Using ed:
$ printf 'g/MATCH/m-2n,pn' | ed -s file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The m command moves the current line after to the subsequent target address. Here, we find all lines matching MATCH (it's the g in front of the regular expression that makes this a "global" operation), and for each line move it one line up (to "after the line two lines up"). The effect is that the MATCH lines swap places with the immediately preceding lines.
The final ,p in the editing script just displays the modified editing buffer. This could be changed to something like wq to write the changed editing buffer back to the original file.
Note that using ed for editing files might look neat, but is not recommended for large files as the whole file is read into memory.
add a comment |
Using sed with a N;P;D cycle:
sed -e '$!N;s/(Line 1)(n)(.*MATCH.*)/321/;t' -e 'P;D' infile
This will swap only if the line with MATCH is preceded by Line 1: the t without label branches to the end of script if successful and so it avoids another swap if any Line 1 is followed by consecutive lines with MATCH. Adjust the regex for any leading/trailing blanks.
add a comment |
Using sed editor, we can swap two lines one of which contains Match keyword with the one preceding it.
$ sed -e '
/MATCH/!{
x;1!p;$!d;g;q
}
$G
' input.txt
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
add a comment |
sed "s/line 2 match//g"|sed "s/line 1/line 2 matchn&/g"
Above command worked fine
1
The lines withMATCHshould swap places with the preceding lines.
– Kusalananda
13 hours ago
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
});
}
});
TheAsker is a new contributor. Be nice, and check out our Code of Conduct.
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%2f501234%2fswap-a-line-with-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
If the idea is to swap the MATCH line with the immediately preceding one, then something like this would do:
$ awk '!/MATCH/ { if (NR > 1) print prev; prev=$0}
/MATCH/ {print $0;}
END {print prev}' < file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The script holds the previous line in prev, printing and updating it on the non-matching lines. On lines matching the pattern, it prints the current line, leaving the previous in the variable to be printed next.
Special cases for the first line (NR==1) when there's no previous line to print, and for the END when we print the held line.
add a comment |
If the idea is to swap the MATCH line with the immediately preceding one, then something like this would do:
$ awk '!/MATCH/ { if (NR > 1) print prev; prev=$0}
/MATCH/ {print $0;}
END {print prev}' < file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The script holds the previous line in prev, printing and updating it on the non-matching lines. On lines matching the pattern, it prints the current line, leaving the previous in the variable to be printed next.
Special cases for the first line (NR==1) when there's no previous line to print, and for the END when we print the held line.
add a comment |
If the idea is to swap the MATCH line with the immediately preceding one, then something like this would do:
$ awk '!/MATCH/ { if (NR > 1) print prev; prev=$0}
/MATCH/ {print $0;}
END {print prev}' < file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The script holds the previous line in prev, printing and updating it on the non-matching lines. On lines matching the pattern, it prints the current line, leaving the previous in the variable to be printed next.
Special cases for the first line (NR==1) when there's no previous line to print, and for the END when we print the held line.
If the idea is to swap the MATCH line with the immediately preceding one, then something like this would do:
$ awk '!/MATCH/ { if (NR > 1) print prev; prev=$0}
/MATCH/ {print $0;}
END {print prev}' < file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The script holds the previous line in prev, printing and updating it on the non-matching lines. On lines matching the pattern, it prints the current line, leaving the previous in the variable to be printed next.
Special cases for the first line (NR==1) when there's no previous line to print, and for the END when we print the held line.
answered 15 hours ago
ilkkachuilkkachu
59.1k892167
59.1k892167
add a comment |
add a comment |
Using ed:
$ printf 'g/MATCH/m-2n,pn' | ed -s file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The m command moves the current line after to the subsequent target address. Here, we find all lines matching MATCH (it's the g in front of the regular expression that makes this a "global" operation), and for each line move it one line up (to "after the line two lines up"). The effect is that the MATCH lines swap places with the immediately preceding lines.
The final ,p in the editing script just displays the modified editing buffer. This could be changed to something like wq to write the changed editing buffer back to the original file.
Note that using ed for editing files might look neat, but is not recommended for large files as the whole file is read into memory.
add a comment |
Using ed:
$ printf 'g/MATCH/m-2n,pn' | ed -s file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The m command moves the current line after to the subsequent target address. Here, we find all lines matching MATCH (it's the g in front of the regular expression that makes this a "global" operation), and for each line move it one line up (to "after the line two lines up"). The effect is that the MATCH lines swap places with the immediately preceding lines.
The final ,p in the editing script just displays the modified editing buffer. This could be changed to something like wq to write the changed editing buffer back to the original file.
Note that using ed for editing files might look neat, but is not recommended for large files as the whole file is read into memory.
add a comment |
Using ed:
$ printf 'g/MATCH/m-2n,pn' | ed -s file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The m command moves the current line after to the subsequent target address. Here, we find all lines matching MATCH (it's the g in front of the regular expression that makes this a "global" operation), and for each line move it one line up (to "after the line two lines up"). The effect is that the MATCH lines swap places with the immediately preceding lines.
The final ,p in the editing script just displays the modified editing buffer. This could be changed to something like wq to write the changed editing buffer back to the original file.
Note that using ed for editing files might look neat, but is not recommended for large files as the whole file is read into memory.
Using ed:
$ printf 'g/MATCH/m-2n,pn' | ed -s file
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
The m command moves the current line after to the subsequent target address. Here, we find all lines matching MATCH (it's the g in front of the regular expression that makes this a "global" operation), and for each line move it one line up (to "after the line two lines up"). The effect is that the MATCH lines swap places with the immediately preceding lines.
The final ,p in the editing script just displays the modified editing buffer. This could be changed to something like wq to write the changed editing buffer back to the original file.
Note that using ed for editing files might look neat, but is not recommended for large files as the whole file is read into memory.
edited 5 hours ago
answered 13 hours ago
KusalanandaKusalananda
130k17247407
130k17247407
add a comment |
add a comment |
Using sed with a N;P;D cycle:
sed -e '$!N;s/(Line 1)(n)(.*MATCH.*)/321/;t' -e 'P;D' infile
This will swap only if the line with MATCH is preceded by Line 1: the t without label branches to the end of script if successful and so it avoids another swap if any Line 1 is followed by consecutive lines with MATCH. Adjust the regex for any leading/trailing blanks.
add a comment |
Using sed with a N;P;D cycle:
sed -e '$!N;s/(Line 1)(n)(.*MATCH.*)/321/;t' -e 'P;D' infile
This will swap only if the line with MATCH is preceded by Line 1: the t without label branches to the end of script if successful and so it avoids another swap if any Line 1 is followed by consecutive lines with MATCH. Adjust the regex for any leading/trailing blanks.
add a comment |
Using sed with a N;P;D cycle:
sed -e '$!N;s/(Line 1)(n)(.*MATCH.*)/321/;t' -e 'P;D' infile
This will swap only if the line with MATCH is preceded by Line 1: the t without label branches to the end of script if successful and so it avoids another swap if any Line 1 is followed by consecutive lines with MATCH. Adjust the regex for any leading/trailing blanks.
Using sed with a N;P;D cycle:
sed -e '$!N;s/(Line 1)(n)(.*MATCH.*)/321/;t' -e 'P;D' infile
This will swap only if the line with MATCH is preceded by Line 1: the t without label branches to the end of script if successful and so it avoids another swap if any Line 1 is followed by consecutive lines with MATCH. Adjust the regex for any leading/trailing blanks.
answered 14 hours ago
don_crisstidon_crissti
51k15135163
51k15135163
add a comment |
add a comment |
Using sed editor, we can swap two lines one of which contains Match keyword with the one preceding it.
$ sed -e '
/MATCH/!{
x;1!p;$!d;g;q
}
$G
' input.txt
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
add a comment |
Using sed editor, we can swap two lines one of which contains Match keyword with the one preceding it.
$ sed -e '
/MATCH/!{
x;1!p;$!d;g;q
}
$G
' input.txt
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
add a comment |
Using sed editor, we can swap two lines one of which contains Match keyword with the one preceding it.
$ sed -e '
/MATCH/!{
x;1!p;$!d;g;q
}
$G
' input.txt
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
Using sed editor, we can swap two lines one of which contains Match keyword with the one preceding it.
$ sed -e '
/MATCH/!{
x;1!p;$!d;g;q
}
$G
' input.txt
Line 2 MATCH
Line 1
Line 3
Line 4
Line 2 MATCH
Line 1
Line 3
Line 4
answered 8 hours ago
Rakesh SharmaRakesh Sharma
312113
312113
add a comment |
add a comment |
sed "s/line 2 match//g"|sed "s/line 1/line 2 matchn&/g"
Above command worked fine
1
The lines withMATCHshould swap places with the preceding lines.
– Kusalananda
13 hours ago
add a comment |
sed "s/line 2 match//g"|sed "s/line 1/line 2 matchn&/g"
Above command worked fine
1
The lines withMATCHshould swap places with the preceding lines.
– Kusalananda
13 hours ago
add a comment |
sed "s/line 2 match//g"|sed "s/line 1/line 2 matchn&/g"
Above command worked fine
sed "s/line 2 match//g"|sed "s/line 1/line 2 matchn&/g"
Above command worked fine
answered 13 hours ago
Praveen Kumar BSPraveen Kumar BS
1,472138
1,472138
1
The lines withMATCHshould swap places with the preceding lines.
– Kusalananda
13 hours ago
add a comment |
1
The lines withMATCHshould swap places with the preceding lines.
– Kusalananda
13 hours ago
1
1
The lines with
MATCH should swap places with the preceding lines.– Kusalananda
13 hours ago
The lines with
MATCH should swap places with the preceding lines.– Kusalananda
13 hours ago
add a comment |
TheAsker is a new contributor. Be nice, and check out our Code of Conduct.
TheAsker is a new contributor. Be nice, and check out our Code of Conduct.
TheAsker is a new contributor. Be nice, and check out our Code of Conduct.
TheAsker is a new contributor. Be nice, and check out our Code of Conduct.
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%2f501234%2fswap-a-line-with-another%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
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
15 hours ago
@Sparhawk Yes, it is.
– TheAsker
15 hours ago
@don_crissti but in the answer provided there, they only swap the line N with the line below. For swap line N with the line above, they swap the Line N-1 with the line below. There is some way to swap with the line above based on a pattern?
– TheAsker
15 hours ago