how can i move lines in mulitple text files?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Hi assume I have a files that look like:
x
x
x
A
B
C
1
2
3
D
E
F
x
x
x
and what need the files to look like:
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
I know I could do this with a few sed -i s/search/replace/g
lines but am wondering if there's a better, easier way to move x
number of rows by y
numbers in either direction?
text-processing
add a comment |Â
up vote
0
down vote
favorite
Hi assume I have a files that look like:
x
x
x
A
B
C
1
2
3
D
E
F
x
x
x
and what need the files to look like:
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
I know I could do this with a few sed -i s/search/replace/g
lines but am wondering if there's a better, easier way to move x
number of rows by y
numbers in either direction?
text-processing
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hi assume I have a files that look like:
x
x
x
A
B
C
1
2
3
D
E
F
x
x
x
and what need the files to look like:
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
I know I could do this with a few sed -i s/search/replace/g
lines but am wondering if there's a better, easier way to move x
number of rows by y
numbers in either direction?
text-processing
Hi assume I have a files that look like:
x
x
x
A
B
C
1
2
3
D
E
F
x
x
x
and what need the files to look like:
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
I know I could do this with a few sed -i s/search/replace/g
lines but am wondering if there's a better, easier way to move x
number of rows by y
numbers in either direction?
text-processing
text-processing
edited 37 mins ago
Kusalananda
110k14213337
110k14213337
asked 1 hour ago
cerr
66382239
66382239
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
ed
echo -e '7,9m12n,p' | ed -s file.txt
1
this solution works only under bash, it doesn't work under other environments
â TNT
28 mins ago
add a comment |Â
up vote
6
down vote
Using sed
(moving rows down, because that's easiest with stream editors):
$ sed '7,9H;d;; 12G;s/n//;' <file
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
You may read the sed
script as "move lines 7 through to 9 to after line 12".
The script annotated:
7,9 # These are the lines we'd like to move
H; # Append them to the hold space (separated by newlines)
d; # Delete from pattern space, start next cycle
12 # This is where we'd like to move those lines
G; # Append the hold space
s/n//; # Remove the newline that the above command inserted
# (implicit print)
Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):
sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file
I.e., "move the lines from the one starting with 1
through to the one starting with 3
to after the line that starts with F
".
A direct translation of the above into awk
:
awk '/^1/,/^3/ hold = hold ORS $0; next
/^F/ $0 = $0 hold
print ' <file
Using "line numbers" (record numbers in awk
):
awk 'NR == 7, NR == 9 hold = hold ORS $0; next
NR == 12 $0 = $0 hold
print ' <file
The condition NR == 7, NR == 9
could also be written NR >= 7 && NR <= 9
.
To be 100% faithful to the sed
code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0)
but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.
1
robust solution thumb up!
â TNT
28 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
ed
echo -e '7,9m12n,p' | ed -s file.txt
1
this solution works only under bash, it doesn't work under other environments
â TNT
28 mins ago
add a comment |Â
up vote
0
down vote
accepted
ed
echo -e '7,9m12n,p' | ed -s file.txt
1
this solution works only under bash, it doesn't work under other environments
â TNT
28 mins ago
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
ed
echo -e '7,9m12n,p' | ed -s file.txt
ed
echo -e '7,9m12n,p' | ed -s file.txt
answered 51 mins ago
ColinSeligSmith
24415
24415
1
this solution works only under bash, it doesn't work under other environments
â TNT
28 mins ago
add a comment |Â
1
this solution works only under bash, it doesn't work under other environments
â TNT
28 mins ago
1
1
this solution works only under bash, it doesn't work under other environments
â TNT
28 mins ago
this solution works only under bash, it doesn't work under other environments
â TNT
28 mins ago
add a comment |Â
up vote
6
down vote
Using sed
(moving rows down, because that's easiest with stream editors):
$ sed '7,9H;d;; 12G;s/n//;' <file
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
You may read the sed
script as "move lines 7 through to 9 to after line 12".
The script annotated:
7,9 # These are the lines we'd like to move
H; # Append them to the hold space (separated by newlines)
d; # Delete from pattern space, start next cycle
12 # This is where we'd like to move those lines
G; # Append the hold space
s/n//; # Remove the newline that the above command inserted
# (implicit print)
Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):
sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file
I.e., "move the lines from the one starting with 1
through to the one starting with 3
to after the line that starts with F
".
A direct translation of the above into awk
:
awk '/^1/,/^3/ hold = hold ORS $0; next
/^F/ $0 = $0 hold
print ' <file
Using "line numbers" (record numbers in awk
):
awk 'NR == 7, NR == 9 hold = hold ORS $0; next
NR == 12 $0 = $0 hold
print ' <file
The condition NR == 7, NR == 9
could also be written NR >= 7 && NR <= 9
.
To be 100% faithful to the sed
code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0)
but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.
1
robust solution thumb up!
â TNT
28 mins ago
add a comment |Â
up vote
6
down vote
Using sed
(moving rows down, because that's easiest with stream editors):
$ sed '7,9H;d;; 12G;s/n//;' <file
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
You may read the sed
script as "move lines 7 through to 9 to after line 12".
The script annotated:
7,9 # These are the lines we'd like to move
H; # Append them to the hold space (separated by newlines)
d; # Delete from pattern space, start next cycle
12 # This is where we'd like to move those lines
G; # Append the hold space
s/n//; # Remove the newline that the above command inserted
# (implicit print)
Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):
sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file
I.e., "move the lines from the one starting with 1
through to the one starting with 3
to after the line that starts with F
".
A direct translation of the above into awk
:
awk '/^1/,/^3/ hold = hold ORS $0; next
/^F/ $0 = $0 hold
print ' <file
Using "line numbers" (record numbers in awk
):
awk 'NR == 7, NR == 9 hold = hold ORS $0; next
NR == 12 $0 = $0 hold
print ' <file
The condition NR == 7, NR == 9
could also be written NR >= 7 && NR <= 9
.
To be 100% faithful to the sed
code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0)
but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.
1
robust solution thumb up!
â TNT
28 mins ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Using sed
(moving rows down, because that's easiest with stream editors):
$ sed '7,9H;d;; 12G;s/n//;' <file
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
You may read the sed
script as "move lines 7 through to 9 to after line 12".
The script annotated:
7,9 # These are the lines we'd like to move
H; # Append them to the hold space (separated by newlines)
d; # Delete from pattern space, start next cycle
12 # This is where we'd like to move those lines
G; # Append the hold space
s/n//; # Remove the newline that the above command inserted
# (implicit print)
Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):
sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file
I.e., "move the lines from the one starting with 1
through to the one starting with 3
to after the line that starts with F
".
A direct translation of the above into awk
:
awk '/^1/,/^3/ hold = hold ORS $0; next
/^F/ $0 = $0 hold
print ' <file
Using "line numbers" (record numbers in awk
):
awk 'NR == 7, NR == 9 hold = hold ORS $0; next
NR == 12 $0 = $0 hold
print ' <file
The condition NR == 7, NR == 9
could also be written NR >= 7 && NR <= 9
.
To be 100% faithful to the sed
code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0)
but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.
Using sed
(moving rows down, because that's easiest with stream editors):
$ sed '7,9H;d;; 12G;s/n//;' <file
x
x
x
A
B
C
D
E
F
1
2
3
x
x
x
You may read the sed
script as "move lines 7 through to 9 to after line 12".
The script annotated:
7,9 # These are the lines we'd like to move
H; # Append them to the hold space (separated by newlines)
d; # Delete from pattern space, start next cycle
12 # This is where we'd like to move those lines
G; # Append the hold space
s/n//; # Remove the newline that the above command inserted
# (implicit print)
Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):
sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file
I.e., "move the lines from the one starting with 1
through to the one starting with 3
to after the line that starts with F
".
A direct translation of the above into awk
:
awk '/^1/,/^3/ hold = hold ORS $0; next
/^F/ $0 = $0 hold
print ' <file
Using "line numbers" (record numbers in awk
):
awk 'NR == 7, NR == 9 hold = hold ORS $0; next
NR == 12 $0 = $0 hold
print ' <file
The condition NR == 7, NR == 9
could also be written NR >= 7 && NR <= 9
.
To be 100% faithful to the sed
code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0)
but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.
edited 16 mins ago
answered 39 mins ago
Kusalananda
110k14213337
110k14213337
1
robust solution thumb up!
â TNT
28 mins ago
add a comment |Â
1
robust solution thumb up!
â TNT
28 mins ago
1
1
robust solution thumb up!
â TNT
28 mins ago
robust solution thumb up!
â TNT
28 mins ago
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475682%2fhow-can-i-move-lines-in-mulitple-text-files%23new-answer', 'question_page');
);
Post as a guest
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
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
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