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
Is there a difference between "Fahrstuhl" and "Aufzug"
I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin
Does Germany produce more waste than the US?
Why doesn't UK go for the same deal Japan has with EU to resolve Brexit?
Where do students learn to solve polynomial equations these days?
What steps are necessary to read a Modern SSD in Medieval Europe?
A small doubt about the dominated convergence theorem
Recycling old answers
How to find the nth term in the following sequence: 1,1,2,2,4,4,8,8,16,16
How do I align (1) and (2)?
How to edit “Name” property in GCI output?
TikZ: How to reverse arrow direction without switching start/end point?
Why isn't the Mueller report being released completely and unredacted?
Necessary condition on homology group for a set to be contractible
Make solar eclipses exceedingly rare, but still have new moons
Is it convenient to ask the journal's editor for two additional days to complete a review?
Grabbing quick drinks
Rotate a column
What does "Its cash flow is deeply negative" mean?
Flying from Cape Town to England and return to another province
Poetry, calligrams and TikZ/PStricks challenge
Which one is the true statement?
What connection does MS Office have to Netscape Navigator?
How to count occurrences of text in a file?
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
yesterday
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
yesterday
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 yesterday
Martin Thornton
2,56451830
2,56451830
asked 2 days ago
EmmetEmmet
7,34522345
7,34522345
What do you want to do with a line like1st word 2nd word
?
– glenn jackman
yesterday
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
yesterday
add a comment |
What do you want to do with a line like1st word 2nd word
?
– glenn jackman
yesterday
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
yesterday
What do you want to do with a line like
1st word 2nd word
?– glenn jackman
yesterday
What do you want to do with a line like
1st word 2nd word
?– glenn jackman
yesterday
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
yesterday
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
yesterday
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
yesterday
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
yesterday
add a comment |
One way using awk:
awk '/^[0-9]+$/$0="number-"$0;1' file
I also seekawk
solution, thanks ! 1+
– Emmet
yesterday
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
yesterday
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
yesterday
add a comment |
Your Answer
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
yesterday
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
yesterday
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
yesterday
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
yesterday
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 yesterday
answered yesterday
Sergiy KolodyazhnyySergiy Kolodyazhnyy
74.7k9155325
74.7k9155325
2
I often use the "substitute if match" variant for things like this/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
yesterday
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
yesterday
add a comment |
2
I often use the "substitute if match" variant for things like this/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
yesterday
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
yesterday
2
2
I often use the "substitute if match" variant for things like this
/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
yesterday
I often use the "substitute if match" variant for things like this
/^[[:digit:]]+$/ s/^/number-/'
– steeldriver
yesterday
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
yesterday
Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O
– Emmet
yesterday
add a comment |
One way using awk:
awk '/^[0-9]+$/$0="number-"$0;1' file
I also seekawk
solution, thanks ! 1+
– Emmet
yesterday
add a comment |
One way using awk:
awk '/^[0-9]+$/$0="number-"$0;1' file
I also seekawk
solution, thanks ! 1+
– Emmet
yesterday
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 yesterday
GuruGuru
52838
52838
I also seekawk
solution, thanks ! 1+
– Emmet
yesterday
add a comment |
I also seekawk
solution, thanks ! 1+
– Emmet
yesterday
I also seek
awk
solution, thanks ! 1+– Emmet
yesterday
I also seek
awk
solution, thanks ! 1+– Emmet
yesterday
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
yesterday
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
yesterday
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 yesterday
Chris WilliamsChris Williams
311
311
New contributor
New contributor
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
yesterday
add a comment |
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
yesterday
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
yesterday
Probably an overkill for this question, but still awesome ! +1
– Sergiy Kolodyazhnyy
yesterday
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
yesterday
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
yesterday
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 yesterday
GoronGoron
668
668
Usingif
statement, nice ! Thank you :D
– Emmet
yesterday
add a comment |
Usingif
statement, nice ! Thank you :D
– Emmet
yesterday
Using
if
statement, nice ! Thank you :D– Emmet
yesterday
Using
if
statement, nice ! Thank you :D– Emmet
yesterday
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
yesterday
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
yesterday