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
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
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
8 hours ago
@Sparhawk Yes, it is.
– TheAsker
8 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
8 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
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
New contributor
New contributor
asked 8 hours ago
TheAskerTheAsker
132
132
New contributor
New contributor
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
8 hours ago
@Sparhawk Yes, it is.
– TheAsker
8 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
8 hours ago
add a comment |
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
8 hours ago
@Sparhawk Yes, it is.
– TheAsker
8 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
8 hours ago
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
8 hours ago
Is the line with "MATCH" always line two, hence to be swapped with the line before it?
– Sparhawk
8 hours ago
@Sparhawk Yes, it is.
– TheAsker
8 hours ago
@Sparhawk Yes, it is.
– TheAsker
8 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
8 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
8 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 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. The effect is that the MATCH
lines swap places with the immediately preceding lines. We use -2
since the m
command moves the line to after the targeted line.
The final ,p
in the editing script just displays the modified editing buffer.
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
The lines withMATCH
should swap places with the preceding lines.
– Kusalananda
5 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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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 7 hours ago
ilkkachuilkkachu
59k892166
59k892166
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 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. The effect is that the MATCH
lines swap places with the immediately preceding lines. We use -2
since the m
command moves the line to after the targeted line.
The final ,p
in the editing script just displays the modified editing buffer.
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 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. The effect is that the MATCH
lines swap places with the immediately preceding lines. We use -2
since the m
command moves the line to after the targeted line.
The final ,p
in the editing script just displays the modified editing buffer.
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 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. The effect is that the MATCH
lines swap places with the immediately preceding lines. We use -2
since the m
command moves the line to after the targeted line.
The final ,p
in the editing script just displays the modified editing buffer.
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 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. The effect is that the MATCH
lines swap places with the immediately preceding lines. We use -2
since the m
command moves the line to after the targeted line.
The final ,p
in the editing script just displays the modified editing buffer.
edited 5 hours ago
answered 6 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 7 hours ago
don_crisstidon_crissti
50.9k15135163
50.9k15135163
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 1 hour ago
Rakesh SharmaRakesh Sharma
302113
302113
add a comment |
add a comment |
sed "s/line 2 match//g"|sed "s/line 1/line 2 matchn&/g"
Above command worked fine
The lines withMATCH
should swap places with the preceding lines.
– Kusalananda
5 hours ago
add a comment |
sed "s/line 2 match//g"|sed "s/line 1/line 2 matchn&/g"
Above command worked fine
The lines withMATCH
should swap places with the preceding lines.
– Kusalananda
5 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 6 hours ago
Praveen Kumar BSPraveen Kumar BS
1,472138
1,472138
The lines withMATCH
should swap places with the preceding lines.
– Kusalananda
5 hours ago
add a comment |
The lines withMATCH
should swap places with the preceding lines.
– Kusalananda
5 hours ago
The lines with
MATCH
should swap places with the preceding lines.– Kusalananda
5 hours ago
The lines with
MATCH
should swap places with the preceding lines.– Kusalananda
5 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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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
8 hours ago
@Sparhawk Yes, it is.
– TheAsker
8 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
8 hours ago