Sunday 29 January 2012

head


head  
head command is used to view or save a specified portion of the beginning of a file.
It  does  the same work as tail does for the end of the file.

Example 1)
Head by default displays first 10 lines of a file when no option is specified.
/home/ftpusr$ head    blackwater.ns
Blackwater USA was formed in 1997, by Erik Prince in North Carolina, to provide training support to military and law enforcement organizations. In explaining the Blackwater's purpose, Prince stated that ‘‘We are trying to do for the national security apparatus what FedEx did for the Postal Service.’’After serving SEAL and SWAT teams, Blackwater USA received their first government contract after the bombing of the USS Cole off of the coast of Yemen in October of 2000. After winning the bid on the contract, Blackwater was able to train over 100,000 sailors safely.Prince purchased about 7,000 acres (28 km2) (from Dow Jones Executive, Sean Trotter) of the Great Dismal Swamp, a vast swamp on the North  Carolina/Virginia border, now mostly a National Wildlife Refuge. "We needed 3,000 acres to make it safe," Prince told reporter Robert Young Pelton.There, he created his state-of-the-art private training facility and his contracting company, Blackwater, which he named for the peat-colored water of the swamp. The The Blackwater Lodge and Training Center officially opened on May 15, 1998 with a 6,000 acre facility and cost $6.5 million.

If   you  wish  to view only first two  lines of the file, use
/home/ftpusr$  head -2  blackwater.ns
Blackwater USA was formed in 1997, by Erik Prince in North Carolina, to provide training support to military and law enforcement organizations. In explaining the Blackwater's purpose, Prince stated that ‘‘We are trying to do for the

Example 2)
when you have a very large file(say of 100000 lines)  and it is difficult to open such a file in vi, but you are concerned only with first  few  lines of the file.
In that case  redirect  the first  few  lines of that huge file  into  another file and open that file in vi.

/home/ftpusr$ head  -1000  migratory_note   > migratory_note_1000
/home/ftpusr$vi  migratory_note_1000
..
..

Example 3)
In  scripts, there are times  where  you  initialize the   output of grep to a variable but  grep searches and returns multiple values, you can use only the first returned value by head -1,
Confused..?the example below shows how.

/home/ftpusr$
/home/ftpusr$b=`grep  7   prime_numbers`
/home/ftpusr$echo $b
7
17
37

Suppose you want the first occurring prime number containing 7,
Use head as follows.
/home/ftpusr$b=` grep  7   prime_numbers | head  -1`
/home/ftpusr$echo  $b
7


Example 4)
To print the nth line of a file, use  head and tail in combination as follows.

/home/ftpusr$ head -100 operating-out.sh |  tail  -1
This prints 100th  line of the file.


No comments:

Post a Comment