How do i swap the first word of a line with the last word in a line
up vote
0
down vote
favorite
I was trying to replace the first word in a line with the last word in a line using a sed command i=but i can't seem to figure it out.
linux text-processing sed
New contributor
Navpreet Singh 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 |
up vote
0
down vote
favorite
I was trying to replace the first word in a line with the last word in a line using a sed command i=but i can't seem to figure it out.
linux text-processing sed
New contributor
Navpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
In every line, or a specific line. If so which line?
– Jesse_b
Nov 28 at 21:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was trying to replace the first word in a line with the last word in a line using a sed command i=but i can't seem to figure it out.
linux text-processing sed
New contributor
Navpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I was trying to replace the first word in a line with the last word in a line using a sed command i=but i can't seem to figure it out.
linux text-processing sed
linux text-processing sed
New contributor
Navpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Navpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 28 at 22:12
Jeff Schaller
37k1052121
37k1052121
New contributor
Navpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 28 at 21:28
Navpreet Singh
1
1
New contributor
Navpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Navpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Navpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
In every line, or a specific line. If so which line?
– Jesse_b
Nov 28 at 21:29
add a comment |
In every line, or a specific line. If so which line?
– Jesse_b
Nov 28 at 21:29
In every line, or a specific line. If so which line?
– Jesse_b
Nov 28 at 21:29
In every line, or a specific line. If so which line?
– Jesse_b
Nov 28 at 21:29
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
Using awk:
awk '{ t=$1; $1=$NF; $NF=t; print}'
This will:
t=$1- settto the first word
$1=$NF- set the first word to the last word
$NF=t- set the last word to the first word
print- print the new line.
$ echo 'one two three four five six' | awk '{ f=$1; l=$NF; $1=l; $NF=f; print}'
six two three four five one
@Kusalananda: Thanks. Didn't think of it.
– Jesse_b
Nov 28 at 21:41
1
Slightly shorter :awk '{ t=$1; $1=$NF; $NF=t}1'
– steve
Nov 28 at 22:37
add a comment |
up vote
4
down vote
Using Perl, and assuming whitespace delimited input and space-delimited output:
perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Testure:
$ printf 'Cleanse Fold and Manipulaten' | perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Manipulate Fold and Cleanse
The Perl code, using -a to split the input on whitespace into the array @F, simply swaps the two elements at the start and end of that array before joining the resulting list with spaces, adding a newline at the end.
A shorter Perl variant that matches the first and last words and swaps them in a substitution (this assumes that there are no flanking whitespace in the input though):
perl -pe 's/^(w*)(.*?)(w*)$/$3$2$1/'
The middle bit, .*?, matches the middle of the string non-greedily. We couldn't have done this this easily with sed as there is no non-greedy modifier like that ? after .*.
1
Or, allowing for leading trailing whitespace:perl -pe 's/^(s*)(S+)(.+?)(S+)(s*)$/$1$4$3$2$5/'-- note the middle bit has at least 1 char to account for separate first/last words.
– glenn jackman
Nov 28 at 22:24
add a comment |
up vote
1
down vote
Semi-serious answer: it's not you, you are fine. The problem is totally in sed's s/// command's verbosity (compare this with the alternative answers):
$ echo "Hello some good world!" |
sed 's/(^[^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+$)/321/'
world! some good Hello
We may also want to swap the first and the last words even if we have space characters before the first and/or after the latter (thanks to comments and other answers):
$ echo " Hello some good world! " |
sed 's/^([[:space:]]*)([^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+)([[:space:]]*)$/14325/'
world! some good Hello
However, these commands use some non-POSIX GNU extensions to the BRE - Basic Regular Expression - syntax (namely, + and |).
A (more portable) command that satisfies the POSIX standard while keeping the convenience of alternation (|) would require Extended regular expressions. For example, using GNU sed with the --posix option (which disables GNU extensions):
$ echo " Hello some good world! " |
sed --posix -E 's/^([[:space:]]*)([^[:space:]]{1,})([[:space:]].*[[:space:]]|[[:space:]]{1,})([^[:space:]]{1,})([[:space:]]*)$/14325/'
world! some good Hello
3
Will not destroy the original whitespace. +1
– glenn jackman
Nov 28 at 22:22
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Using awk:
awk '{ t=$1; $1=$NF; $NF=t; print}'
This will:
t=$1- settto the first word
$1=$NF- set the first word to the last word
$NF=t- set the last word to the first word
print- print the new line.
$ echo 'one two three four five six' | awk '{ f=$1; l=$NF; $1=l; $NF=f; print}'
six two three four five one
@Kusalananda: Thanks. Didn't think of it.
– Jesse_b
Nov 28 at 21:41
1
Slightly shorter :awk '{ t=$1; $1=$NF; $NF=t}1'
– steve
Nov 28 at 22:37
add a comment |
up vote
4
down vote
Using awk:
awk '{ t=$1; $1=$NF; $NF=t; print}'
This will:
t=$1- settto the first word
$1=$NF- set the first word to the last word
$NF=t- set the last word to the first word
print- print the new line.
$ echo 'one two three four five six' | awk '{ f=$1; l=$NF; $1=l; $NF=f; print}'
six two three four five one
@Kusalananda: Thanks. Didn't think of it.
– Jesse_b
Nov 28 at 21:41
1
Slightly shorter :awk '{ t=$1; $1=$NF; $NF=t}1'
– steve
Nov 28 at 22:37
add a comment |
up vote
4
down vote
up vote
4
down vote
Using awk:
awk '{ t=$1; $1=$NF; $NF=t; print}'
This will:
t=$1- settto the first word
$1=$NF- set the first word to the last word
$NF=t- set the last word to the first word
print- print the new line.
$ echo 'one two three four five six' | awk '{ f=$1; l=$NF; $1=l; $NF=f; print}'
six two three four five one
Using awk:
awk '{ t=$1; $1=$NF; $NF=t; print}'
This will:
t=$1- settto the first word
$1=$NF- set the first word to the last word
$NF=t- set the last word to the first word
print- print the new line.
$ echo 'one two three four five six' | awk '{ f=$1; l=$NF; $1=l; $NF=f; print}'
six two three four five one
edited Nov 28 at 21:58
answered Nov 28 at 21:37
Jesse_b
11.5k23063
11.5k23063
@Kusalananda: Thanks. Didn't think of it.
– Jesse_b
Nov 28 at 21:41
1
Slightly shorter :awk '{ t=$1; $1=$NF; $NF=t}1'
– steve
Nov 28 at 22:37
add a comment |
@Kusalananda: Thanks. Didn't think of it.
– Jesse_b
Nov 28 at 21:41
1
Slightly shorter :awk '{ t=$1; $1=$NF; $NF=t}1'
– steve
Nov 28 at 22:37
@Kusalananda: Thanks. Didn't think of it.
– Jesse_b
Nov 28 at 21:41
@Kusalananda: Thanks. Didn't think of it.
– Jesse_b
Nov 28 at 21:41
1
1
Slightly shorter :
awk '{ t=$1; $1=$NF; $NF=t}1'– steve
Nov 28 at 22:37
Slightly shorter :
awk '{ t=$1; $1=$NF; $NF=t}1'– steve
Nov 28 at 22:37
add a comment |
up vote
4
down vote
Using Perl, and assuming whitespace delimited input and space-delimited output:
perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Testure:
$ printf 'Cleanse Fold and Manipulaten' | perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Manipulate Fold and Cleanse
The Perl code, using -a to split the input on whitespace into the array @F, simply swaps the two elements at the start and end of that array before joining the resulting list with spaces, adding a newline at the end.
A shorter Perl variant that matches the first and last words and swaps them in a substitution (this assumes that there are no flanking whitespace in the input though):
perl -pe 's/^(w*)(.*?)(w*)$/$3$2$1/'
The middle bit, .*?, matches the middle of the string non-greedily. We couldn't have done this this easily with sed as there is no non-greedy modifier like that ? after .*.
1
Or, allowing for leading trailing whitespace:perl -pe 's/^(s*)(S+)(.+?)(S+)(s*)$/$1$4$3$2$5/'-- note the middle bit has at least 1 char to account for separate first/last words.
– glenn jackman
Nov 28 at 22:24
add a comment |
up vote
4
down vote
Using Perl, and assuming whitespace delimited input and space-delimited output:
perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Testure:
$ printf 'Cleanse Fold and Manipulaten' | perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Manipulate Fold and Cleanse
The Perl code, using -a to split the input on whitespace into the array @F, simply swaps the two elements at the start and end of that array before joining the resulting list with spaces, adding a newline at the end.
A shorter Perl variant that matches the first and last words and swaps them in a substitution (this assumes that there are no flanking whitespace in the input though):
perl -pe 's/^(w*)(.*?)(w*)$/$3$2$1/'
The middle bit, .*?, matches the middle of the string non-greedily. We couldn't have done this this easily with sed as there is no non-greedy modifier like that ? after .*.
1
Or, allowing for leading trailing whitespace:perl -pe 's/^(s*)(S+)(.+?)(S+)(s*)$/$1$4$3$2$5/'-- note the middle bit has at least 1 char to account for separate first/last words.
– glenn jackman
Nov 28 at 22:24
add a comment |
up vote
4
down vote
up vote
4
down vote
Using Perl, and assuming whitespace delimited input and space-delimited output:
perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Testure:
$ printf 'Cleanse Fold and Manipulaten' | perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Manipulate Fold and Cleanse
The Perl code, using -a to split the input on whitespace into the array @F, simply swaps the two elements at the start and end of that array before joining the resulting list with spaces, adding a newline at the end.
A shorter Perl variant that matches the first and last words and swaps them in a substitution (this assumes that there are no flanking whitespace in the input though):
perl -pe 's/^(w*)(.*?)(w*)$/$3$2$1/'
The middle bit, .*?, matches the middle of the string non-greedily. We couldn't have done this this easily with sed as there is no non-greedy modifier like that ? after .*.
Using Perl, and assuming whitespace delimited input and space-delimited output:
perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Testure:
$ printf 'Cleanse Fold and Manipulaten' | perl -ape '($F[0],$F[-1])=($F[-1],$F[0]);$_="@Fn"'
Manipulate Fold and Cleanse
The Perl code, using -a to split the input on whitespace into the array @F, simply swaps the two elements at the start and end of that array before joining the resulting list with spaces, adding a newline at the end.
A shorter Perl variant that matches the first and last words and swaps them in a substitution (this assumes that there are no flanking whitespace in the input though):
perl -pe 's/^(w*)(.*?)(w*)$/$3$2$1/'
The middle bit, .*?, matches the middle of the string non-greedily. We couldn't have done this this easily with sed as there is no non-greedy modifier like that ? after .*.
edited Nov 28 at 22:26
answered Nov 28 at 22:08
Kusalananda
118k16223361
118k16223361
1
Or, allowing for leading trailing whitespace:perl -pe 's/^(s*)(S+)(.+?)(S+)(s*)$/$1$4$3$2$5/'-- note the middle bit has at least 1 char to account for separate first/last words.
– glenn jackman
Nov 28 at 22:24
add a comment |
1
Or, allowing for leading trailing whitespace:perl -pe 's/^(s*)(S+)(.+?)(S+)(s*)$/$1$4$3$2$5/'-- note the middle bit has at least 1 char to account for separate first/last words.
– glenn jackman
Nov 28 at 22:24
1
1
Or, allowing for leading trailing whitespace:
perl -pe 's/^(s*)(S+)(.+?)(S+)(s*)$/$1$4$3$2$5/' -- note the middle bit has at least 1 char to account for separate first/last words.– glenn jackman
Nov 28 at 22:24
Or, allowing for leading trailing whitespace:
perl -pe 's/^(s*)(S+)(.+?)(S+)(s*)$/$1$4$3$2$5/' -- note the middle bit has at least 1 char to account for separate first/last words.– glenn jackman
Nov 28 at 22:24
add a comment |
up vote
1
down vote
Semi-serious answer: it's not you, you are fine. The problem is totally in sed's s/// command's verbosity (compare this with the alternative answers):
$ echo "Hello some good world!" |
sed 's/(^[^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+$)/321/'
world! some good Hello
We may also want to swap the first and the last words even if we have space characters before the first and/or after the latter (thanks to comments and other answers):
$ echo " Hello some good world! " |
sed 's/^([[:space:]]*)([^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+)([[:space:]]*)$/14325/'
world! some good Hello
However, these commands use some non-POSIX GNU extensions to the BRE - Basic Regular Expression - syntax (namely, + and |).
A (more portable) command that satisfies the POSIX standard while keeping the convenience of alternation (|) would require Extended regular expressions. For example, using GNU sed with the --posix option (which disables GNU extensions):
$ echo " Hello some good world! " |
sed --posix -E 's/^([[:space:]]*)([^[:space:]]{1,})([[:space:]].*[[:space:]]|[[:space:]]{1,})([^[:space:]]{1,})([[:space:]]*)$/14325/'
world! some good Hello
3
Will not destroy the original whitespace. +1
– glenn jackman
Nov 28 at 22:22
add a comment |
up vote
1
down vote
Semi-serious answer: it's not you, you are fine. The problem is totally in sed's s/// command's verbosity (compare this with the alternative answers):
$ echo "Hello some good world!" |
sed 's/(^[^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+$)/321/'
world! some good Hello
We may also want to swap the first and the last words even if we have space characters before the first and/or after the latter (thanks to comments and other answers):
$ echo " Hello some good world! " |
sed 's/^([[:space:]]*)([^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+)([[:space:]]*)$/14325/'
world! some good Hello
However, these commands use some non-POSIX GNU extensions to the BRE - Basic Regular Expression - syntax (namely, + and |).
A (more portable) command that satisfies the POSIX standard while keeping the convenience of alternation (|) would require Extended regular expressions. For example, using GNU sed with the --posix option (which disables GNU extensions):
$ echo " Hello some good world! " |
sed --posix -E 's/^([[:space:]]*)([^[:space:]]{1,})([[:space:]].*[[:space:]]|[[:space:]]{1,})([^[:space:]]{1,})([[:space:]]*)$/14325/'
world! some good Hello
3
Will not destroy the original whitespace. +1
– glenn jackman
Nov 28 at 22:22
add a comment |
up vote
1
down vote
up vote
1
down vote
Semi-serious answer: it's not you, you are fine. The problem is totally in sed's s/// command's verbosity (compare this with the alternative answers):
$ echo "Hello some good world!" |
sed 's/(^[^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+$)/321/'
world! some good Hello
We may also want to swap the first and the last words even if we have space characters before the first and/or after the latter (thanks to comments and other answers):
$ echo " Hello some good world! " |
sed 's/^([[:space:]]*)([^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+)([[:space:]]*)$/14325/'
world! some good Hello
However, these commands use some non-POSIX GNU extensions to the BRE - Basic Regular Expression - syntax (namely, + and |).
A (more portable) command that satisfies the POSIX standard while keeping the convenience of alternation (|) would require Extended regular expressions. For example, using GNU sed with the --posix option (which disables GNU extensions):
$ echo " Hello some good world! " |
sed --posix -E 's/^([[:space:]]*)([^[:space:]]{1,})([[:space:]].*[[:space:]]|[[:space:]]{1,})([^[:space:]]{1,})([[:space:]]*)$/14325/'
world! some good Hello
Semi-serious answer: it's not you, you are fine. The problem is totally in sed's s/// command's verbosity (compare this with the alternative answers):
$ echo "Hello some good world!" |
sed 's/(^[^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+$)/321/'
world! some good Hello
We may also want to swap the first and the last words even if we have space characters before the first and/or after the latter (thanks to comments and other answers):
$ echo " Hello some good world! " |
sed 's/^([[:space:]]*)([^[:space:]]+)([[:space:]].*[[:space:]]|[[:space:]]+)([^[:space:]]+)([[:space:]]*)$/14325/'
world! some good Hello
However, these commands use some non-POSIX GNU extensions to the BRE - Basic Regular Expression - syntax (namely, + and |).
A (more portable) command that satisfies the POSIX standard while keeping the convenience of alternation (|) would require Extended regular expressions. For example, using GNU sed with the --posix option (which disables GNU extensions):
$ echo " Hello some good world! " |
sed --posix -E 's/^([[:space:]]*)([^[:space:]]{1,})([[:space:]].*[[:space:]]|[[:space:]]{1,})([^[:space:]]{1,})([[:space:]]*)$/14325/'
world! some good Hello
edited 2 days ago
answered Nov 28 at 22:05
fra-san
1,026214
1,026214
3
Will not destroy the original whitespace. +1
– glenn jackman
Nov 28 at 22:22
add a comment |
3
Will not destroy the original whitespace. +1
– glenn jackman
Nov 28 at 22:22
3
3
Will not destroy the original whitespace. +1
– glenn jackman
Nov 28 at 22:22
Will not destroy the original whitespace. +1
– glenn jackman
Nov 28 at 22:22
add a comment |
Navpreet Singh is a new contributor. Be nice, and check out our Code of Conduct.
Navpreet Singh is a new contributor. Be nice, and check out our Code of Conduct.
Navpreet Singh is a new contributor. Be nice, and check out our Code of Conduct.
Navpreet Singh 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f484769%2fhow-do-i-swap-the-first-word-of-a-line-with-the-last-word-in-a-line%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
In every line, or a specific line. If so which line?
– Jesse_b
Nov 28 at 21:29