Replacing strings in lines Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionReordering strings in linuxDelete lines between 2 strings in SolarisReplacing a character at a random position using sed?Delete multiple lines in a fileHow to escape special characters in long sed stringFormatting text - insert newline before commented linesReplacing multiple lines in Unixreplacing and adding at the end of lines with one line sed commandExtract all line from a file that contains two strings in any positionAdding tags to a sentence on each line
I need to find the potential function of a vector field.
Do I really need recursive chmod to restrict access to a folder?
How much radiation do nuclear physics experiments expose researchers to nowadays?
What is the longest distance a 13th-level monk can jump while attacking on the same turn?
Does surprise arrest existing movement?
Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?
Is there a concise way to say "all of the X, one of each"?
Why did the IBM 650 use bi-quinary?
What does '1 unit of lemon juice' mean in a grandma's drink recipe?
Stars Make Stars
Determinant is linear as a function of each of the rows of the matrix.
How to recreate this effect in Photoshop?
If Jon Snow became King of the Seven Kingdoms what would his regnal number be?
Sorting numerically
How to deal with a team lead who never gives me credit?
How can players work together to take actions that are otherwise impossible?
If a contract sometimes uses the wrong name, is it still valid?
Why does Python start at index -1 when indexing a list from the end?
Does accepting a pardon have any bearing on trying that person for the same crime in a sovereign jurisdiction?
Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?
Why are there no cargo aircraft with "flying wing" design?
How can I fade player character when he goes inside or outside of the area?
Why is black pepper both grey and black?
Disable hyphenation for an entire paragraph
Replacing strings in lines
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionReordering strings in linuxDelete lines between 2 strings in SolarisReplacing a character at a random position using sed?Delete multiple lines in a fileHow to escape special characters in long sed stringFormatting text - insert newline before commented linesReplacing multiple lines in Unixreplacing and adding at the end of lines with one line sed commandExtract all line from a file that contains two strings in any positionAdding tags to a sentence on each line
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have lines like this:
400 january ####
304 april ####
151 may ####
126 june ####
115 august ####
98 december ####
And I want them to look like this:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
I tried using this command, but no avail.
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed
add a comment |
I have lines like this:
400 january ####
304 april ####
151 may ####
126 june ####
115 august ####
98 december ####
And I want them to look like this:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
I tried using this command, but no avail.
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed
add a comment |
I have lines like this:
400 january ####
304 april ####
151 may ####
126 june ####
115 august ####
98 december ####
And I want them to look like this:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
I tried using this command, but no avail.
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed
I have lines like this:
400 january ####
304 april ####
151 may ####
126 june ####
115 august ####
98 december ####
And I want them to look like this:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
I tried using this command, but no avail.
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed
sed
edited Apr 12 at 15:02
Rui F Ribeiro
42.1k1483142
42.1k1483142
asked Apr 11 at 21:14
Laura Laura
433
433
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Try:
$ sed -E 's/^(.*[0-9])(.*[[:alpha:]])(.*)/3 2 1/' file
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
How it works
^(.*[0-9])
matches from the beginning of the line to the last number of the line.(.*[[:alpha:]])
matches from the after the above to the last alphabetic character on the line.(.*)
matches anything after the last alphabetic character on the line.
Discussion
Consider:
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed's regexes match leftmost-longest. That means that the first (.*)
above matches the entire line. The remaining groups are empty. If we modify the command, this becomes clear:
$ sed -E 's/(.*)(.*)(.*) /3=3 and 2=2 and 1=1/' file
3= and 2= and 1= 400 january ####
3= and 2= and 1= 304 april ####
3= and 2= and 1= 151 may ####
3= and 2= and 1= 126 june ####
3= and 2= and 1= 115 august ####
3= and 2= and 1= 98 december ####
add a comment |
Here is a simpler way to do it:
awk 'print $3," "$2," "$1' file
It just uses awk
to print columns 3, 2, and 1 with two spaces between the 3rd and 2nd column and four spaces between the 2nd and 1st column.
Output:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
If you want it to edit the file in place, use this command:
awk -i inplace 'print $3," "$2," "$1' file
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%2f511999%2freplacing-strings-in-lines%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
Try:
$ sed -E 's/^(.*[0-9])(.*[[:alpha:]])(.*)/3 2 1/' file
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
How it works
^(.*[0-9])
matches from the beginning of the line to the last number of the line.(.*[[:alpha:]])
matches from the after the above to the last alphabetic character on the line.(.*)
matches anything after the last alphabetic character on the line.
Discussion
Consider:
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed's regexes match leftmost-longest. That means that the first (.*)
above matches the entire line. The remaining groups are empty. If we modify the command, this becomes clear:
$ sed -E 's/(.*)(.*)(.*) /3=3 and 2=2 and 1=1/' file
3= and 2= and 1= 400 january ####
3= and 2= and 1= 304 april ####
3= and 2= and 1= 151 may ####
3= and 2= and 1= 126 june ####
3= and 2= and 1= 115 august ####
3= and 2= and 1= 98 december ####
add a comment |
Try:
$ sed -E 's/^(.*[0-9])(.*[[:alpha:]])(.*)/3 2 1/' file
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
How it works
^(.*[0-9])
matches from the beginning of the line to the last number of the line.(.*[[:alpha:]])
matches from the after the above to the last alphabetic character on the line.(.*)
matches anything after the last alphabetic character on the line.
Discussion
Consider:
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed's regexes match leftmost-longest. That means that the first (.*)
above matches the entire line. The remaining groups are empty. If we modify the command, this becomes clear:
$ sed -E 's/(.*)(.*)(.*) /3=3 and 2=2 and 1=1/' file
3= and 2= and 1= 400 january ####
3= and 2= and 1= 304 april ####
3= and 2= and 1= 151 may ####
3= and 2= and 1= 126 june ####
3= and 2= and 1= 115 august ####
3= and 2= and 1= 98 december ####
add a comment |
Try:
$ sed -E 's/^(.*[0-9])(.*[[:alpha:]])(.*)/3 2 1/' file
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
How it works
^(.*[0-9])
matches from the beginning of the line to the last number of the line.(.*[[:alpha:]])
matches from the after the above to the last alphabetic character on the line.(.*)
matches anything after the last alphabetic character on the line.
Discussion
Consider:
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed's regexes match leftmost-longest. That means that the first (.*)
above matches the entire line. The remaining groups are empty. If we modify the command, this becomes clear:
$ sed -E 's/(.*)(.*)(.*) /3=3 and 2=2 and 1=1/' file
3= and 2= and 1= 400 january ####
3= and 2= and 1= 304 april ####
3= and 2= and 1= 151 may ####
3= and 2= and 1= 126 june ####
3= and 2= and 1= 115 august ####
3= and 2= and 1= 98 december ####
Try:
$ sed -E 's/^(.*[0-9])(.*[[:alpha:]])(.*)/3 2 1/' file
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
How it works
^(.*[0-9])
matches from the beginning of the line to the last number of the line.(.*[[:alpha:]])
matches from the after the above to the last alphabetic character on the line.(.*)
matches anything after the last alphabetic character on the line.
Discussion
Consider:
sed -E 's/(.*)(.*)(.*) /3 2 1/'
sed's regexes match leftmost-longest. That means that the first (.*)
above matches the entire line. The remaining groups are empty. If we modify the command, this becomes clear:
$ sed -E 's/(.*)(.*)(.*) /3=3 and 2=2 and 1=1/' file
3= and 2= and 1= 400 january ####
3= and 2= and 1= 304 april ####
3= and 2= and 1= 151 may ####
3= and 2= and 1= 126 june ####
3= and 2= and 1= 115 august ####
3= and 2= and 1= 98 december ####
edited Apr 11 at 21:27
answered Apr 11 at 21:21
John1024John1024
48.7k5114129
48.7k5114129
add a comment |
add a comment |
Here is a simpler way to do it:
awk 'print $3," "$2," "$1' file
It just uses awk
to print columns 3, 2, and 1 with two spaces between the 3rd and 2nd column and four spaces between the 2nd and 1st column.
Output:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
If you want it to edit the file in place, use this command:
awk -i inplace 'print $3," "$2," "$1' file
add a comment |
Here is a simpler way to do it:
awk 'print $3," "$2," "$1' file
It just uses awk
to print columns 3, 2, and 1 with two spaces between the 3rd and 2nd column and four spaces between the 2nd and 1st column.
Output:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
If you want it to edit the file in place, use this command:
awk -i inplace 'print $3," "$2," "$1' file
add a comment |
Here is a simpler way to do it:
awk 'print $3," "$2," "$1' file
It just uses awk
to print columns 3, 2, and 1 with two spaces between the 3rd and 2nd column and four spaces between the 2nd and 1st column.
Output:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
If you want it to edit the file in place, use this command:
awk -i inplace 'print $3," "$2," "$1' file
Here is a simpler way to do it:
awk 'print $3," "$2," "$1' file
It just uses awk
to print columns 3, 2, and 1 with two spaces between the 3rd and 2nd column and four spaces between the 2nd and 1st column.
Output:
#### january 400
#### april 304
#### may 151
#### june 126
#### august 115
#### december 98
If you want it to edit the file in place, use this command:
awk -i inplace 'print $3," "$2," "$1' file
edited Apr 12 at 11:54
answered Apr 11 at 21:42
Nasir RileyNasir Riley
3,0622410
3,0622410
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%2f511999%2freplacing-strings-in-lines%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