add numbers at the beginning of each line and replace asterisks with numbers in a document
Clash 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.
text-processing
New contributor
add a comment |Â
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.
text-processing
New contributor
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
add a comment |Â
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.
text-processing
New contributor
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
text-processing
New contributor
New contributor
edited 19 mins ago
Jeff Schaller
33.4k850112
33.4k850112
New contributor
asked 49 mins ago
marco
853
853
New contributor
New contributor
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
add a comment |Â
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
add a comment |Â
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 b
ody numbering style of a
ll 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.
@ 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
add a comment |Â
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
1
thank you @goro this worked
â marco
39 mins ago
add a comment |Â
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 b
ody numbering style of a
ll 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.
@ 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
add a comment |Â
up vote
6
down vote
accepted
To number every line, use nl:
nl -ba input
The flag means: use a b
ody numbering style of a
ll 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.
@ 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
add a comment |Â
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 b
ody numbering style of a
ll 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.
To number every line, use nl:
nl -ba input
The flag means: use a b
ody numbering style of a
ll 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.
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
add a comment |Â
@ 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
add a comment |Â
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
1
thank you @goro this worked
â marco
39 mins ago
add a comment |Â
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
1
thank you @goro this worked
â marco
39 mins ago
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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.
marco is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
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
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
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
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