Copy digits from end of string to anotherHow to permanently change a file using awk? (“in-place” edits,...
Why isn't Bash trap working if output is redirected to stdout?
Options basics: How to realize profit from a long call position
Why did the World Bank set the global poverty line at $1.90?
Do you have to have figures when playing D&D?
Grep Match and extract
What is Gilligan's full Name?
Use 1 9 6 2 in this order to make 75
How to get depth and other lengths of a font?
Canada travel to US using Global Entry
Convert only certain words to lowercase
Oil draining out shortly after turbo hose detached/broke
Seasonality after 1st differencing
Rail-to-rail op-amp only reaches 90% of VCC, works sometimes, not everytime
bash vs. zsh: What are the practical differences?
What do Birth, Age, and Death mean in the first noble truth?
If absolute velocity does not exist, how can we say a rocket accelerates in empty space?
How (un)safe is it to ride barefoot?
Suppose leased car is totalled: what are financial implications?
If there's something that implicates the president why is there then a national security issue? (John Dowd)
Why is long-term living in Almost-Earth causing severe health problems?
Do you need to let the DM know when you are multiclassing?
The origin of the Russian proverb about two hares
Should I put programming books I wrote a few years ago on my resume?
Why is the length of the Kelvin unit of temperature equal to that of the Celsius unit?
Copy digits from end of string to another
How to permanently change a file using awk? (“in-place” edits, as with “sed -i”)sed remove end of line for specific linesPrint a line in stdout that matches an expression if the output contains another expressionSplit string into array and print each element on a new line with commandlineIs there any way to Copy and Paste from one file(from beginning to end) to another using SED and AWK?sed remove digits from end of stirngawk remove lines with digits at endExtract words from multiple strings using AWK/SEDRemove “n” string from end of certain linesFiltering list of numbers containing sequential digitsHow to extract second element from path?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
How could I copy digits from one end of a string to another end of a string? So example,
Input -
Example123:Hello
Exp12:Hey1
Exp:heylo
expected output -
Example123:Hello123
Exp12:Hey112
Exp:heylo
I'm open to using sed or awk, seperator must be accounted for, so row1 is the row to extract digits from and row 2 is the row to place digits
awk sed
New contributor
add a comment |
How could I copy digits from one end of a string to another end of a string? So example,
Input -
Example123:Hello
Exp12:Hey1
Exp:heylo
expected output -
Example123:Hello123
Exp12:Hey112
Exp:heylo
I'm open to using sed or awk, seperator must be accounted for, so row1 is the row to extract digits from and row 2 is the row to place digits
awk sed
New contributor
2
From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.
– Stéphane Chazelas
1 hour ago
add a comment |
How could I copy digits from one end of a string to another end of a string? So example,
Input -
Example123:Hello
Exp12:Hey1
Exp:heylo
expected output -
Example123:Hello123
Exp12:Hey112
Exp:heylo
I'm open to using sed or awk, seperator must be accounted for, so row1 is the row to extract digits from and row 2 is the row to place digits
awk sed
New contributor
How could I copy digits from one end of a string to another end of a string? So example,
Input -
Example123:Hello
Exp12:Hey1
Exp:heylo
expected output -
Example123:Hello123
Exp12:Hey112
Exp:heylo
I'm open to using sed or awk, seperator must be accounted for, so row1 is the row to extract digits from and row 2 is the row to place digits
awk sed
awk sed
New contributor
New contributor
edited 1 hour ago
Sparhawk
10.7k848102
10.7k848102
New contributor
asked 2 hours ago
user357075user357075
1
1
New contributor
New contributor
2
From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.
– Stéphane Chazelas
1 hour ago
add a comment |
2
From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.
– Stéphane Chazelas
1 hour ago
2
2
From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.
– Stéphane Chazelas
1 hour ago
From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.
– Stéphane Chazelas
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
Assuming there's one and only one occurrence of :
in each line of the input, you could do something like:
sed 's/([[:digit:]]*):.*/&1/' < input
If there can be more than one :
(and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:
sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input
add a comment |
You can use awk here
awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}'
Explanation
-F':'
: tells awk to use colon as the separator
j=$1; gsub(/[^0-9]+/,"",j)
: assigns the first column to to a temporary variable j
, and then removes anything that is not a digit from j
printf "%s%sn",$0,j;}'
: finally prints the original string appended with j
that carries our number
add a comment |
With the match()
function of GNU awk to store the matching group in an array, you could do
awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file
On any POSIX compliant awk
you could do
awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file
See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.
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
});
}
});
user357075 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');
});
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%2f523920%2fcopy-digits-from-end-of-string-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming there's one and only one occurrence of :
in each line of the input, you could do something like:
sed 's/([[:digit:]]*):.*/&1/' < input
If there can be more than one :
(and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:
sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input
add a comment |
Assuming there's one and only one occurrence of :
in each line of the input, you could do something like:
sed 's/([[:digit:]]*):.*/&1/' < input
If there can be more than one :
(and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:
sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input
add a comment |
Assuming there's one and only one occurrence of :
in each line of the input, you could do something like:
sed 's/([[:digit:]]*):.*/&1/' < input
If there can be more than one :
(and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:
sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input
Assuming there's one and only one occurrence of :
in each line of the input, you could do something like:
sed 's/([[:digit:]]*):.*/&1/' < input
If there can be more than one :
(and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:
sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input
edited 1 hour ago
answered 1 hour ago
Stéphane ChazelasStéphane Chazelas
321k57613983
321k57613983
add a comment |
add a comment |
You can use awk here
awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}'
Explanation
-F':'
: tells awk to use colon as the separator
j=$1; gsub(/[^0-9]+/,"",j)
: assigns the first column to to a temporary variable j
, and then removes anything that is not a digit from j
printf "%s%sn",$0,j;}'
: finally prints the original string appended with j
that carries our number
add a comment |
You can use awk here
awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}'
Explanation
-F':'
: tells awk to use colon as the separator
j=$1; gsub(/[^0-9]+/,"",j)
: assigns the first column to to a temporary variable j
, and then removes anything that is not a digit from j
printf "%s%sn",$0,j;}'
: finally prints the original string appended with j
that carries our number
add a comment |
You can use awk here
awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}'
Explanation
-F':'
: tells awk to use colon as the separator
j=$1; gsub(/[^0-9]+/,"",j)
: assigns the first column to to a temporary variable j
, and then removes anything that is not a digit from j
printf "%s%sn",$0,j;}'
: finally prints the original string appended with j
that carries our number
You can use awk here
awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}'
Explanation
-F':'
: tells awk to use colon as the separator
j=$1; gsub(/[^0-9]+/,"",j)
: assigns the first column to to a temporary variable j
, and then removes anything that is not a digit from j
printf "%s%sn",$0,j;}'
: finally prints the original string appended with j
that carries our number
answered 1 hour ago
amisaxamisax
1,636615
1,636615
add a comment |
add a comment |
With the match()
function of GNU awk to store the matching group in an array, you could do
awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file
On any POSIX compliant awk
you could do
awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file
See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.
add a comment |
With the match()
function of GNU awk to store the matching group in an array, you could do
awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file
On any POSIX compliant awk
you could do
awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file
See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.
add a comment |
With the match()
function of GNU awk to store the matching group in an array, you could do
awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file
On any POSIX compliant awk
you could do
awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file
See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.
With the match()
function of GNU awk to store the matching group in an array, you could do
awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file
On any POSIX compliant awk
you could do
awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file
See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.
edited 1 hour ago
Stéphane Chazelas
321k57613983
321k57613983
answered 1 hour ago
InianInian
6,4401734
6,4401734
add a comment |
add a comment |
user357075 is a new contributor. Be nice, and check out our Code of Conduct.
user357075 is a new contributor. Be nice, and check out our Code of Conduct.
user357075 is a new contributor. Be nice, and check out our Code of Conduct.
user357075 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');
});
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%2f523920%2fcopy-digits-from-end-of-string-to-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');
});
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
2
From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.
– Stéphane Chazelas
1 hour ago