JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Print a file after skipping first X lines in Bash

Author:JIYIK Last Updated:2025/03/22 Views:

Suppose you have a file, a large file, and you want to display its contents. How would you do it?

You obviously don't want to print out the entire contents of the file, as that's not very practical. You might want to print some selective lines; perhaps you'd use a regular expression to parse the file and print only the matches.

This article will explain several methods.


Skipping lines using head and tail commands

The following example uses five of the most commonly used Bash commands to do this:

tail fileName.txt
head fileName.txt
tail -n fileName.txt # here n is an integer
head -n fileName.txt # here n is an integer
tail +n fileName.txt # prints all lines starting from
# the nth file

tailThe command prints the last ten lines of a file, while the head command prints the first ten lines of a file.

When you use the command with –n option tail, it prints the last n lines. Conversely, when you use the command with –n option head, it prints the first n lines.

It is important to note that n here is an integer, so when you execute the command, you must replace n with an integer.

The fifth method is a bit tricky. It ignores the first (n – 1) rows and prints all the rows after that.


Skip the first n lines using vim

You can also skip first n lines using Vim editor. Vim is a console-based text editor that allows you to create and change any text document efficiently.

To use Vim for the first time, you must install it. Do this with the following command:

$ sudo apt install vim

Now that Vim is installed, we get into the business part of our goal, which is to skip the first n lines using Vim.

We will do this by using an intermediate file. We will first copy the contents of the old file into the new file; then, we will delete the first n lines of the new file.

We will use input and output redirection to copy the contents of one file to another file. If you have taken an operating system course, you should have heard of the ppfdt table (process file descriptor table).

By default, the first descriptor points to stdin (or the keyboard), the second to stdout (or the monitor), and the third to stderror.

Consider the following script for further understanding.

cat 0<old_file.txt 1>new_file.txt # copies old file’s contents to new file

The above command reads the contents of the old file and copies it to the new file. Notice how we use descriptor 0 to read and descriptor 1 to write.

If you find this descriptor confusing, we have something different for you. The following command will also work:

cat <old_file.txt >new_file.txt # copies old file’s contents to new file
cp <old_file.txt >new_file.txt # copies old file’s contents to new file

请注意In the second method above, we used the copy command (i.e., cp). It has two parameters: the path of the source file and the path of the destination file.

Now that we have copied the file, open the new file in Vim and use the following command:

vim new_file.txt

Now use Shift+Esc and type the following command in Vim:

:1,nd # here n is an integer number e.g., 2

The above command deletes the first n lines from new_file.txt. Here, d stands for delete.


Using sed to skip first n lines

Creating a new file and then deleting its contents can be cumbersome. Also, if the old file is large, it can consume a lot of extra memory as well.

So, let's look at a simpler way to achieve the same thing:

sed 1,nd old_file.txt # here n is an integer

It is important to note that sedthe command does not modify the old file. Instead, it simply displays the contents of the old file after deleting the first n lines.

Previous:Get the Primary IP Address in Linux

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Get the Primary IP Address in Linux

Publish Date:2025/03/22 Views:70 Category:OPERATING SYSTEM

There are various ways to get network details in Linux. We will learn some of them in this article. This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash

Bash History Size

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it. Most deve

Find Current Folder Name in Bash

Publish Date:2025/03/22 Views:106 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Changing Directories in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

In this article, we will learn how to change directories in Bash. The term directory is used to refer to a folder. Because you frequently move between folders when using Bash and when using the Git version control system, it is essential to

Running Regular Expressions in Case Statements in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters

Sending a message to logged in users in Bash

Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM

This article explores methods to send data to another logged in user in Bash. This article discusses methods to find active users and send them messages. Finding Online Users in Bash Before sending data to an online user, you must verify wh

Bash copy all files from remote directory

Publish Date:2025/03/22 Views:156 Category:OPERATING SYSTEM

This article describes methods for copying files between different hosts on a network. It also explores the scp command and how to use it to copy files from a remote location. scp command Secure Copy Protocol (SCP) is a secure copy network

Batch file to loop through files in subdirectories

Publish Date:2025/03/22 Views:96 Category:OPERATING SYSTEM

This article explains how we can write a batch script to loop through files in subdirectories. We will take an example to explain the concept. Batch file to loop through files in subdirectories Assume we have the directory structure shown b

Batch file to remove X characters from a file name

Publish Date:2025/03/22 Views:153 Category:OPERATING SYSTEM

This article explains how we can remove specific characters from the filename of a file using a batch script. We will cover several methods below to rename files on Windows. File Explorer Renaming on Windows File Explorer offers the least f

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial