[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5066
  • Last Modified:

delete line of file

is it possible to delete line of a file in perl without opening and closing a file
i mean i know to create a new file blah..... but that is a long way....

i want simple unix command to be used that can take care of it.

ex if file temp.txt contains:
james
robert half
todd minella

after executing my perl script the file temp.txt comtains
rahuk
todd minells
0
shahrahulb
Asked:
shahrahulb
  • 4
  • 3
  • 2
1 Solution
 
shahrahulbAuthor Commented:
also how can i delete first n lines
0
 
ozoCommented:
perl -MTie::File -e 'tie @array, "Tie::File", shift or die $!; splice @array,0,2,"rahuk" ' temp.txt

perl -MTie::File -e '$n=2;tie @array, "Tie::File", shift or die $!; splice @array,0,$n' temp.txt
0
 
manav_mathurCommented:
DO you want to delete the line based on 'matching to a string' or on the basis of line numbers??

for deletion based on matching
sed '/string to match/d' temp.txt

for deletion based on
sed 'nd' temp.txt ##where n is the line number you want to delete.

but again, looks like you are inserting 'rahuk' also. Then it depends whether you want to go with Ozo's solution, or if you can, possibly do a
sed 's/initial/rahuk/' temp
#or
sed 'ns/.*/rahuk/' temp ##where n is the line number you want to replace


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
manav_mathurCommented:
to delet 1st n lines

sed '1,nd' temp.txt
0
 
ozoCommented:
sed '1,2d' temp.txt
will print all but the first 2 lines, but not affect the original file.
perl -pe '$_=""if 1..2' temp.txt
will print all but the first 2 lines, but not affect the original file.
perl -i -pe '$_=""if 1..2' temp.txt
will delete the first 2 lines from the file.
If course, all these are opening and closing the file behind the scenes.


0
 
manav_mathurCommented:
Yeah, I was thinking that maybe the output redirection and renaming part could always be taken care of. But the author says 'I want simple Unix commands' and Perl solutions were already given.

And Tie::File also does rewrite the file on every modification.

perl -i -ne 'print if ($. > n)' temp.txt
#can also be used to remove the first n lines of the file. but again, even this opens/closes the file
0
 
shahrahulbAuthor Commented:
if i try this in perl script:
system ("perl -i -pe '$_=\"\"if 1..7' out.txt");

i get error
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
0
 
manav_mathurCommented:
Atrnatively, you can do

{local $^I; local @ARGV=("out.txt");
while(<>) {$_="" if 1..7}
}
or
{local $^I; local @ARGV=("out.txt");
while(<>) {print if $.>7}
}
0
 
shahrahulbAuthor Commented:
this worked

system("perl -i -pe '\$_=\"\"if 1..2' out.txt");
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now