How to prepend a string to only the lines of text which are numbersAutomatically doubling the value of...

How to format long polynomial?

What are these boxed doors outside store fronts in New York?

Is it legal for company to use my work email to pretend I still work there?

Why are electrically insulating heatsinks so rare? Is it just cost?

Theorems that impeded progress

What's the point of deactivating Num Lock on login screens?

Can I make popcorn with any corn?

How does one intimidate enemies without having the capacity for violence?

What would happen to a modern skyscraper if it rains micro blackholes?

a relationship between local compactness and closure

Can I ask the recruiters in my resume to put the reason why I am rejected?

Why is 150k or 200k jobs considered good when there's 300k+ births a month?

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

Adding span tags within wp_list_pages list items

Why is consensus so controversial in Britain?

Is it possible to do 50 km distance without any previous training?

"You are your self first supporter", a more proper way to say it

What typically incentivizes a professor to change jobs to a lower ranking university?

Hiring someone is unethical to Kantians because you're treating them as a means?

Finding the repeating unit of polymerisation given two constituent molecules

When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?

Why don't electron-positron collisions release infinite energy?

Is a conference paper whose proceedings will be published in IEEE Xplore counted as a publication?

Watching something be written to a file live with tail



How to prepend a string to only the lines of text which are numbers


Automatically 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






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







5















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 ).










share|improve this question

























  • What do you want to do with a line like 1st word 2nd word?

    – glenn jackman
    Mar 29 at 18:00











  • 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
    Mar 29 at 19:57




















5















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 ).










share|improve this question

























  • What do you want to do with a line like 1st word 2nd word?

    – glenn jackman
    Mar 29 at 18:00











  • 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
    Mar 29 at 19:57
















5












5








5








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 ).










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 9:21









Martin Thornton

2,56361830




2,56361830










asked Mar 29 at 3:24









EmmetEmmet

7,51522345




7,51522345













  • What do you want to do with a line like 1st word 2nd word?

    – glenn jackman
    Mar 29 at 18:00











  • 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
    Mar 29 at 19:57





















  • What do you want to do with a line like 1st word 2nd word?

    – glenn jackman
    Mar 29 at 18:00











  • 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
    Mar 29 at 19:57



















What do you want to do with a line like 1st word 2nd word?

– glenn jackman
Mar 29 at 18:00





What do you want to do with a line like 1st word 2nd word?

– glenn jackman
Mar 29 at 18:00













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
Mar 29 at 19:57







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
Mar 29 at 19:57












4 Answers
4






active

oldest

votes


















9














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





share|improve this answer





















  • 2





    I often use the "substitute if match" variant for things like this /^[[:digit:]]+$/ s/^/number-/'

    – steeldriver
    Mar 29 at 7:29











  • Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O

    – Emmet
    Mar 29 at 9:41



















4














One way using awk:



awk '/^[0-9]+$/{$0="number-"$0;}1'  file





share|improve this answer
























  • I also seek awk solution, thanks ! 1+

    – Emmet
    Mar 29 at 9:42



















3















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.






share|improve this answer
























  • Probably an overkill for this question, but still awesome ! +1

    – Sergiy Kolodyazhnyy
    Mar 29 at 12:06



















2














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





share|improve this answer
























  • Using if statement, nice ! Thank you :D

    – Emmet
    Mar 29 at 9:42














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
});


}
});














draft saved

draft discarded


















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









9














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





share|improve this answer





















  • 2





    I often use the "substitute if match" variant for things like this /^[[:digit:]]+$/ s/^/number-/'

    – steeldriver
    Mar 29 at 7:29











  • Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O

    – Emmet
    Mar 29 at 9:41
















9














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





share|improve this answer





















  • 2





    I often use the "substitute if match" variant for things like this /^[[:digit:]]+$/ s/^/number-/'

    – steeldriver
    Mar 29 at 7:29











  • Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O

    – Emmet
    Mar 29 at 9:41














9












9








9







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 29 at 7:07

























answered Mar 29 at 7:00









Sergiy KolodyazhnyySergiy Kolodyazhnyy

75k9155327




75k9155327








  • 2





    I often use the "substitute if match" variant for things like this /^[[:digit:]]+$/ s/^/number-/'

    – steeldriver
    Mar 29 at 7:29











  • Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O

    – Emmet
    Mar 29 at 9:41














  • 2





    I often use the "substitute if match" variant for things like this /^[[:digit:]]+$/ s/^/number-/'

    – steeldriver
    Mar 29 at 7:29











  • Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O

    – Emmet
    Mar 29 at 9:41








2




2





I often use the "substitute if match" variant for things like this /^[[:digit:]]+$/ s/^/number-/'

– steeldriver
Mar 29 at 7:29





I often use the "substitute if match" variant for things like this /^[[:digit:]]+$/ s/^/number-/'

– steeldriver
Mar 29 at 7:29













Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O

– Emmet
Mar 29 at 9:41





Thanks for the complete instruction, I'm a total noob when it comes to text processing :-O

– Emmet
Mar 29 at 9:41













4














One way using awk:



awk '/^[0-9]+$/{$0="number-"$0;}1'  file





share|improve this answer
























  • I also seek awk solution, thanks ! 1+

    – Emmet
    Mar 29 at 9:42
















4














One way using awk:



awk '/^[0-9]+$/{$0="number-"$0;}1'  file





share|improve this answer
























  • I also seek awk solution, thanks ! 1+

    – Emmet
    Mar 29 at 9:42














4












4








4







One way using awk:



awk '/^[0-9]+$/{$0="number-"$0;}1'  file





share|improve this answer













One way using awk:



awk '/^[0-9]+$/{$0="number-"$0;}1'  file






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 29 at 7:04









GuruGuru

52838




52838













  • I also seek awk solution, thanks ! 1+

    – Emmet
    Mar 29 at 9:42



















  • I also seek awk solution, thanks ! 1+

    – Emmet
    Mar 29 at 9:42

















I also seek awk solution, thanks ! 1+

– Emmet
Mar 29 at 9:42





I also seek awk solution, thanks ! 1+

– Emmet
Mar 29 at 9:42











3















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.






share|improve this answer
























  • Probably an overkill for this question, but still awesome ! +1

    – Sergiy Kolodyazhnyy
    Mar 29 at 12:06
















3















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.






share|improve this answer
























  • Probably an overkill for this question, but still awesome ! +1

    – Sergiy Kolodyazhnyy
    Mar 29 at 12:06














3












3








3








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.






share|improve this answer














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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 29 at 11:56









Chris WilliamsChris Williams

311




311













  • Probably an overkill for this question, but still awesome ! +1

    – Sergiy Kolodyazhnyy
    Mar 29 at 12:06



















  • Probably an overkill for this question, but still awesome ! +1

    – Sergiy Kolodyazhnyy
    Mar 29 at 12:06

















Probably an overkill for this question, but still awesome ! +1

– Sergiy Kolodyazhnyy
Mar 29 at 12:06





Probably an overkill for this question, but still awesome ! +1

– Sergiy Kolodyazhnyy
Mar 29 at 12:06











2














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





share|improve this answer
























  • Using if statement, nice ! Thank you :D

    – Emmet
    Mar 29 at 9:42


















2














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





share|improve this answer
























  • Using if statement, nice ! Thank you :D

    – Emmet
    Mar 29 at 9:42
















2












2








2







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 29 at 7:26









GoronGoron

1669




1669













  • Using if statement, nice ! Thank you :D

    – Emmet
    Mar 29 at 9:42





















  • Using if statement, nice ! Thank you :D

    – Emmet
    Mar 29 at 9:42



















Using if statement, nice ! Thank you :D

– Emmet
Mar 29 at 9:42







Using if statement, nice ! Thank you :D

– Emmet
Mar 29 at 9:42




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...