- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- head / tail function
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2003 03:18 AM
04-04-2003 03:18 AM
if I have a file thus :
1
2
3
4
5
and I wanted to get a section, or number of lines from the middle (using two line numbers) i.e. :
2
3
4
how would I do this (could I use head or tail?)
many thanks
John
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2003 03:37 AM
04-04-2003 03:37 AM
Solution1
2
3
4
5
.
.
.
10
#!/bin/bash
# expects number of lines to be removed from head and tail as argument
nlines=`wc -l a.txt | awk '{print $1}'`
nlines=`expr $nlines - $1`
hlines=`expr $nlines - $1`
tail -$nlines a.txt | head -$hlines
# EOF
should work.
- ramd.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2003 03:43 AM
04-04-2003 03:43 AM
Re: head / tail function
This is one way to do it using head and tail:
head -4 file | tail +2
The problem with head and tail is that they have a limited buffer space to work with, so they work fine with smaller files but not with large files. Probably Perl is a better choice to work with most any size file. You can do it like this with Perl:
perl -ne '2..4 and print' file
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2003 04:05 AM
04-04-2003 04:05 AM
Re: head / tail function
Probably perl would be a better option as pointed out.
- ramd.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2003 04:16 AM
04-04-2003 04:16 AM
Re: head / tail function
sed -ne '2,5p' filename
HTH,
G.