Sponsored Content
Top Forums Shell Programming and Scripting Perl:How to insert a line to a file. Post 302480361 by citaylor on Tuesday 14th of December 2010 04:19:36 PM
Old 12-14-2010
Sorry, I misunderstood...

Code:
#!/usr/bin/perl
open(FILE,"foo.txt") || die "can't open file for read\n"; 
my @lines=<FILE>;
close(FILE);
open(FILE,">foo.txt")|| die "can't open file for write\n";
foreach $line (@lines) {
    print FILE "CCC\n" if($line =~ /AAA/);
    print FILE $line;
}#end foreach
close(FILE);

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Insert a line as the first line into a very huge file

Hello, I need to insert a line (like a header) as the first line of a very huge file (about 3 ml rows). I am able to do it with sed, but redirecting the output and creating a new file takes quite some time. I was wondering if there was a more efficient way of doing it? Any help would be... (3 Replies)
Discussion started by: shriek
3 Replies

2. Shell Programming and Scripting

insert a line in a file

Hello guys, Need to know how to insert a line at top of the file, without using temp files. Can we do it on the fly? Regards, Rishi (7 Replies)
Discussion started by: RishiPahuja
7 Replies

3. Shell Programming and Scripting

how to insert text before first line in perl

Hello all im doing simple parsing on text file , but now I need to insert string before the first line of the text file , how can I do that in perl? (3 Replies)
Discussion started by: umen
3 Replies

4. Shell Programming and Scripting

Insert line into file

Hi, My File 'temp.txt' contents are like this. <Managers> Mng={{FIL|FAVEI.mng|111}|15.000000|17.000000|17.000000| Mng={{FIL|FAPSV.mng|222}|3.000000|0.000000|0.000000|0.000000| Mng={{FIL|FAVIF.mng|333}|8.000000|8.000000|8.000000|8.000000|... (3 Replies)
Discussion started by: vinay123
3 Replies

5. Shell Programming and Scripting

to insert some word somewhere in the line with shell (or perl)

hello mighty all there is a line of 50 words and i need to take a random number of words from the beginning (20 words for example) then put my word then add other 10 words from the continue then add another my special word then add 20 words till the end.. my own knowledge base can say it is... (12 Replies)
Discussion started by: tip78
12 Replies

6. Programming

insert line into a file

how to insert a line of text that is next to the current line(file pointer pointing to) in the file ?? :wall: ex: suppose a file named 'Sample' has the following content in it. this is to give clear idea about the problem if file pointer is pointing to the first line then i want to... (3 Replies)
Discussion started by: kavitha rao
3 Replies

7. Linux

How to insert new line in perl

HI, I have a text file in which I have removed all new lines as I would like to introduce a new line at the end of each record in the file. There is no common end line for all the records. A new record will start by *RECORD*. So I want to introduce a new line before this line *RECORD*. So Can... (2 Replies)
Discussion started by: kaav06
2 Replies

8. Shell Programming and Scripting

Insert a new line before every 5th line in a file

Hi, I need to insert a new line containing the string "QUERY" above every 5 lines. The below piece of code inserts a new line after every 5th line awk '{print $0} !(NR%5) {print "QUERY"}' sed 'n;n;n;n;G;' --> I do not know how to give "QUERY" string here But I need to insert it before... (4 Replies)
Discussion started by: royalibrahim
4 Replies

9. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

10. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies
URI::ToDisk(3pm)					User Contributed Perl Documentation					  URI::ToDisk(3pm)

NAME
URI::ToDisk - An object for mapping a URI to an on-disk storage directory SYNOPSIS
# We have a directory on disk that is accessible via a web server my $authors = URI::ToDisk->new( '/var/www/AUTHORS', 'http://ali.as/AUTHORS' ); # We know where a particular generated file needs to go my $about = $authors->catfile( 'A', 'AD', 'ADAMK', 'about.html' ); # Save the file to disk my $file = $about->path; open( FILE, ">$file" ) or die "open: $!"; print FILE, $content; close FILE; # Show the user where to see the file my $uri = $about->uri; print "Author information is at $uri "; DESCRIPTION
In several process relating to working with the web, we may need to keep track of an area of disk that maps to a particular URL. From this location, we should be able to derived both a filesystem path and URL for any given directory or file under this location that we might need to work with. Implementation Internally each "URI::ToDisk" object contains both a filesystem path, which is altered using File::Spec, and a URI object. When making a change, the path section of the URI is altered using <File::Spec::Unix>. Method Calling Conventions The main functional methods, such as "catdir" and "catfile", do not modify the original object, instead returning a new object containing the new location. This means that it should be used in a somewhat similar way to File::Spec. # The File::Spec way my $path = '/some/path'; $path = File::Spec->catfile( $path, 'some', 'file.txt' ); # The URI::ToDisk way my $location = URI::ToDisk->new( '/some/path', 'http://foo.com/blah' ); $location = $location->catfile( 'some', 'file.txt' ); OK, well it's not exactly THAT close, but you get the idea. It also allows you to do method chaining, which is basically URI::ToDisk->new( '/foo', 'http://foo.com/' )->catfile( 'bar.txt' )->uri Which may seem a little trivial now, but I expect it to get more useful later. It also means you can do things like this. my $base = URI::ToDisk->new( '/my/cache', 'http://foo.com/' ); foreach my $path ( @some_files ) { my $file = $base->catfile( $path ); print $file->path . ': ' . $file->uri . " "; } In the above example, you don't have to be continuously cloning the location, because all that stuff happens internally as needed. METHODS
new $path, $http_url The "new" constructor takes as argument a filesystem path and a http(s) URL. Both are required, and the method will return "undef" is either is illegal. The URL is not required to have protocol, host or port sections, and as such allows for host-relative URL to be used. Returns a new "URI::ToDisk" object on success, or "undef" on failure. param $various "param" is provided as a mechanism for higher order modules to flexibly accept URI::ToDisk's as parameters. In this case, it accepts either an existing URI::ToDisk object, two arguments ($path, $http_url), or a reference to an array containing the same two arguments. Returns a URI::ToDisk if possible, or "undef" if one cannot be provided. uri The "uri" method gets and returns the current URI of the location, in string form. URI The capitalised "URI" method gets and returns a copy of the raw URI, held internally by the location. Note that only a copy is returned, and as such as safe to further modify yourself without effecting the location. path The "path" method returns the filesystem path componant of the location. catdir 'dir', 'dir', ... A File::Spec workalike, the "catdir" method acts in the same way as for File::Spec, modifying both componants of the location. The "catdir" method returns a new URI::ToDisk object representing the new location, or "undef" on error. catfile [ 'dir', ..., ] $file Like "catdir", the "catfile" method acts in the same was as for File::Spec, and returns a new URI::ToDisk object representing the file, or "undef" on error. TO DO
Add more File::Spec-y methods as needed. Ask if you need one. SUPPORT
Bugs should be reported via the CPAN bug tracker at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=URI-ToDisk> For other issues, or commercial enhancement or support, contact the author. AUTHORS
Adam Kennedy <http://ali.as/>, cpan@ali.as COPYRIGHT
Copyright (c) 2003 - 2006 Adam Kennedy. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. perl v5.10.1 2007-11-12 URI::ToDisk(3pm)
All times are GMT -4. The time now is 08:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy