Skip to main content

head command in unix

 

 

Head Command Examples

The head command in unix or linux system is used to print the first N lines from the file to the terminal. The syntax of head command is 

head [options] [files]


The 
head command options are: 

·                       c : Prints the first N bytes of file; With leading -, prints all but the last N bytes of the file.

·                       n : Prints first N lines; With leading - print all but the last N lines of each file.


Head Command Examples
Create the following file in your linux or unix operating system for practicing the examples: 

> cat example.txt

linux storage

ubuntu os

fedora


1. Display first 10 lines 

By default, the head command prints the first 10 lines from a file. 

> head example.txt


2. Display first N lines 

Use the -n option to 
print the first n lines from a file. The following example prints the first 2 lines from the file: 

> head -n2 example.txt

linux storage

ubuntu os


3. Skip last N lines 

You can 
skip the last N lines from a file and print the remaining lines. The following example skips the last 2 lines and prints the remaining lines. 

> head -n-2 example.txt

linux storage


4. Print the first n bytes. 

use the -c option to 
print the first N bytes from the file. The following example prints the first 5 bytes from the file. 

> head -c5 example.txt

linux


5. Skip printing last n bytes. 

Use the leading "-", to skip printing last N bytes. 

> head -c-7 example.txt

linux storage

ubuntu os


6. Print line between M and N lines. 

You can combine the head command with tail command to 
print lines between the line numbers M and N. The following command prints the lines between numbers 5 and 10. 

> head -n10 filename | tail -5

 

Comments

Popular posts from this blog

Oracle Merge Statement Delta Detection

The MERGE Syntax by Ajay Nerumati Delta Detection in Oracle SQL Posted on  8. October 2016 Delta detection is a common task in every Data Warehouse. It compares new data from a source system with the last versions in the Data Warehouse to find out whether a new version has to be created. There are several ways to implement this in Oracle. Your source system delivers a full extraction every night, and you have to load only the changed rows into your Core Data Warehouse? You receive incremental loads from another source system every few minutes, but only a few columns are loaded into the Data Warehouse. In all these situations, you need a delta detection mechanism to identify the rows that have to be inserted or updated in your Data Warehouse tables. In this blog post, I want to show different methods in Oracle SQL that provide the subset of rows of a source table that were changed since the last load. All these methods are set-based, i.e. they can be executed in one SQL sta...

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7   ...

PL / SQL practice programs

  PL / SQL  practice programs 1. Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING   BEGIN   DBMS_OUTPUT.PUT_LINE('WELCOME   TO   PL/SQL   PROGRAMMING'); END; /   2. Write a program to print the numbers from 1 to 100   DECLARE N NUMBER(3):=1; V VARCHAR2(1000); BEGIN WHILE N <=1000 LOOP V:=V||''||N; N:=N+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V); END; / 3. write a program to print the even numbers from 1 to 100 DECLARE N NUMBER(3):=0; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 4. Write a program to print the odd numbers from 1 to 100 DECLARE N NUMBER(3):=1; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 5. write a program for multiplication table DECLARE A NUMBER(2):=&A; B   NUMBER(2):=1; C ...