Re: Deleting first and last lines of a text file
by morgon (Priest) on May 16, 2014 at 09:32 UTC
|
perl -i.old -ne 'print unless $.==1 or eof' <input-file>
This will also keep the old version around as <input.file>.old | [reply] [d/l] |
|
Hi
I tried the code like this and got error as
syntax error at ./perl_deletelines.pl line 5, near "-ne"
Execution of ./perl_deletelines.pl aborted due to compilation errors.
</P
#!/usr/bin/perl
use strict;
use warnings;
perl -i.old -ne 'print unless $.==1 or eof' /home/file_20140407.txt
| [reply] [d/l] |
|
BEGIN { $^I = ".old"; }
LINE: while ( defined( $_ = <ARGV> ) ) {
print $_ unless $. == 1 or eof;
}
putting the above in a script should work.
Of course, morgon code works fine from the CLI.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] [d/l] |
|
|
|
|
|
|
Thanks for the reply. I will try and let you know
| [reply] |
Re: Deleting first and last lines of a text file
by AppleFritter (Vicar) on May 16, 2014 at 09:32 UTC
|
#!/usr/bin/perl
<>;
while($line = <>) {
print if defined;
$_ = $line;
}
Explanation: the angle bracket operator reads a line from a while, so the first line is read and discarded. After that, $_ (Perl's implicit topic variable) is used to store the previous line; it's initially undefined, so the print statement doesn't get executed on the first loop iteration, only the subsequent ones. Since only the previous line gets printed, not the current line, the last line of the file is skipped.
EDIT: here's a slightly more idiomatic solution:
#!/usr/bin/perl
<>;
while(<>) {
last if eof;
print;
}
It's pretty much the same as before, but does away with remembering the previous line and instead just exits the loop before printing if the end of the file has been reached, thus neglecting to print the last line.
EDIT 2: just so there's no confusion in the future, the OP originally asked for the first and last line of a text file to be removed, not the first line and last two lines.
| [reply] [d/l] [select] |
|
#!/usr/bin/perl
my $file='jjl.exec.txt';
open STDOUT, ">", $file or die "$0: open: $!";
open STDERR, ">&STDOUT" or die "$0: dup: $!";
<>;
while($line = <>) {
print if defined;
$_ = $line;
}
| [reply] [d/l] |
|
Hmm, that should... and is, in fact, working for me. Are you sure there's not simply an empty line at the end of your input file? Perhaps you could share some sample data.
Also, why the open calls? If you want the output to go to a specific file, it'd be easier to simply redirect the script's output on the command line, like so:
$ ./scriptname srcfile.txt >jjl.exec.txt
Alternatively, I'd suggest at least using a different filehandle than STDOUT, though you'd of course have to adjust the print statement to print to that filehandle then. opening STDOUT like that apparently works, but it's giving me the heebies.
| [reply] [d/l] |
|
Hi
Where are we specifying the filename here?.
Thanks
VJ
| [reply] |
|
$ perl script.pl data.dat
or
$ cat data.dat | perl script.pl
Such is the magic of the angle bracket operator when used with a null filehandle. Quoting perlop:
The null filehandle <> is special: it can be used to emulate the behavior of sed and awk, and any other Unix filter program that takes a list of filenames, doing the same to each line of input from all of them. Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames. The loop
while (<>) {
... # code for each line
}
is equivalent to the following Perl-like pseudo code:
unshift(@ARGV, '-') unless @ARGV;
while ($ARGV = shift) {
open(ARGV, $ARGV);
while (<ARGV>) {
... # code for each line
}
}
except that it isn't so cumbersome to say, and will actually work. It really does shift the @ARGV array and put the current filename into the $ARGV variable. It also uses filehandle ARGV internally. <> is just a synonym for <ARGV>, which is magical. (The pseudo code above doesn't work because it treats <ARGV> as non-magical.)
Hope this helps!
| [reply] [d/l] [select] |
Re: Deleting first and last lines of a text file
by ww (Archbishop) on May 16, 2014 at 11:09 UTC
|
Hi, vsmeruga. I'm a coder, not an architect, but I need to design and build a skyscraper. If I ask how to do each step and question each bit of advice I'm given (without referring to the tutorials and examples here), will you build me my skyscraper step-by-step and without compensation?
If you didn't program your executable by toggling in binary, it wasn't really programming!
| [reply] |
|
Gurus
I know programming but not Perl. That is why I have asked you for help.
Many thanks
VJ
| [reply] |
Re: Deleting first and last lines of a text file
by blue_cowdawg (Monsignor) on May 16, 2014 at 13:56 UTC
|
#!/usr/bin/perl -w
use Tie::File;
use strict;
my $fname = shift @ARGV;
tie my @file,'Tie::File',$fname or die $!;
pop @file;
pop @file;
shift @file;
untie @file;
exit(0);
Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
| [reply] [d/l] |
Re: Deleting first and last lines of a text file
by DrHyde (Prior) on May 16, 2014 at 10:48 UTC
|
HEAD=`head -1 myfile.txt`
TAIL=`tail -1 myfile.txt`
grep -v $HEAD myfile.txt|grep -v $TAIL>myfile.txt~
mv myfile.txt~ myfile.txt
Untested code. Needs some debugging. | [reply] [d/l] |
|
the
quick
brown
fox
jumps
over
the
lazy
dog's
back
into this:
quick
brown
fox
jumps
over
lazy
dog's
rather than this:
quick
brown
fox
jumps
over
the
lazy
dog's
| [reply] [d/l] [select] |
|
| [reply] |
|
This deletes the fist line and the last 2 lines of input.txt and keeps a copy of the old version in input.txt.old.
perl -i.old -ne 'my $l = <>; print($l), next if $.==2; print "$_$l" un
+less eof' input.txt
Note that this is a one-liner to be run as is from the command-line (adjust your input file-name). | [reply] [d/l] |
|
Re: Deleting first and last lines of a text file
by hippo (Bishop) on May 16, 2014 at 13:51 UTC
|
Here is a relatively simple generic solution to be tailored to your specific requirements. I've named it headtail.pl but you can call it whatever you want.
#!/usr/bin/perl -Tw
use strict;
use warnings;
my $skiphead = 1;
my $skipfoot = 2;
my $infile = 'test.txt';
my $outfile = '/tmp/out.txt';
my @buffer = ();
open IN, '<', $infile or die "Cannot open $infile for reading: $!";
open OUT, '>', $outfile or die "Cannot open $outfile for writing: $!";
<IN> for (1..$skiphead);
push @buffer, scalar <IN> for (1..$skipfoot);
while (<IN>) { print OUT shift @buffer; push @buffer, $_; }
close OUT;
close IN;
| [reply] [d/l] [select] |
|
Oh! YES. Thank you very much. It works.. deleted first line and last 2 lines in my text file
| [reply] |
|
#!/usr/bin/perl
use Time::Piece;
use strict;
use warnings;
my $filedt = localtime->strftime('%Y%m%d');
my $skiphead = 1;
my $skipfoot = 2;
my $infile = '/home/vmeruga_alt/EXEC_${filedt}.txt';
my $outfile = '/home/vmeruga_alt/EXEC_tgt.txt';
my @buffer = ();
open IN, '<', $infile or die "Cannot open $infile for reading: $!";
open OUT, '>', $outfile or die "Cannot open $outfile for writing: $!";
<IN> for (1..$skiphead);
push @buffer, scalar <IN> for (1..$skipfoot);
while (<IN>) { print OUT shift @buffer; push @buffer, $_; }
close OUT;
close IN;
| [reply] [d/l] |
|
| [reply] |
|
|
|
| [reply] |
Re: Deleting first and last lines of a text file
by Anonymous Monk on May 16, 2014 at 09:21 UTC
|
| [reply] |
|
| [reply] |
Re: Deleting first and last lines of a text file
by Bloodnok (Vicar) on May 16, 2014 at 10:25 UTC
|
| [reply] [d/l] |
|
Hi I tried the below shell code. But it removed only first line. Thanks
#!/bin/sh
sed -e '1d;$d' /home/file_20140407.txt >>file_20140407_n.txt
| [reply] [d/l] |
|
$ sed -e '1d;$d' jaxws/src/share/jaxws_classes/com/sun/xml/internal/me
+ssaging/saaj/soap/SOAPDocumentFragment.java > t
$ diff jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/sa
+aj/soap/SOAPDocumentFragment.java t
1d0
< /*
45d43
< }
Does your file have a newline as the last character ? You'll experience problems if not.
A user level that continues to overstate my experience :-))
| [reply] [d/l] |
|
Re: Deleting first and last lines of a text file
by admiral_grinder (Pilgrim) on May 16, 2014 at 13:39 UTC
|
use Path::Class qw{ file };
#
# Getting file name is left to reader
#
my $input_file = file( $filename );
#
# WARNING: Destructive operation!
#
my $output_file = $input_file; # Left to the reader to change
my @file_contents = $input_file->slurp();
shift @file_contents; # Remove first line
pop @file_contents; # Remove last line
$output_file->spew( @file_contents );
| [reply] [d/l] |
Re: Deleting first and last lines of a text file
by Anonymous Monk on May 16, 2014 at 09:30 UTC
|
# In file: "command.ed"
1
d
$
d
w
Q
# ed file-to-modify < command.ed >/dev/null
| [reply] [d/l] |
|
# In file: "command.ed"
1d
$d
wQ
...
| [reply] [d/l] |
|
Hi
I have to automate the task as I have to delete lines in the file every day before my original load task begins
Thanks
VJ
| [reply] |
|