Linux cat first n lines

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: Display First Ten Lines (1-10) With Numbers Command

    [donotprint]
    Tutorial details
    Difficulty level Easy
    Root privileges No
    Requirements None
    Est. reading time 1m

    [/donotprint]

  1. sed command
  2. cat command
  3. less command
  4. awk command
  5. mapfile command (Bash v4.x only)

To display 1 to 10 lines from /etc/passwd file and number all output lines, enter:

  • 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

Fig.01: Linux/Unix: Bash/KSH SHELL Print nth Line Of a Text File

The following example use the head command to display first 10 lines of /etc/passwd file:

Источник

How to Use Linux Cat Command (With Examples)

Home » SysAdmin » How to Use Linux Cat Command (With Examples)

If you have worked in Linux, you surely have seen a code snippet that uses the cat command. Cat is short for concatenate. This command displays the contents of one or more files without having to open the file for editing.

In this article, learn how to use the cat command in Linux.

  • A system running Linux
  • Access to a terminal window / command line

cat Command Syntax

To use the cat command, follow the format:

[options] – This lets you issue additional instructions to the cat command. For example, to display the contents of a file with each line numbered, use the –n option:

filename(s) – Specify the name of the file (or files) that you want to display. If you use more than one filename, each file will be displayed.

Linux Cat Command Examples

This article includes 15 cat commands and examples of how to use them. To try out the commands, create a couple of sample files, and test the cat commands listed below.

1. Create a New File

You can create new files and add content to them using the cat command.

Create test1.txt and test2.txt, which you can use as sample files to test out the other commands.

1. Open a terminal window and create the first file:

2. The cursor moves to a new line where you can add the wanted text. Type a simple sentence such as:

3. To exit the prompt and write the changes to the file, hold the Ctrl key and press d.

4. Repeat the process to create test2.txt. Run:

6. Press Ctrl+d.

2. Display Contents of a Single File

To display the contents of test1.txt using the cat command run:

The output displays the content as in the image below.

3. Display Contents of Multiple Files

To display the contents of both files, run the command:

4. Redirect Contents of a Single File

Instead of displaying the contents of a file on the screen, cat can put them in a file.

If the destination filename doesn’t exist, it will be created. If you run cat on test3.txt, you should see the contents from test1.txt:

The output displays:

If a file is exported that already exists, this will overwrite the contents of the file:

The test3.txt file now has the following content:

5. Redirect Contents of Multiple Files

You can redirect the contents of multiple file into one single file:

Display the content of test3.txt with:

The output shows the contents of both files, as in the image below.

6. Display the Contents in Reverse Order

The cat command can display the content of a file in reverse order (by lines). To do this, use tac (cat in reverse):

7. Append File Contents to Another File

The cat command can add the contents of a file to the end of another file. Instead of using a single > sign, use a double >> sign:

Open the test3 file by running:

The content of test3 followed by test1 should display.

Note: If you want to remove the sample files, take a look at how to remove files and directories using the Linux command line.

8. Append Text to Existing File

You can use a similar command to append text to an existing file:

Add a new line to the file:

Hold Ctrl and hit d.

Check the content of the test1.txt file:

9. Combine Operations

The functions of the cat command can be combined. For example, to combine the output of two files, and store the result in a new file:

Alternately, you can append multiple files to the end of an existing file:

Note that the order specified is the order the files in which they are added to the destination file.

Note: Once you have created multiple files, you may want to group them in a single directory. Take a look at how to use mkdir command to make or create a Linux directory.

10. More and Less Options (Manage Large Files)

If you use cat on a very large file, you’ll end up with a huge string of data that’s hard to read. You can break it into pages using | more :

This displays a single page of the file. When you press a key, it will scroll to the next page.

If you’d like the ability to scroll forward and backward through the display, use | less .

11. Show Line Numbering

You may find it useful to have line numbers in the output, especially for large files. To enable line numbering, add the -n option to the cat command:

The output should appear as in the image below:

12. Show the End of Line

You can instruct cat to highlight the end of each line and spaces between lines with $.

To do so, use the command:

Since the sample file test1.txt has only one line, the output shows one $ at the end of it.

13. Show TAB Separated Lines

The cat command has the option of displaying the file content along with the tab space within the text.

To show tab separated lines for a sample run:

The tab space within the text is represented by ^I.

14. Remove Blank Lines

To omit blank lines from the output of cat with the –s option:

15. List All CAT Commands

If you have trouble remembering the options, use the —help command:

You should now have a good understanding of how to use the cat command in Linux.

Want to master more Linux commands? Check out our Linux Commands Cheat Sheet.

Источник

Читайте также:  Windows ce 6 0 навигатора навитела
Оцените статью