Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Single line insert to the start of a file

by darklord_999 (Acolyte)
on Aug 16, 2012 at 07:15 UTC ( [id://987703]=perlquestion: print w/replies, xml ) Need Help??

darklord_999 has asked for the wisdom of the Perl Monks concerning the following question:

I have a 2 files as below

File1:

HEADER XXXXX YYYYY ZZZZZ EOF

File2:

AAAAA BBBBB CCCCC DDDDD EOF

How can I get the first line from File1 and insert it into the first line of File2?File 2 ultimately should look like below:

HEADER AAAAA BBBBB CCCCC DDDDD EOF

Is there a single liner which can do this ?

Replies are listed 'Best First'.
Re: Single line insert to the start of a file
by aaron_baugher (Curate) on Aug 16, 2012 at 09:57 UTC

    Assuming a *nix system, and the existence of a real-world problem to be solved rather than a homework problem in a particular language:

    head -1 file1 >/tmp/file.$$; cat file2 >>/tmp/file.$$; mv /tmp/file.$$ file2

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re: Single line insert to the start of a file
by Anonymous Monk on Aug 16, 2012 at 07:18 UTC
Re: Single line insert to the start of a file
by cheekuperl (Monk) on Aug 16, 2012 at 07:38 UTC
    How big is your file? You can also try reading the file into an array, then unshift new entry to that array and write it back to the file :P
Re: Single line insert to the start of a file
by trizen (Hermit) on Aug 16, 2012 at 09:52 UTC
    perl -MTie::File -e'tie@a,"Tie::File","f2.txt";unshift@a,scalar<>'<f1.txt
Re: Single line insert to the start of a file
by kcott (Archbishop) on Aug 16, 2012 at 10:14 UTC

    I suspect any one-liner is going to be somewhat unwieldy. Here's one using Tie::File:

    ken@ganymede: ~/tmp $ cat > pm_add_header_1 HEADER XXXXX YYYYY ZZZZZ ken@ganymede: ~/tmp $ cat > pm_add_header_2 AAAAA BBBBB CCCCC DDDDD ken@ganymede: ~/tmp $ perl -Mstrict -Mwarnings -MTie::File -e 'my @f; my $i = 0; tie @{$f[ +$i++]}, q{Tie::File}, $_ or die $! for @ARGV; unshift @{$f[1]}, $f[0] +[0];' pm_add_header_1 pm_add_header_2 ken@ganymede: ~/tmp $ cat pm_add_header_2 HEADER AAAAA BBBBB CCCCC DDDDD ken@ganymede: ~/tmp $

    -- Ken

Re: Single line insert to the start of a file
by aitap (Curate) on Aug 16, 2012 at 08:24 UTC
    Use -p flag to run your code in the loop:
    LINE: while (<>) { ... # your program goes here } continue { print or die "-p destination: $!\n"; }
    and -i flag to edit files in-place (see perlrun for more). Use $. to check the line number. Define BEGIN{} to read the header, then write it in the main loop:
    perl -pi -e'BEGIN{$HEAD=shift@ARGV;open(HEAD);$main::head=<HEAD>}$_=$m +ain::head.$_ if $.==1' header-file file-to-be-edited
    Perhaps avoiding the -p will be more efficient, but not so compact:
    perl -e'($HEAD,$FILE)=@ARGV;open(HEAD);open(FILE);open($f,">$FILE.$$") +;print $f scalar <HEAD>,<FILE>;rename "$FILE.$$","$FILE"' header-file + file-to-be-edited
    Warning: both of the one-liners use somewhat deprecated one-argument open and barewords as filehandles.
    Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://987703]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-19 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found