How to prepend a string to only the lines of text which are numbers The Next CEO of Stack OverflowAutomatically doubling the value of numbers in a string in multiple filesHow can I count lines of differently named files, and write the outcome to a csv file?Multiplying numbers in file by random numbersHow to get line from a file using line number and edit it easily?Only in lines with a specific string replace another stringDelete all lines from middle of a line matching a string until the second string match is foundInsert a line of text after the line containing the last occurence of a specified wordDelete ranges of lines, but skip the comments which come in between the linesHow to recursively go through all text files in a directory to fetch the desired line, and write these lines to same text file?grep and sed only the numbers from a text file's line
What difference does it make using sed with/without whitespaces?
Is French Guiana a (hard) EU border?
Is it correct to say moon starry nights?
Where do students learn to solve polynomial equations these days?
What is the difference between "hamstring tendon" and "common hamstring tendon"?
Expressing the idea of having a very busy time
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
Does Germany produce more waste than the US?
Easy to read palindrome checker
Defamation due to breach of confidentiality
How to use ReplaceAll on an expression that contains a rule
How did Beeri the Hittite come up with naming his daughter Yehudit?
Calculate the Mean mean of two numbers
Is it ever safe to open a suspicious HTML file (e.g. email attachment)?
From jafe to El-Guest
Help! I cannot understand this game’s notations!
Reference request: Grassmannian and Plucker coordinates in type B, C, D
Is it convenient to ask the journal's editor for two additional days to complete a review?
How many extra stops do monopods offer for tele photographs?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
Is "three point ish" an acceptable use of ish?
What would be the main consequences for a country leaving the WTO?
What is the difference between Statistical Mechanics and Quantum Mechanics
How to prepend a string to only the lines of text which are numbers
The Next CEO of Stack OverflowAutomatically doubling the value of numbers in a string in multiple filesHow can I count lines of differently named files, and write the outcome to a csv file?Multiplying numbers in file by random numbersHow to get line from a file using line number and edit it easily?Only in lines with a specific string replace another stringDelete all lines from middle of a line matching a string until the second string match is foundInsert a line of text after the line containing the last occurence of a specified wordDelete ranges of lines, but skip the comments which come in between the linesHow to recursively go through all text files in a directory to fetch the desired line, and write these lines to same text file?grep and sed only the numbers from a text file's line
Suppose I have 6 lines of text.
Series
Of
Word
73914
Again
Word
I need to prepend a string to the beginning of lines that contain ONLY numbers. Say I insert number-
Series
Of
Word
number-73914
Again
Word
Currently I run two commands to achieve the desired result. I wonder if there is a more efficient method.
Note: There are 1000+ lines, so preferably this applies to all lines ( I already state it ).
command-line text-processing sed awk
add a comment |
Suppose I have 6 lines of text.
Series
Of
Word
73914
Again
Word
I need to prepend a string to the beginning of lines that contain ONLY numbers. Say I insert number-
Series
Of
Word
number-73914
Again
Word
Currently I run two commands to achieve the desired result. I wonder if there is a more efficient method.
Note: There are 1000+ lines, so preferably this applies to all lines ( I already state it ).
command-line text-processing sed awk
What do you want to do with a line like1st word 2nd word
?
– glenn jackman
2 days ago
And do you want only lines that are entirely a number or that start with a number? E.g. would you want123abc
to be changed tonumber-123abc
or not?
– Kevin
2 days ago
add a comment |
Suppose I have 6 lines of text.
Series
Of
Word
73914
Again
Word
I need to prepend a string to the beginning of lines that contain ONLY numbers. Say I insert number-
Series
Of
Word
number-73914
Again
Word
Currently I run two commands to achieve the desired result. I wonder if there is a more efficient method.
Note: There are 1000+ lines, so preferably this applies to all lines ( I already state it ).
command-line text-processing sed awk
Suppose I have 6 lines of text.
Series
Of
Word
73914
Again
Word
I need to prepend a string to the beginning of lines that contain ONLY numbers. Say I insert number-
Series
Of
Word
number-73914
Again
Word
Currently I run two commands to achieve the desired result. I wonder if there is a more efficient method.
Note: There are 1000+ lines, so preferably this applies to all lines ( I already state it ).
command-line text-processing sed awk
command-line text-processing sed awk
edited 2 days ago
Martin Thornton
2,56551830
2,56551830
asked 2 days ago
EmmetEmmet
7,35522345
7,35522345
What do you want to do with a line like1st word 2nd word
?
– glenn jackman
2 days ago
And do you want only lines that are entirely a number or that start with a number? E.g. would you want123abc
to be changed tonumber-123abc
or not?
– Kevin
2 days ago
add a comment |
What do you want to do with a line like1st word 2nd word
?
– glenn jackman
2 days ago
And do you want only lines that are entirely a number or that start with a number? E.g. would you want123abc
to be changed tonumber-123abc
or not?
– Kevin
2 days ago
What do you want to do with a line like
1st word 2nd word
?– glenn jackman
2 days ago
What do you want to do with a line like
1st word 2nd word
?– glenn jackman
2 days ago
And do you want only lines that are entirely a number or that start with a number? E.g. would you want
123abc
to be changed to number-123abc
or not?– Kevin
2 days ago
And do you want only lines that are entirely a number or that start with a number? E.g. would you want
123abc
to be changed to number-123abc
or not?– Kevin
2 days ago
add a comment |
4 Answers
4
active
oldest
votes
sed
can do that:
$ sed 's/^[[:digit:]]*$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
In case we want to account for empty lines, we'd use +
and -r
option:
$ sed -r 's/^[[:digit:]]+$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
Once you verify everything is proper, you can use -i
option to edit the file itself, i.e. sed -i ...
. Otherwise, you can always make a copy of the file with sed 's/^[[:digit:]]*$/number-&/' input.txt > output.txt
Note that this assumes consistent file format, with no leading whitespaces or trailing whitespaces on each line.
And here's Python as extra:
$ python3 -c 'import sys; print("n".join([ "number-" + i.strip() if i.strip().isnumeric() else i.strip() for i in sys.stdin]))' < input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
2
I often use the "substitute if match" variant for things like this/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
2 days ago
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
2 days ago
add a comment |
One way using awk:
awk '/^[0-9]+$/$0="number-"$0;1' file
I also seekawk
solution, thanks ! 1+
– Emmet
2 days ago
add a comment |
lines that contain ONLY numbers
It's unclear whether you mean numbers or just 0-9. Here's a Perl one-liner that picks out the likes of 123, 3.14 and 1e-12 while ignoring various representations of infinity and not-a-number:
$ perl -MScalar::Util -ne 'chomp; if (!(m/^s/ || m/^[+-]?inf(?:inity)?$/i || m/^nan$/i) && Scalar::Util::looks_like_number($_)) print("N:"); print("$_n");' <x
a
N:123
N:+1
N:-1
1
b
1a
N:3.14
c
3.1415926 is an approximation of pi
N:1e-12
inf
Inf
Infinity
Infinity +1 sword
+Infinity
-infinity
NaN
1/2
I changed the prefix to "N:" simply because "number--1" looks a bit rubbish. Note that this treats " 1", for example, as not numeric. If that is undesirable behaviour, do not include the "m/^s/" test for leading whitespace.
If you mean "0-9", Sergiy's sed solution above is fine.
New contributor
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
2 days ago
add a comment |
You can try this
$cat input.txt
Series
Of
Word
73914
Again
Word
$awk ' if($1 ~/[0-9]/) printf "number - %sn",$1; else print $1 ' input.txt
Series
Of
Word
number - 73914
Again
Word
Usingif
statement, nice ! Thank you :D
– Emmet
2 days ago
add a comment |
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1129567%2fhow-to-prepend-a-string-to-only-the-lines-of-text-which-are-numbers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed
can do that:
$ sed 's/^[[:digit:]]*$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
In case we want to account for empty lines, we'd use +
and -r
option:
$ sed -r 's/^[[:digit:]]+$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
Once you verify everything is proper, you can use -i
option to edit the file itself, i.e. sed -i ...
. Otherwise, you can always make a copy of the file with sed 's/^[[:digit:]]*$/number-&/' input.txt > output.txt
Note that this assumes consistent file format, with no leading whitespaces or trailing whitespaces on each line.
And here's Python as extra:
$ python3 -c 'import sys; print("n".join([ "number-" + i.strip() if i.strip().isnumeric() else i.strip() for i in sys.stdin]))' < input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
2
I often use the "substitute if match" variant for things like this/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
2 days ago
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
2 days ago
add a comment |
sed
can do that:
$ sed 's/^[[:digit:]]*$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
In case we want to account for empty lines, we'd use +
and -r
option:
$ sed -r 's/^[[:digit:]]+$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
Once you verify everything is proper, you can use -i
option to edit the file itself, i.e. sed -i ...
. Otherwise, you can always make a copy of the file with sed 's/^[[:digit:]]*$/number-&/' input.txt > output.txt
Note that this assumes consistent file format, with no leading whitespaces or trailing whitespaces on each line.
And here's Python as extra:
$ python3 -c 'import sys; print("n".join([ "number-" + i.strip() if i.strip().isnumeric() else i.strip() for i in sys.stdin]))' < input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
2
I often use the "substitute if match" variant for things like this/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
2 days ago
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
2 days ago
add a comment |
sed
can do that:
$ sed 's/^[[:digit:]]*$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
In case we want to account for empty lines, we'd use +
and -r
option:
$ sed -r 's/^[[:digit:]]+$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
Once you verify everything is proper, you can use -i
option to edit the file itself, i.e. sed -i ...
. Otherwise, you can always make a copy of the file with sed 's/^[[:digit:]]*$/number-&/' input.txt > output.txt
Note that this assumes consistent file format, with no leading whitespaces or trailing whitespaces on each line.
And here's Python as extra:
$ python3 -c 'import sys; print("n".join([ "number-" + i.strip() if i.strip().isnumeric() else i.strip() for i in sys.stdin]))' < input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
sed
can do that:
$ sed 's/^[[:digit:]]*$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
In case we want to account for empty lines, we'd use +
and -r
option:
$ sed -r 's/^[[:digit:]]+$/number-&/' input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
Once you verify everything is proper, you can use -i
option to edit the file itself, i.e. sed -i ...
. Otherwise, you can always make a copy of the file with sed 's/^[[:digit:]]*$/number-&/' input.txt > output.txt
Note that this assumes consistent file format, with no leading whitespaces or trailing whitespaces on each line.
And here's Python as extra:
$ python3 -c 'import sys; print("n".join([ "number-" + i.strip() if i.strip().isnumeric() else i.strip() for i in sys.stdin]))' < input.txt
Series
Of
Word
number-73914
Again
Word
line below is empty
line above is empty
edited 2 days ago
answered 2 days ago
Sergiy KolodyazhnyySergiy Kolodyazhnyy
74.7k9155325
74.7k9155325
2
I often use the "substitute if match" variant for things like this/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
2 days ago
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
2 days ago
add a comment |
2
I often use the "substitute if match" variant for things like this/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
2 days ago
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
2 days ago
2
2
I often use the "substitute if match" variant for things like this
/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
2 days ago
I often use the "substitute if match" variant for things like this
/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
2 days ago
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
2 days ago
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
2 days ago
add a comment |
One way using awk:
awk '/^[0-9]+$/$0="number-"$0;1' file
I also seekawk
solution, thanks ! 1+
– Emmet
2 days ago
add a comment |
One way using awk:
awk '/^[0-9]+$/$0="number-"$0;1' file
I also seekawk
solution, thanks ! 1+
– Emmet
2 days ago
add a comment |
One way using awk:
awk '/^[0-9]+$/$0="number-"$0;1' file
One way using awk:
awk '/^[0-9]+$/$0="number-"$0;1' file
answered 2 days ago
GuruGuru
52838
52838
I also seekawk
solution, thanks ! 1+
– Emmet
2 days ago
add a comment |
I also seekawk
solution, thanks ! 1+
– Emmet
2 days ago
I also seek
awk
solution, thanks ! 1+– Emmet
2 days ago
I also seek
awk
solution, thanks ! 1+– Emmet
2 days ago
add a comment |
lines that contain ONLY numbers
It's unclear whether you mean numbers or just 0-9. Here's a Perl one-liner that picks out the likes of 123, 3.14 and 1e-12 while ignoring various representations of infinity and not-a-number:
$ perl -MScalar::Util -ne 'chomp; if (!(m/^s/ || m/^[+-]?inf(?:inity)?$/i || m/^nan$/i) && Scalar::Util::looks_like_number($_)) print("N:"); print("$_n");' <x
a
N:123
N:+1
N:-1
1
b
1a
N:3.14
c
3.1415926 is an approximation of pi
N:1e-12
inf
Inf
Infinity
Infinity +1 sword
+Infinity
-infinity
NaN
1/2
I changed the prefix to "N:" simply because "number--1" looks a bit rubbish. Note that this treats " 1", for example, as not numeric. If that is undesirable behaviour, do not include the "m/^s/" test for leading whitespace.
If you mean "0-9", Sergiy's sed solution above is fine.
New contributor
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
2 days ago
add a comment |
lines that contain ONLY numbers
It's unclear whether you mean numbers or just 0-9. Here's a Perl one-liner that picks out the likes of 123, 3.14 and 1e-12 while ignoring various representations of infinity and not-a-number:
$ perl -MScalar::Util -ne 'chomp; if (!(m/^s/ || m/^[+-]?inf(?:inity)?$/i || m/^nan$/i) && Scalar::Util::looks_like_number($_)) print("N:"); print("$_n");' <x
a
N:123
N:+1
N:-1
1
b
1a
N:3.14
c
3.1415926 is an approximation of pi
N:1e-12
inf
Inf
Infinity
Infinity +1 sword
+Infinity
-infinity
NaN
1/2
I changed the prefix to "N:" simply because "number--1" looks a bit rubbish. Note that this treats " 1", for example, as not numeric. If that is undesirable behaviour, do not include the "m/^s/" test for leading whitespace.
If you mean "0-9", Sergiy's sed solution above is fine.
New contributor
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
2 days ago
add a comment |
lines that contain ONLY numbers
It's unclear whether you mean numbers or just 0-9. Here's a Perl one-liner that picks out the likes of 123, 3.14 and 1e-12 while ignoring various representations of infinity and not-a-number:
$ perl -MScalar::Util -ne 'chomp; if (!(m/^s/ || m/^[+-]?inf(?:inity)?$/i || m/^nan$/i) && Scalar::Util::looks_like_number($_)) print("N:"); print("$_n");' <x
a
N:123
N:+1
N:-1
1
b
1a
N:3.14
c
3.1415926 is an approximation of pi
N:1e-12
inf
Inf
Infinity
Infinity +1 sword
+Infinity
-infinity
NaN
1/2
I changed the prefix to "N:" simply because "number--1" looks a bit rubbish. Note that this treats " 1", for example, as not numeric. If that is undesirable behaviour, do not include the "m/^s/" test for leading whitespace.
If you mean "0-9", Sergiy's sed solution above is fine.
New contributor
lines that contain ONLY numbers
It's unclear whether you mean numbers or just 0-9. Here's a Perl one-liner that picks out the likes of 123, 3.14 and 1e-12 while ignoring various representations of infinity and not-a-number:
$ perl -MScalar::Util -ne 'chomp; if (!(m/^s/ || m/^[+-]?inf(?:inity)?$/i || m/^nan$/i) && Scalar::Util::looks_like_number($_)) print("N:"); print("$_n");' <x
a
N:123
N:+1
N:-1
1
b
1a
N:3.14
c
3.1415926 is an approximation of pi
N:1e-12
inf
Inf
Infinity
Infinity +1 sword
+Infinity
-infinity
NaN
1/2
I changed the prefix to "N:" simply because "number--1" looks a bit rubbish. Note that this treats " 1", for example, as not numeric. If that is undesirable behaviour, do not include the "m/^s/" test for leading whitespace.
If you mean "0-9", Sergiy's sed solution above is fine.
New contributor
New contributor
answered 2 days ago
Chris WilliamsChris Williams
311
311
New contributor
New contributor
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
2 days ago
add a comment |
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
2 days ago
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
2 days ago
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
2 days ago
add a comment |
You can try this
$cat input.txt
Series
Of
Word
73914
Again
Word
$awk ' if($1 ~/[0-9]/) printf "number - %sn",$1; else print $1 ' input.txt
Series
Of
Word
number - 73914
Again
Word
Usingif
statement, nice ! Thank you :D
– Emmet
2 days ago
add a comment |
You can try this
$cat input.txt
Series
Of
Word
73914
Again
Word
$awk ' if($1 ~/[0-9]/) printf "number - %sn",$1; else print $1 ' input.txt
Series
Of
Word
number - 73914
Again
Word
Usingif
statement, nice ! Thank you :D
– Emmet
2 days ago
add a comment |
You can try this
$cat input.txt
Series
Of
Word
73914
Again
Word
$awk ' if($1 ~/[0-9]/) printf "number - %sn",$1; else print $1 ' input.txt
Series
Of
Word
number - 73914
Again
Word
You can try this
$cat input.txt
Series
Of
Word
73914
Again
Word
$awk ' if($1 ~/[0-9]/) printf "number - %sn",$1; else print $1 ' input.txt
Series
Of
Word
number - 73914
Again
Word
answered 2 days ago
GoronGoron
668
668
Usingif
statement, nice ! Thank you :D
– Emmet
2 days ago
add a comment |
Usingif
statement, nice ! Thank you :D
– Emmet
2 days ago
Using
if
statement, nice ! Thank you :D– Emmet
2 days ago
Using
if
statement, nice ! Thank you :D– Emmet
2 days ago
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1129567%2fhow-to-prepend-a-string-to-only-the-lines-of-text-which-are-numbers%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
What do you want to do with a line like
1st word 2nd word
?– glenn jackman
2 days ago
And do you want only lines that are entirely a number or that start with a number? E.g. would you want
123abc
to be changed tonumber-123abc
or not?– Kevin
2 days ago