Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

1 votes
4 answers
520 views
How do I print line numbers but reset line counter at empty line?
I have a file.txt containing: ``` this is the first second line not last line fourth but first second in list seventh in file seventh with nl ``` Normally I would just `cat` and pipe `|` it into `nl` like so: ```bash $> cat file.txt | nl 1 this is the first 2 second line 3 not last line 4 fourth but...
I have a file.txt containing:
this is the first
second line
not last line

fourth but first
second in list
seventh in file
seventh with nl
Normally I would just cat and pipe | it into nl like so:
$> cat file.txt | nl
1  this is the first
2  second line
3  not last line

4  fourth but first
5  second in list
6  seventh in file
7  seventh with nl
But I need the line numbers to reset when it encounters an empty line like so:
$> alias_or_function file.txt
1  this is the first
2  second line
3  not last line

1  fourth but first
2  second in list
3  seventh in file
4  seventh with nl
How could I do this using a quick function or alias in my ~/.zshrc?
ntruter42 (197 rep)
Aug 22, 2020, 04:17 AM • Last activity: Mar 3, 2025, 07:36 PM
9 votes
5 answers
1562 views
nl: How to start line numbers on the second line?
This is the standard output of `ls -ln | nl` wolf@linux:~$ ls -lh | nl 1 total 24 2 -rw-rw-r-- 1 wolf wolf 186 Sep 24 22:18 01.py 3 -rw-rw-r-- 1 wolf wolf 585 Sep 24 22:21 02.py 4 -rw-rw-r-- 1 wolf wolf 933 Sep 24 22:26 03.py wolf@linux:~$ Instead of starting the number from `total 24`, would it be...
This is the standard output of ls -ln | nl wolf@linux:~$ ls -lh | nl 1 total 24 2 -rw-rw-r-- 1 wolf wolf 186 Sep 24 22:18 01.py 3 -rw-rw-r-- 1 wolf wolf 585 Sep 24 22:21 02.py 4 -rw-rw-r-- 1 wolf wolf 933 Sep 24 22:26 03.py wolf@linux:~$ Instead of starting the number from total 24, would it be possible to start it from the actual files/directory which is the second line? Desired output wolf@linux:~$ ls -lh | nl total 24 1 -rw-rw-r-- 1 wolf wolf 186 Sep 24 22:18 01.py 2 -rw-rw-r-- 1 wolf wolf 585 Sep 24 22:21 02.py 3 -rw-rw-r-- 1 wolf wolf 933 Sep 24 22:26 03.py wolf@linux:~$
Wolf (1741 rep)
Sep 25, 2020, 11:29 AM • Last activity: Feb 11, 2025, 07:06 PM
1 votes
2 answers
469 views
head not returning n lines
When I run the command `head -n 445 /etc/snort/snort.conf | nl` I expect lines 1-445 to be returned. However, only up to line 371 is returned: [snip] 370 preprocessor dcerpc2_server: default, policy WinXP, \ 371 detect [smb [139, 445], tcp 35, udp 135, rpc-over-http-server 593], \ What is happening?
When I run the command head -n 445 /etc/snort/snort.conf | nl I expect lines 1-445 to be returned. However, only up to line 371 is returned: [snip] 370 preprocessor dcerpc2_server: default, policy WinXP, \ 371 detect [smb [139, 445], tcp 35, udp 135, rpc-over-http-server 593], \ What is happening?
Syd (13 rep)
Nov 17, 2019, 05:20 PM • Last activity: Aug 19, 2023, 07:18 PM
1 votes
2 answers
957 views
nl without additional leading tab/space
I notice that there is additional leading tab/space whenever `nl` is used. wolf@linux:~$ echo ABC ABC wolf@linux:~$ wolf@linux:~$ echo ABC | nl 1 ABC wolf@linux:~$ Is there any option to disable this? Desired Output wolf@linux:~$ echo ABC | nl 1 ABC wolf@linux:~$
I notice that there is additional leading tab/space whenever nl is used. wolf@linux:~$ echo ABC ABC wolf@linux:~$ wolf@linux:~$ echo ABC | nl 1 ABC wolf@linux:~$ Is there any option to disable this? Desired Output wolf@linux:~$ echo ABC | nl 1 ABC wolf@linux:~$
Wolf (1741 rep)
Nov 16, 2020, 12:55 PM • Last activity: Nov 16, 2020, 12:59 PM
-1 votes
1 answers
67 views
least astonishment transgressed by `nl` on Darwin
Transgression of [least astonishment][0]: ``` bash-5.0$ (for i in {1..1000010}; do echo $i; done) | nl | tail -24 999987 999987 999988 999988 999989 999989 999990 999990 999991 999991 999992 999992 999993 999993 999994 999994 999995 999995 999996 999996 999997 999997 999998 999998 999999 999999 0000...
Transgression of least astonishment :
bash-5.0$ (for i in {1..1000010}; do echo $i; done) | nl | tail -24
999987	999987
999988	999988
999989	999989
999990	999990
999991	999991
999992	999992
999993	999993
999994	999994
999995	999995
999996	999996
999997	999997
999998	999998
999999	999999
000000	1000000
000001	1000001
000002	1000002
000003	1000003
000004	1000004
000005	1000005
000006	1000006
000007	1000007
000008	1000008
000009	1000009
000010	1000010
bash-5.0$ uname -a
Darwin llc.local 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 20
20; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64        
bash-5.0$ sw_vers 
ProductName:	Mac OS X
ProductVersion:	10.15.4
[...]
bash-5.0$ _
Compare with expectation:
admin@ip-w-x-y-z:~$ (for i in {1..1000010}; do echo $i; done) | nl | tail -24
999987	999987
999988	999988
999989	999989
999990	999990
999991	999991
999992	999992
999993	999993
999994	999994
999995	999995
999996	999996
999997	999997
999998	999998
999999	999999
1000000	1000000
1000001	1000001
1000002	1000002
1000003	1000003
1000004	1000004
1000005	1000005
1000006	1000006
1000007	1000007
1000008	1000008
1000009	1000009
1000010	1000010
admin@ip-w-x-y-z:~$ uname -a
Linux ip-w-x-y-z 4.9.0-9-amd64 #1 SMP Debian 4.9.168-1+deb9u4 (2019-07-19)
 x86_64 GNU/Linux
admin@ip-w-x-y-z:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 9.9 (stretch)
Release:	9.9
Codename:	stretch
admin@ip-w-x-y-z:~$ _
I'm curious what are your thoughts about this and how to overcome. Thanks.
41754 (95 rep)
Apr 7, 2020, 07:54 AM • Last activity: Sep 23, 2020, 03:51 PM
1 votes
2 answers
388 views
`nl` invocation soaking all input before numbering
How can this be done? I don't see any applicable option in the manual. I have positively checked that indentation breaks after ten million lines. You can check it like: $ (for i in `seq 0 10000000`; do echo "$i"; done) | nl I don't often generate so many lines, but I don't want it to break like it d...
How can this be done? I don't see any applicable option in the manual. I have positively checked that indentation breaks after ten million lines. You can check it like: $ (for i in seq 0 10000000; do echo "$i"; done) | nl I don't often generate so many lines, but I don't want it to break like it does. How can this be done?
41754 (95 rep)
Jan 14, 2015, 10:53 AM • Last activity: Sep 23, 2020, 03:51 PM
2 votes
3 answers
157 views
how to align hypen
This is my command ``` echo -e "Test sur la vie, l'univers et le reste \n " >test42 |grep '42' questions.txt | nl -w1 -s ' - ' | cut -d "/" -f 1 >> test42 ``` this is the output ``` Test sur la vie, l'univers et le reste 1 - Quel est le numéro de la dernière armure de Tony Stark dans le fi...
This is my command
echo -e  "Test sur la vie, l'univers et le reste \n " >test42 |grep '42' questions.txt | nl -w1  -s '  - ' | cut -d "/" -f 1 >> test42
this is the output
Test sur la vie, l'univers et le reste 
 
1  - Quel est le numéro de la dernière armure de Tony Stark dans le film Iron Man 3 ?
2  - Quel est l’angle visuel entre la partie colorée d’un arc-en-ciel et le point central de son arc ?
3  - Quel est le score maximal réalisable à titre individuel au cours des Olympiades internationales de mathématiques ?
4  - À combien de gallons un baril de pétrole est-il équivalent ?
5  - Quel est le nombre d’années de mariage des noces de nacre ?
6  - Quelle est la résistance mécanique en GPa du graphène ?
7  - Combien d'éclipses de soleil et de lune durant un saros ?
8  - Quel est le numéro de l’inode du répertoire root dans un système de fichier reiser4 ?
9  - Combien la Bible de Gutenberg comporte de lignes de texte par colonne ?
10  - Quel est le titre de l'épisode 3X07 de la série télévisée Doctor Who ?
11  - Quel est le numéro atomique du molybdène, un métal de transition ?
But the result should be like this
Test sur la vie, l'univers et le reste

1  - Quel est le numéro de la dernière armure de Tony Stark dans le film Iron Man 3 ?
2  - Quel est l’angle visuel entre la partie colorée d’un arc-en-ciel et le point central de son arc ?
3  - Quel est le score maximal réalisable à titre individuel au cours des Olympiades internationales de mathématiques ?
4  - À combien de gallons un baril de pétrole est-il équivalent ?
5  - Quel est le nombre d’années de mariage des noces de nacre ?
6  - Quelle est la résistance mécanique en GPa du graphène ?
7  - Combien d'éclipses de soleil et de lune durant un saros ?
8  - Quel est le numéro de l’inode du répertoire root dans un système de fichier reiser4 ?
9  - Combien la Bible de Gutenberg comporte de lignes de texte par colonne ?
10 - Quel est le titre de l'épisode 3X07 de la série télévisée Doctor Who ?
11 - Quel est le numéro atomique du molybdène, un métal de transition ?
12 - Quelle est la taille, en mètre, de l'obélisque inachevé dans la carrière d'Assouan en Égypte ?
I need to have 2 spaces between the number 1 to 9 and the hypen. But i need to have only 1 space between the hypen and number 10 and more. Do you guys have any idea ? Thanks
david b.k (39 rep)
Feb 19, 2020, 09:48 PM • Last activity: Feb 19, 2020, 11:26 PM
2 votes
3 answers
6884 views
Line Number for all non-blank lines?
Kind of amused here, I'm trying to pass the [prereqs](https://classroom.udacity.com/courses/ud219/lessons/972928869/concepts/9977987120923) to get into the [Udacity Course on High Performance Computer Architecture by Georgia Institute of Technology](https://www.udacity.com/course/high-performance-co...
Kind of amused here, I'm trying to pass the [prereqs](https://classroom.udacity.com/courses/ud219/lessons/972928869/concepts/9977987120923) to get into the [Udacity Course on High Performance Computer Architecture by Georgia Institute of Technology](https://www.udacity.com/course/high-performance-computer-architecture--ud007) For the life of me I can't figure out what they want, Question reads, > To list the contents of a file, with line numbers for non-blank line shown use the command with an option: ________ ___ filename. I've tried nl. It didn't work. Most bizarre question ever. Question
Evan Carroll (34663 rep)
Apr 17, 2018, 11:11 PM • Last activity: Feb 8, 2020, 05:10 PM
1 votes
1 answers
335 views
What is considered a header and footer in a text formatted for printing?
From `nl` manual: ... -f, --footer-numbering=STYLE use STYLE for numbering footer lines -h, --header-numbering=STYLE use STYLE for numbering header lines ... Can you provide an example of usage of the `-f` and `-h` option? What is considered as being a footer or header?
From nl manual: ... -f, --footer-numbering=STYLE use STYLE for numbering footer lines -h, --header-numbering=STYLE use STYLE for numbering header lines ... Can you provide an example of usage of the -f and -h option? What is considered as being a footer or header?
user123456 (5258 rep)
Jul 28, 2019, 10:03 AM • Last activity: Jul 28, 2019, 10:44 AM
4 votes
4 answers
1782 views
Is there an alphabetic equivalent of nl or seq?
I have a file where I'd like to number the lines using "the alphabet" (simple ascii a, b, c, etc.) instead of numbers. So where I could do: nl somefile I'd like to do something like: abc somefile and get output like: ``` a line 1 of file b line 2 of file c line 3 of file ... ``` Even an alphabetic e...
I have a file where I'd like to number the lines using "the alphabet" (simple ascii a, b, c, etc.) instead of numbers. So where I could do: nl somefile I'd like to do something like: abc somefile and get output like:
a line 1 of file
b line 2 of file
c line 3 of file
...
Even an alphabetic equivalent of seq could help -- I could use paste to prefix lines. To keep things simple, my file would have 26 or less lines (so abc would not need to wrap letters like ...x, y, z, aa, ab, ac... kind of thing.)
Mike (153 rep)
Jun 15, 2019, 12:42 PM • Last activity: Jun 16, 2019, 01:55 PM
0 votes
1 answers
874 views
"tail" Is Returning the Wrong Requested Number Lines
I'm trying to use the following approach to subset the output of a manual: man dig | nl | tail -n +389 | head -n 6 However, the output starts at line 304, not line 389. Doing some research, it seems lines marked as "#####################" are not counted. This is very aggravating, and one of my curr...
I'm trying to use the following approach to subset the output of a manual: man dig | nl | tail -n +389 | head -n 6 However, the output starts at line 304, not line 389. Doing some research, it seems lines marked as "#####################" are not counted. This is very aggravating, and one of my current books was using this approach to subset number lines. Is there any solution to fix the overlooked lines?
George Thompson (3 rep)
Jun 6, 2019, 08:49 AM • Last activity: Jun 6, 2019, 08:52 AM
1 votes
1 answers
598 views
Is it possible to run head & use nl to number the lines?
When i ran `head file.txt && nl file.txt` it did each command in order of occurance (which makes sense). Is it possible to have the head display with numbered lines, so that this: word word word would become this: 1 word 2 word 3 word
When i ran head file.txt && nl file.txt it did each command in order of occurance (which makes sense). Is it possible to have the head display with numbered lines, so that this: word word word would become this: 1 word 2 word 3 word
Jonathon (13 rep)
Jan 19, 2019, 06:57 AM • Last activity: Jan 19, 2019, 08:47 AM
2 votes
2 answers
284 views
How to prepend numerical order on filenames by Zsh/AWK/...?
I would like to prepend numerical order form 1-777 on the orders given to it. You can loop it through and increment variable in Bash. However, I would like to do it with Zsh and/or Awk. An example follows. Filenames IMG_20161215_081850.jpg IMG_20161215_081851.jpg IMG_20161215_090559.jpg IMG_20161215...
I would like to prepend numerical order form 1-777 on the orders given to it. You can loop it through and increment variable in Bash. However, I would like to do it with Zsh and/or Awk. An example follows. Filenames IMG_20161215_081850.jpg IMG_20161215_081851.jpg IMG_20161215_090559.jpg IMG_20161215_090601.jpg IMG_20161215_091308.jpg Wanted 1.IMG_20161215_081850.jpg 2.IMG_20161215_081851.jpg 3.IMG_20161215_090559.jpg 4.IMG_20161215_090601.jpg 5.IMG_20161215_091308.jpg OS: Debian 8.5
Léo Léopold Hertz 준영 (7138 rep)
Dec 23, 2016, 04:44 PM • Last activity: Dec 30, 2016, 01:37 PM
9 votes
4 answers
3822 views
How to increment line counter for line beginning replacements by AWK/...?
I first thought about SED (`sed "s/^/COUNTER \&/" /tmp/1.tex`) but it is designed for a single line, and I cannot increment the counter itself by sed so thinking now `awk` because I have great experiences with `gawk` in integrated approaches. Data What & South Dragon & North Dragon & 5 \\ \hline Wha...
I first thought about SED (sed "s/^/COUNTER \&/" /tmp/1.tex) but it is designed for a single line, and I cannot increment the counter itself by sed so thinking now awk because I have great experiences with gawk in integrated approaches. Data What & South Dragon & North Dragon & 5 \\ \hline What & South Dragon & North Dragon & 5 \\ \hline What & South Dragon & North Dragon & 5 \\ \hline Expected output 1 & What & South Dragon & North Dragon & 5 \\ \hline 2 & What & South Dragon & North Dragon & 5 \\ \hline 3 & What & South Dragon & North Dragon & 5 \\ \hline OS: Debian 8.5
Léo Léopold Hertz 준영 (7138 rep)
Dec 27, 2016, 07:59 PM • Last activity: Dec 30, 2016, 12:57 PM
1 votes
1 answers
696 views
nl - numbering input in descending order
Is this possible to number file lines in descending order? That would be something like: nl -i -1 unfortunately this gives me: nl: invalid line number increment: ‘-1’: Numerical result out of range I know that it has no sense in pipe redirection, as `nl` can't guess number of total lines, but if I p...
Is this possible to number file lines in descending order? That would be something like: nl -i -1 unfortunately this gives me: nl: invalid line number increment: ‘-1’: Numerical result out of range I know that it has no sense in pipe redirection, as nl can't guess number of total lines, but if I pass file as an argument it should be quite easy - even it it needs two passes to accomplish the task. If not nl than maybe some other command.
sZpak (511 rep)
Nov 13, 2016, 06:17 PM • Last activity: Nov 13, 2016, 06:22 PM
0 votes
1 answers
267 views
nl add numbers suffix if regex match?
If I have a text file saying this: Xgrep () { something ; } Xgrep Xgrep Xgrep Xgrep Xgrep I want it to look like this Xgrep () { something ; } Xgrep 12 Xgrep 13 Xgrep 14 Xgrep 15 Xgrep 16 Meaning I want to choose at which number to begin, with suffixes instead of prefixes with one space between (`-s...
If I have a text file saying this: Xgrep () { something ; } Xgrep Xgrep Xgrep Xgrep Xgrep I want it to look like this Xgrep () { something ; } Xgrep 12 Xgrep 13 Xgrep 14 Xgrep 15 Xgrep 16 Meaning I want to choose at which number to begin, with suffixes instead of prefixes with one space between (-s " " for a space between). I thought that I could use nl for this, but reading the man page it doesn't seem so. I tried this: nl -b pexpr=Xgrep -s " " ~/desktop/test.txt However this just out put this: Xgrep Xgrep Xgrep Xgrep Xgrep Which is probably because the regex syntax is wrong but I couldn't find a way for it to match, all other ways it just listed the usage. [This website] is pretty much exactly what I am trying to do, but in command line form and only with numbers suffix.
DisplayName (12016 rep)
Oct 3, 2015, 03:55 PM • Last activity: Oct 3, 2015, 04:12 PM
2 votes
1 answers
2276 views
How to add a line number follow by a tab to a text file
I tried using nl -ba -s '\t' full_media > full_media2 But it really did not add the tab, but it added actually the "\t" text after the line number. The file im editing is a CSV file and here by i'm trying to add a new column to the beginning with id.
I tried using nl -ba -s '\t' full_media > full_media2 But it really did not add the tab, but it added actually the "\t" text after the line number. The file im editing is a CSV file and here by i'm trying to add a new column to the beginning with id.
dinesh707 (131 rep)
Feb 9, 2015, 09:14 AM • Last activity: Feb 9, 2015, 09:33 AM
Showing page 1 of 17 total questions