- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- How to strip off the first and last line of an inp...
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
09-07-2005 10:28 PM
09-07-2005 10:28 PM
How to strip off the first and last line of an input file
tail command was tried out but does'nt work with largefiles. (tail +2 input file | head –count_of_data_records > output file)
'sed' was also tried , but it has a limitation on record length (around 4000 char).
Can someone suggest an alternative for this , maybe using AWK.
Tanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2005 10:36 PM
09-07-2005 10:36 PM
Re: How to strip off the first and last line of an input file
# print all but first line of file
sed 1d infile >> outfile
-and-
# delete the last line of the file
sed -e '$d' outputfile
Pete
Pete
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2005 10:50 PM
09-07-2005 10:50 PM
Re: How to strip off the first and last line of an input file
2 bah
3 ohh
4 yea
lt09:/home/merijn 105 > cat xx.txt
1 hah
2 bah
3 ohh
4 yea
5 mee
lt09:/home/merijn 106 >
or, shorter
lt09:/home/merijn 107 > perl -ne'$.>2&&print$p;$p=$_' xx.txt
2 bah
3 ohh
4 yea
lt09:/home/merijn 108 >
Enjoy, Have FUN! H.Merijn
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2005 10:51 PM
09-07-2005 10:51 PM
Re: How to strip off the first and last line of an input file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2005 11:41 PM
09-07-2005 11:41 PM
Re: How to strip off the first and last line of an input file
Another perl variation:
# perl -ne 'print if ($. > 1 && !eof)' myfile
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2005 12:13 AM
09-08-2005 12:13 AM
Re: How to strip off the first and last line of an input file
Merijn's perl solution would be my favourite, but if you insist on awk a much similar solution is:
awk 'BEGIN{getline} (NR>2) {print line}{line=$0}' input-file > output-file
- eat the first line to begin
- print variable 'line', holding the prior line, starting on the 3rd input line.
- stash current input line in variable 'line'
The 'tricky' part in awk was to deal with an initially empty 'line'. Awk insists on 'helping' by adding a new-line, perl has no such problem.
I have NOT tried on long records.
hth,
Hein
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2005 01:10 AM
09-08-2005 01:10 AM
Re: How to strip off the first and last line of an input file
It can be done easily in ex with the following commands:
# ex -s input_file <<\EOF
> 1d | $d
> wq
> EOF
- Tags:
- ex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2005 02:32 AM
09-08-2005 02:32 AM
Re: How to strip off the first and last line of an input file
Haven't tested this on record lengths of approx. 4000 chars, but this awk construct will strip an input file of its first and last lines:
# awk '{"wc -l <" FILENAME|getline cnt;if((NR!=1)&&(NR!=cnt)) print $0 > "outfile"}' inputfile
cheers!