- Linux/Unix: Cat Command Display Line Numbers
- Syntax For Cat Command To Display Line Numbers
- Displaying line number using cat command
- Removing empty output while showing line numbers
- Say hello to nl command
- Examples
- A note about sed
- Summing up
- Linux set and unset
- Syntax
- Options: Bourne Shell (sh)
- Options: C Shell (csh)
- Options: K Shell (ksh)
- Examples
- Related commands
Linux/Unix: Cat Command Display Line Numbers
H ow do I use the cat command to display the line numbers for a file called myapp.c under Linux or Unix like operating systems?
You can use the cat command concatenate files and show on the screen under Linux or Unix like operating systems.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | cat command on Linux or Unix |
Est. reading time | 2 mintues |
The cat command also number all output lines starting with number one with the following syntax:
Syntax For Cat Command To Display Line Numbers
The syntax is as follows:
cat -n fileNameHere
cat —number foo.c
We can use the more command/less command as filter when text can not be fitted on the screen:
cat —number foo.c | more
cat —number foo.c | less
Displaying line number using cat command
The -b / —number-nonblank option number all nonempty output lines, starting with one and the syntax is:
cat -b fileNameHere
OR
cat —number—nonblank filename
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Removing empty output while showing line numbers
Finally, you suppress or remove repeated empty output lines with the -s / —squeeze-blank option:
cat -s -n fileNameHere
cat -s -n /etc/resolv.conf
Sample outputs:
OR (GNU only syntax)
cat —squeeze-blank -n filename
Say hello to nl command
Use the nl command number lines of files under Linux or Unix oses. The syntax is:
Examples
Create a text file called hello.c as follows:
Use the cat or nl command to display line numbers:
Fig.01: Displaying line numbers for hello.c using the cat and nl command.
A note about sed
To just print 3rd line use the sed command:
sed -n 3p /etc/resolv.conf
To just print 3rd and 5th line use the sed command:
sed -n -e 3p -e 5p /etc/resolv.conf
To see specific range, say show lines between 3 to 5, run:
sed -n 3,5p /etc/resolv.conf
Summing up
You learned various commands that can display line numbers on Linux or Unix-like systems. See the following man page:
man cat
man nl
man sed
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Hi Thanks for sharing … Its of great help …
And I m a great fan of your … because I found 90% of what i think on cyberciti.
Just a little correction here in this post ..
“cat –number–nonblank filename ”
is giving error
cat: unrecognized option ‘–number–nonblank’
Try ‘cat –help’ for more information.
Just make it
“cat –number-nonblank filename”
a single hyphen – after number
But we had a trouble. “cat” command does not stop display when the text is full screen.
So, we have any other option to resolve this in-convenience.
The standard output could instead be redirected using a pipe (represented by a vertical bar) to a filter (i.e., a program that transforms data in some meaningful way) for further processing. For example, if the file is too large for all of the text to fit on the monitor screen simultaneously, as is frequently the case, the text will scroll down the screen at high speed and be very difficult to read. This problem is easily solved by piping the output to the filter less, i.e.,
cat file1 | less
This allows the user to advance the contents of the file one screenful at a time by pressing the space bar and to move backwards by pressing the b key. The user can exit from less by pressing the q key.
Источник
Linux set and unset
On Unix-like operating systems, the set command is a built-in function of the Bourne shell (sh), C shell (csh), and Korn shell (ksh), which is used to define and determine the values of the system environment.
Syntax
Bourne shell (sh):
Options: Bourne Shell (sh)
In sh, the set built-in command has the following options:
— | An option of a double-dash («—«) signifies the end of an option list. This option is primarily useful when values listed after the options start with a dash themselves. |
-a | Mark variables that are modified or created for «export»; environment variables set in this way will be passed on to the environments of any subsequent commands. |
-e | Exit immediately if a command exits with a non-zero exit status. |
-f | Disable file name generation (globbing). |
-h | Locate and remember function commands as functions are defined (function commands are normally located when the function is executed). |
-k | All keyword arguments are placed in the environment for a command, not only those that precede the command name. |
-n | Read commands but do not execute them. |
-t | Exit after reading and executing one command. |
-u | Treat unset variables as an error when substituting. |
-v | Print shell input lines as they are read. |
-x | Print commands and their arguments as they are executed. |
Using + rather than — causes these flags to be turned off. These flags can also be used upon invocation of the shell itself. The current set of flags are found in the variable $-. The remaining arguments are positional parameters and are assigned, in order, to $1, $2, etc. If no arguments are given the values of all names are printed.
For each name, the unset command removes the corresponding variable or function value. The special variables PATH, PS1, PS2, MAILCHECK, and IF cannot be unset.
With the export built-in command, the given names are marked for automatic export to the environment of subsequently executed commands. If no arguments are given, variable names that are marked for export during the current shell’s execution are listed. Function names are not exported.
Options: C Shell (csh)
In csh, If no arguments are specified, set displays the values of all shell variables. Multiword values are displayed as a parenthesized list. With the var argument alone, set assigns an empty (null) value to the variable var. With arguments of the form var = value, set assigns value to var, where value is one of:
word | A single word (or quoted string). |
(wordlist) | A space-separated list of words enclosed in parentheses. |
Values are command and file name expanded before being assigned. The form set var[n]=word replaces the n‘th word in a multiword value with word.
unset removes variables whose names match (using file name substitution) pattern. All variables are removed by «unset *«; this is a very bad idea if you don’t know what you’re doing, though.
Options: K Shell (ksh)
In ksh, the set command takes the following options:
-A | Array assignment. Unset the variable name and assign values sequentially from the list arg. If +A is used, the variable name is not unset first. | ||||||||||||||||||||||||||||||||||||||||
-a | All subsequent variables that are defined are automatically exported. | ||||||||||||||||||||||||||||||||||||||||
-e | If a command has a non-zero exit status, execute the ERR trap (if set), and exit. This mode is disabled while reading profiles. | ||||||||||||||||||||||||||||||||||||||||
-f | Disables file name generation (globbing). | ||||||||||||||||||||||||||||||||||||||||
-h | Each command becomes a tracked alias when first encountered. | ||||||||||||||||||||||||||||||||||||||||
-k | All variable assignment arguments are placed in the environment for a command, not only those preceding the command name. | ||||||||||||||||||||||||||||||||||||||||
-m | Background jobs run in a separate process group and a line prints upon completion. The exit status of background jobs is reported in a completion message. On systems with job control, this flag is turned on automatically for interactive shells. | ||||||||||||||||||||||||||||||||||||||||
-n | Read commands and check them for syntax errors, but do not execute them. Ignored for interactive shells. | ||||||||||||||||||||||||||||||||||||||||
-o option | The option argument can be one of the following:
If no option name is supplied then the current option settings are printed. | ||||||||||||||||||||||||||||||||||||||||
-p | Disables processing of the $HOME/.profile file and uses the file /etc/suid_profile instead of the ENV file. This mode is on whenever the effective uid (user ID) is not equal to the real uid, or when the effective gid (group ID) is not equal to the real gid. Turning this off causes the effective uid and gid to be set to the real uid and gid. | ||||||||||||||||||||||||||||||||||||||||
-s | Sort the positional parameters lexicographically. | ||||||||||||||||||||||||||||||||||||||||
-t | Exit after reading and executing one command. | ||||||||||||||||||||||||||||||||||||||||
-u | Treat unset parameters as an error when substituting. | ||||||||||||||||||||||||||||||||||||||||
-v | Print shell input lines as they are read. | ||||||||||||||||||||||||||||||||||||||||
-x | Print commands and their arguments as they are executed. | ||||||||||||||||||||||||||||||||||||||||
— | Turns off -x and -v flags and stops examining arguments for flags. | ||||||||||||||||||||||||||||||||||||||||
— | Do not change any of the flags; useful in setting $1 to a value beginning with —. If no arguments follow this flag, then the positional parameters are unset. |
Using + rather than — causes these flags to be turned off. These flags can also be used upon invocation of the shell. The current set of flags are found in $-. Unless -A is specified, the remaining arguments are positional parameters and are assigned, in order, to «$1 $2 . «. If no arguments are given, then the names and values of all variables are printed on the standard output.
The variables given by the list of names are automatically unassigned: their values and attributes are erased. Readonly variables cannot be unset. If the -f flag is set, then the names refer to function names. Unsetting ERRNO, LINENO, MAILCHECK, OPTARG, OPTIND, RANDOM, SECONDS, TMOUT, and _ removes their special meaning even if they are subsequently assigned.
When using unset, the variables given by the list of names are similarly unassigned: their values and attributes are erased. Readonly variables cannot be unset. If the -f, flag is set, then the names refer to function names. Unsetting ERRNO, LINENO, MAILCHECK, OPTARG, OPTIND, RANDOM, SECONDS, TMOUT, and _ removes their special meaning even if they are subsequently assigned.
With the export built-in command, the given names are marked for automatic export to the environment of subsequently-executed commands. ksh commands that are preceded by one or two «*» characters (asterisks) are treated specially in the following ways:
- Variable assignment lists preceding the command remain in effect when the command completes.
- I/Oredirections are processed after variable assignments.
- Errors cause a script containing them to abort.
- Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This assignment means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed.
Examples
In csh, this command sets the environment variable PATH, such that the shell searches for files in the /bin, /usr/bin, /usr/sbin and /usr/local/bin directories, in that order.
Related commands
csh — The C shell command interpreter.
ksh — The Korn shell command interpreter.
setenv — Set the value of an environment variable.
sh — The Bourne shell command interpreter.
Источник