Advertisement

01.02.2007 at 09:59PM PST, ID: 22109090
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

delete 1st line in text file

Tags: perl, line, file, delete, text
I need a perl script that will open an existing file delete the first line and then write them back out.
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: casit
Solution Provided By: teraplane
Participating Experts: 4
Solution Grade: A
Views: 130
Translate:
Loading Advertisement...
01.02.2007 at 10:04PM PST, ID: 18233415

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 10:04PM PST, ID: 18233416

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 10:06PM PST, ID: 18233419

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 10:13PM PST, ID: 18233434

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 10:16PM PST, ID: 18233437

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 10:31PM PST, ID: 18233473

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 10:38PM PST, ID: 18233496

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 10:41PM PST, ID: 18233505

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 10:57PM PST, ID: 18233530

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2007 at 11:02PM PST, ID: 18233549

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.03.2007 at 04:00AM PST, ID: 18234367

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.03.2007 at 05:13AM PST, ID: 18234694

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.02.2007 at 10:04PM PST, ID: 18233415

Rank: Wizard

open(IN,"<file.txt);
open(OUT,">new_file.txt");
while (IN)
{
   # if not first line ($.) write current line ($_) to out file
    $. > 1 and print(OUT $_);
}
close(IN);
close(OUT);

# if you want ot overwrite the original
rename("new_file.txt","file.txt");
 
01.02.2007 at 10:04PM PST, ID: 18233416

Rank: Genius

use Tie::File;
 tie @array, 'Tie::File', $filename or die "$filename $!";
 shift @array;
 untie @array;
 
01.02.2007 at 10:06PM PST, ID: 18233419

Rank: Genius

perl -i.bak -ne "print if 2..0" filename
 
01.02.2007 at 10:13PM PST, ID: 18233434
How would I get your script teraplane to accept the filename as a parameter?
 
01.02.2007 at 10:16PM PST, ID: 18233437

Rank: Genius

How do you want to pass the parameter?
 
01.02.2007 at 10:31PM PST, ID: 18233473

Rank: Wizard

Try this, now just say:
perl progname,pl in_file_name

my $in_file = $ARGV[0];
my $out_file = "$in_file\.tmp";
open(IN,"<$in_file);
open(OUT,">$out_file);
while (IN)
{
   # if not first line ($.) write current line ($_) to out file
    $. > 1 and print(OUT $_);
}
close(IN);
close(OUT);

# if you want to overwrite the original
rename($out_file,$in_file);
Accepted Solution
 
01.02.2007 at 10:38PM PST, ID: 18233496

Rank: Genius

$^I=".tmp";
<>;
print while <>;
 
01.02.2007 at 10:41PM PST, ID: 18233505

Rank: Genius

But the using Tie::File is the reccomended solution
perldoc -q "delete a line in a file"
 
01.03.2007 at 04:00AM PST, ID: 18234367

Rank: Sage

Since it's just the first line:

#!/usr/bin/perl -w
use strict;

my $file = $ARGV[0] or die "Please include the filename.\n";

my $contents;
open (ORG, "$file") or die "Can't open $file: $!\n";
$_ = <ORG>;
{ local $/; $contents = <ORG>; }
open (OUT, ">$file") or die "Can't open new $file: $!\n";
print OUT $contents;
 
01.03.2007 at 05:13AM PST, ID: 18234694

Rank: Sage

If you'd prefer to get a bit more verbose, this will take the input and the output filenames on the command line, giving you a couple of attempts to get both right, avoid overwriting existing files, etc.

#!/usr/bin/perl -w
use strict;
$| = 1;

if (@ARGV != 2) {
    my ($script) = ($0 =~ /[\/|\\]?([\w\.]+)$/);
    print "Usage: $script org_file new_file\n";
    exit;
}

unless (-e $ARGV[0]) {
    my $file;
    print "\nNo file named $ARGV[0] found.  Retry (y/n) [n]? ";

    my $ans = <STDIN>;
   
    if ($ans =~ /^y(es)?\n$/i) {
        print "\nEnter new filename: ";
        $file = <STDIN>;
        chomp ($file);
        if (($file) && (-e $file)) {
            $ARGV[0] = $file;
        } else {
            print "\nCan't find second attempt at file to alter.\n";
            print "\nQuitting. No changes made.\n";
            exit;
        }
    } else {
        print "\nQuitting. No changes made.\n";
        exit;
    }
}

if (-e $ARGV[1]) {
    print "\nA file named $ARGV[1] already exists and will be overwritten.  Re-enter output file name (y/n) [n]? ";

    my $ans = <STDIN>;
   
    if ($ans !~ /^y(es)?\n$/i) {
        print "\n$ARGV[1] will be overwritten.  Are you sure? (y/n) [n]? ";
        my $ans = <STDIN>;
        if ($ans !~ /^y(es)?\n$/i) {
            print "\nQuitting. No changes made.\n";
            exit;
        }
    } else {
        print "\nEnter new filename: ";
        my $new_outfile = <STDIN>;
        chomp ($new_outfile);
        if ($new_outfile) {
            if (-e $new_outfile) {
                print "\nA file named $new_outfile already exists and will be overwritten.  Continue (y/n) [n]? ";
                my $ans = <STDIN>;
                if ($ans !~ /^y(es)?\n$/i) {
                    print "\nQuitting. No changes made.\n";
                    exit;
                }
            }
            $ARGV[1] = $new_outfile;
        } else {
            print "\nQuitting. No changes made.\n";
            exit;
        }
    }
}

my $contents;
open (ORG, "$ARGV[0]") or die "Can't open $ARGV[0]: $!\n";
$_ = <ORG>;
{ local $/; $contents = <ORG>; }
open (OUT, ">$ARGV[1]") or die "Can't open $ARGV[1]: $!\n";
print OUT $contents;

print "\nDone.\n";
 
 
20080236-EE-VQP-29