add numbers at the beginning of each line and replace asterisks with numbers in a document

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












Is there any way in Linux centos7 to add numbers to the lines of a document. Any method is ok, command, code, script or what ever. I have document and I want to number the lines.



input example



Only I can change my life. 
Good, better, best.
Life is 10% what happens to you and 90% how you react to it.


output



1 Only I can change my life. 
2 Good, better, best.
3 Life is 10% what happens to you and 90% how you react to it.


Another question
How can I change the asterisks "*" at the beginning of a text with numbers.



input



* Only I can change my life. 
* Good, better, best.
* Life is 10% what happens to you and 90% how you react to it.


output



1 Only I can change my life. 
2 Good, better, best.
3 Life is 10% what happens to you and 90% how you react to it.









share|improve this question









New contributor




marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Please focus on one question per Question, so that we can have good answers to each. Thank you!
    – Jeff Schaller
    47 mins ago










  • @Jeff Schaller I am so sorry! just I am new to U&L and still don't know the rules!
    – marco
    42 mins ago














up vote
3
down vote

favorite












Is there any way in Linux centos7 to add numbers to the lines of a document. Any method is ok, command, code, script or what ever. I have document and I want to number the lines.



input example



Only I can change my life. 
Good, better, best.
Life is 10% what happens to you and 90% how you react to it.


output



1 Only I can change my life. 
2 Good, better, best.
3 Life is 10% what happens to you and 90% how you react to it.


Another question
How can I change the asterisks "*" at the beginning of a text with numbers.



input



* Only I can change my life. 
* Good, better, best.
* Life is 10% what happens to you and 90% how you react to it.


output



1 Only I can change my life. 
2 Good, better, best.
3 Life is 10% what happens to you and 90% how you react to it.









share|improve this question









New contributor




marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Please focus on one question per Question, so that we can have good answers to each. Thank you!
    – Jeff Schaller
    47 mins ago










  • @Jeff Schaller I am so sorry! just I am new to U&L and still don't know the rules!
    – marco
    42 mins ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











Is there any way in Linux centos7 to add numbers to the lines of a document. Any method is ok, command, code, script or what ever. I have document and I want to number the lines.



input example



Only I can change my life. 
Good, better, best.
Life is 10% what happens to you and 90% how you react to it.


output



1 Only I can change my life. 
2 Good, better, best.
3 Life is 10% what happens to you and 90% how you react to it.


Another question
How can I change the asterisks "*" at the beginning of a text with numbers.



input



* Only I can change my life. 
* Good, better, best.
* Life is 10% what happens to you and 90% how you react to it.


output



1 Only I can change my life. 
2 Good, better, best.
3 Life is 10% what happens to you and 90% how you react to it.









share|improve this question









New contributor




marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Is there any way in Linux centos7 to add numbers to the lines of a document. Any method is ok, command, code, script or what ever. I have document and I want to number the lines.



input example



Only I can change my life. 
Good, better, best.
Life is 10% what happens to you and 90% how you react to it.


output



1 Only I can change my life. 
2 Good, better, best.
3 Life is 10% what happens to you and 90% how you react to it.


Another question
How can I change the asterisks "*" at the beginning of a text with numbers.



input



* Only I can change my life. 
* Good, better, best.
* Life is 10% what happens to you and 90% how you react to it.


output



1 Only I can change my life. 
2 Good, better, best.
3 Life is 10% what happens to you and 90% how you react to it.






text-processing






share|improve this question









New contributor




marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 19 mins ago









Jeff Schaller

33.4k850112




33.4k850112






New contributor




marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 49 mins ago









marco

853




853




New contributor




marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






marco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Please focus on one question per Question, so that we can have good answers to each. Thank you!
    – Jeff Schaller
    47 mins ago










  • @Jeff Schaller I am so sorry! just I am new to U&L and still don't know the rules!
    – marco
    42 mins ago
















  • Please focus on one question per Question, so that we can have good answers to each. Thank you!
    – Jeff Schaller
    47 mins ago










  • @Jeff Schaller I am so sorry! just I am new to U&L and still don't know the rules!
    – marco
    42 mins ago















Please focus on one question per Question, so that we can have good answers to each. Thank you!
– Jeff Schaller
47 mins ago




Please focus on one question per Question, so that we can have good answers to each. Thank you!
– Jeff Schaller
47 mins ago












@Jeff Schaller I am so sorry! just I am new to U&L and still don't know the rules!
– marco
42 mins ago




@Jeff Schaller I am so sorry! just I am new to U&L and still don't know the rules!
– marco
42 mins ago










2 Answers
2






active

oldest

votes

















up vote
6
down vote



accepted










To number every line, use nl:



nl -ba input


The flag means: use a body numbering style of all lines.



To number only non-blank lines, use:



nl -bt input


nl provides a variety of features for formatting the numbers; by default, it separates the numbers with a tab; for a single space, use -s' '. It also assumes a default column width for the numbers; if you don't want such padding space, use -w 1.



To replace leading characters with sed, see Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed, for example:



sed 's/^*//' input


... where the * has to be escaped because it's a regular expression token meaning zero-or-more of the previous item. While there is no previous item (it's an anchor meaning beginning-of-the-line), it's a better habit to escape it.






share|improve this answer






















  • @ Jeff Schaller thanks you very much, how can I replace the /*/ ?
    – marco
    40 mins ago










  • AFAIK, the * does not have to be escaped if it's the first character in a regular expression (or occurs straight after the ^ anchor).
    – Kusalananda
    20 mins ago







  • 1




    Agreed, @Kusalananda, though I had to test it to be sure -- but I'd rather encourage the habit of escape characters that you don't intend to be special.
    – Jeff Schaller
    19 mins ago

















up vote
5
down vote













To add numbers to a document:



cat -b file > output_file
-b, --number-nonblank number nonempty output lines, overrides -n


To write a file to standard output, with line numbers added instead of asterisks.



cat file | sed 's/*//' | nl > output_file





share|improve this answer


















  • 1




    thank you @goro this worked
    – marco
    39 mins ago










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






marco is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473487%2fadd-numbers-at-the-beginning-of-each-line-and-replace-asterisks-with-numbers-in%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote



accepted










To number every line, use nl:



nl -ba input


The flag means: use a body numbering style of all lines.



To number only non-blank lines, use:



nl -bt input


nl provides a variety of features for formatting the numbers; by default, it separates the numbers with a tab; for a single space, use -s' '. It also assumes a default column width for the numbers; if you don't want such padding space, use -w 1.



To replace leading characters with sed, see Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed, for example:



sed 's/^*//' input


... where the * has to be escaped because it's a regular expression token meaning zero-or-more of the previous item. While there is no previous item (it's an anchor meaning beginning-of-the-line), it's a better habit to escape it.






share|improve this answer






















  • @ Jeff Schaller thanks you very much, how can I replace the /*/ ?
    – marco
    40 mins ago










  • AFAIK, the * does not have to be escaped if it's the first character in a regular expression (or occurs straight after the ^ anchor).
    – Kusalananda
    20 mins ago







  • 1




    Agreed, @Kusalananda, though I had to test it to be sure -- but I'd rather encourage the habit of escape characters that you don't intend to be special.
    – Jeff Schaller
    19 mins ago














up vote
6
down vote



accepted










To number every line, use nl:



nl -ba input


The flag means: use a body numbering style of all lines.



To number only non-blank lines, use:



nl -bt input


nl provides a variety of features for formatting the numbers; by default, it separates the numbers with a tab; for a single space, use -s' '. It also assumes a default column width for the numbers; if you don't want such padding space, use -w 1.



To replace leading characters with sed, see Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed, for example:



sed 's/^*//' input


... where the * has to be escaped because it's a regular expression token meaning zero-or-more of the previous item. While there is no previous item (it's an anchor meaning beginning-of-the-line), it's a better habit to escape it.






share|improve this answer






















  • @ Jeff Schaller thanks you very much, how can I replace the /*/ ?
    – marco
    40 mins ago










  • AFAIK, the * does not have to be escaped if it's the first character in a regular expression (or occurs straight after the ^ anchor).
    – Kusalananda
    20 mins ago







  • 1




    Agreed, @Kusalananda, though I had to test it to be sure -- but I'd rather encourage the habit of escape characters that you don't intend to be special.
    – Jeff Schaller
    19 mins ago












up vote
6
down vote



accepted







up vote
6
down vote



accepted






To number every line, use nl:



nl -ba input


The flag means: use a body numbering style of all lines.



To number only non-blank lines, use:



nl -bt input


nl provides a variety of features for formatting the numbers; by default, it separates the numbers with a tab; for a single space, use -s' '. It also assumes a default column width for the numbers; if you don't want such padding space, use -w 1.



To replace leading characters with sed, see Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed, for example:



sed 's/^*//' input


... where the * has to be escaped because it's a regular expression token meaning zero-or-more of the previous item. While there is no previous item (it's an anchor meaning beginning-of-the-line), it's a better habit to escape it.






share|improve this answer














To number every line, use nl:



nl -ba input


The flag means: use a body numbering style of all lines.



To number only non-blank lines, use:



nl -bt input


nl provides a variety of features for formatting the numbers; by default, it separates the numbers with a tab; for a single space, use -s' '. It also assumes a default column width for the numbers; if you don't want such padding space, use -w 1.



To replace leading characters with sed, see Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed, for example:



sed 's/^*//' input


... where the * has to be escaped because it's a regular expression token meaning zero-or-more of the previous item. While there is no previous item (it's an anchor meaning beginning-of-the-line), it's a better habit to escape it.







share|improve this answer














share|improve this answer



share|improve this answer








edited 5 mins ago

























answered 45 mins ago









Jeff Schaller

33.4k850112




33.4k850112











  • @ Jeff Schaller thanks you very much, how can I replace the /*/ ?
    – marco
    40 mins ago










  • AFAIK, the * does not have to be escaped if it's the first character in a regular expression (or occurs straight after the ^ anchor).
    – Kusalananda
    20 mins ago







  • 1




    Agreed, @Kusalananda, though I had to test it to be sure -- but I'd rather encourage the habit of escape characters that you don't intend to be special.
    – Jeff Schaller
    19 mins ago
















  • @ Jeff Schaller thanks you very much, how can I replace the /*/ ?
    – marco
    40 mins ago










  • AFAIK, the * does not have to be escaped if it's the first character in a regular expression (or occurs straight after the ^ anchor).
    – Kusalananda
    20 mins ago







  • 1




    Agreed, @Kusalananda, though I had to test it to be sure -- but I'd rather encourage the habit of escape characters that you don't intend to be special.
    – Jeff Schaller
    19 mins ago















@ Jeff Schaller thanks you very much, how can I replace the /*/ ?
– marco
40 mins ago




@ Jeff Schaller thanks you very much, how can I replace the /*/ ?
– marco
40 mins ago












AFAIK, the * does not have to be escaped if it's the first character in a regular expression (or occurs straight after the ^ anchor).
– Kusalananda
20 mins ago





AFAIK, the * does not have to be escaped if it's the first character in a regular expression (or occurs straight after the ^ anchor).
– Kusalananda
20 mins ago





1




1




Agreed, @Kusalananda, though I had to test it to be sure -- but I'd rather encourage the habit of escape characters that you don't intend to be special.
– Jeff Schaller
19 mins ago




Agreed, @Kusalananda, though I had to test it to be sure -- but I'd rather encourage the habit of escape characters that you don't intend to be special.
– Jeff Schaller
19 mins ago












up vote
5
down vote













To add numbers to a document:



cat -b file > output_file
-b, --number-nonblank number nonempty output lines, overrides -n


To write a file to standard output, with line numbers added instead of asterisks.



cat file | sed 's/*//' | nl > output_file





share|improve this answer


















  • 1




    thank you @goro this worked
    – marco
    39 mins ago














up vote
5
down vote













To add numbers to a document:



cat -b file > output_file
-b, --number-nonblank number nonempty output lines, overrides -n


To write a file to standard output, with line numbers added instead of asterisks.



cat file | sed 's/*//' | nl > output_file





share|improve this answer


















  • 1




    thank you @goro this worked
    – marco
    39 mins ago












up vote
5
down vote










up vote
5
down vote









To add numbers to a document:



cat -b file > output_file
-b, --number-nonblank number nonempty output lines, overrides -n


To write a file to standard output, with line numbers added instead of asterisks.



cat file | sed 's/*//' | nl > output_file





share|improve this answer














To add numbers to a document:



cat -b file > output_file
-b, --number-nonblank number nonempty output lines, overrides -n


To write a file to standard output, with line numbers added instead of asterisks.



cat file | sed 's/*//' | nl > output_file






share|improve this answer














share|improve this answer



share|improve this answer








edited 43 mins ago









ilkkachu

52.5k679145




52.5k679145










answered 45 mins ago









Goro

6,59552865




6,59552865







  • 1




    thank you @goro this worked
    – marco
    39 mins ago












  • 1




    thank you @goro this worked
    – marco
    39 mins ago







1




1




thank you @goro this worked
– marco
39 mins ago




thank you @goro this worked
– marco
39 mins ago










marco is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















marco is a new contributor. Be nice, and check out our Code of Conduct.












marco is a new contributor. Be nice, and check out our Code of Conduct.











marco is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473487%2fadd-numbers-at-the-beginning-of-each-line-and-replace-asterisks-with-numbers-in%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery