Switch characters in a string using sed
Clash Royale CLAN TAG#URR8PPP
How would you switch characters in a string using sed
?
Say we had a number 12345678
and I wanted to switch the 2nd and 4th digit with each other (i.e. number = 14325678
).
text-processing sed
add a comment |
How would you switch characters in a string using sed
?
Say we had a number 12345678
and I wanted to switch the 2nd and 4th digit with each other (i.e. number = 14325678
).
text-processing sed
sed
modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?
– Kusalananda
Feb 11 at 6:50
add a comment |
How would you switch characters in a string using sed
?
Say we had a number 12345678
and I wanted to switch the 2nd and 4th digit with each other (i.e. number = 14325678
).
text-processing sed
How would you switch characters in a string using sed
?
Say we had a number 12345678
and I wanted to switch the 2nd and 4th digit with each other (i.e. number = 14325678
).
text-processing sed
text-processing sed
edited Feb 11 at 3:47
αғsнιη
17k102966
17k102966
asked Feb 10 at 23:51
JKFJKF
61
61
sed
modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?
– Kusalananda
Feb 11 at 6:50
add a comment |
sed
modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?
– Kusalananda
Feb 11 at 6:50
sed
modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?– Kusalananda
Feb 11 at 6:50
sed
modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?– Kusalananda
Feb 11 at 6:50
add a comment |
2 Answers
2
active
oldest
votes
Group parts of the expression with parenthesis and later recall these groups in different order with n
where n
is a group index. For example
$ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
14325678
add a comment |
You can do it multiple ways :
echo 12345678 |
sed -e '
s/./&n/4
s/./n&/2
s/n(.)(.*)(.)n/321/
'
14325678
perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'
perl -F -lane '@F[1,3] = @F[3,1];print @F'
awk -F "" 't=$2;$2=$4;$4=t1' OFS=
With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.
With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.
With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.
HTH.
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
);
);
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%2f499836%2fswitch-characters-in-a-string-using-sed%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
Group parts of the expression with parenthesis and later recall these groups in different order with n
where n
is a group index. For example
$ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
14325678
add a comment |
Group parts of the expression with parenthesis and later recall these groups in different order with n
where n
is a group index. For example
$ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
14325678
add a comment |
Group parts of the expression with parenthesis and later recall these groups in different order with n
where n
is a group index. For example
$ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
14325678
Group parts of the expression with parenthesis and later recall these groups in different order with n
where n
is a group index. For example
$ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
14325678
answered Feb 11 at 0:02
jimmijjimmij
32.1k874108
32.1k874108
add a comment |
add a comment |
You can do it multiple ways :
echo 12345678 |
sed -e '
s/./&n/4
s/./n&/2
s/n(.)(.*)(.)n/321/
'
14325678
perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'
perl -F -lane '@F[1,3] = @F[3,1];print @F'
awk -F "" 't=$2;$2=$4;$4=t1' OFS=
With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.
With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.
With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.
HTH.
add a comment |
You can do it multiple ways :
echo 12345678 |
sed -e '
s/./&n/4
s/./n&/2
s/n(.)(.*)(.)n/321/
'
14325678
perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'
perl -F -lane '@F[1,3] = @F[3,1];print @F'
awk -F "" 't=$2;$2=$4;$4=t1' OFS=
With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.
With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.
With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.
HTH.
add a comment |
You can do it multiple ways :
echo 12345678 |
sed -e '
s/./&n/4
s/./n&/2
s/n(.)(.*)(.)n/321/
'
14325678
perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'
perl -F -lane '@F[1,3] = @F[3,1];print @F'
awk -F "" 't=$2;$2=$4;$4=t1' OFS=
With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.
With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.
With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.
HTH.
You can do it multiple ways :
echo 12345678 |
sed -e '
s/./&n/4
s/./n&/2
s/n(.)(.*)(.)n/321/
'
14325678
perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'
perl -F -lane '@F[1,3] = @F[3,1];print @F'
awk -F "" 't=$2;$2=$4;$4=t1' OFS=
With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.
With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.
With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.
HTH.
edited Feb 11 at 5:07
answered Feb 11 at 4:57
Rakesh SharmaRakesh Sharma
344114
344114
add a comment |
add a comment |
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%2f499836%2fswitch-characters-in-a-string-using-sed%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
sed
modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?– Kusalananda
Feb 11 at 6:50