in reply to Re: How to enter a filename in each line
in thread How to enter a filename in each line

Like johngg, assuming that the filename argument is abc.txt and you wish to print out its contents with each line ending with ", abc.txt\n", here's another way:
perl -pe 'BEGIN { $\ = ", $ARGV[0]\n" } chomp' abc.txt

This merely changes the output line terminator (originally "\n") for the implicit print().

Replies are listed 'Best First'.
Re^3: How to enter a filename in each line
by johngg (Canon) on Sep 12, 2008 at 18:36 UTC
    That method works for a change to a single file but goes a bit pear-shaped if dealing with multiple files. Consider the following.

    [johngg@ovs276 perl]$ ls abc.txt def.txt [johngg@ovs276 perl]$ head -99 * ==> abc.txt <== test text line 1 test text line 2 test text line 3 ==> def.txt <== test text line 1 test text line 2 test text line 3 [johngg@ovs276 perl]$ perl -i.BAK -pe 'BEGIN { $\ = ",$ARGV[0]\n" } ch +omp' *.txt [johngg@ovs276 perl]$ ls abc.txt abc.txt.BAK def.txt def.txt.BAK [johngg@ovs276 perl]$ head -99 * ==> abc.txt <== test text line 1,abc.txt test text line 2,abc.txt test text line 3,abc.txt ==> abc.txt.BAK <== test text line 1 test text line 2 test text line 3 ==> def.txt <== test text line 1,abc.txt test text line 2,abc.txt test text line 3,abc.txt ==> def.txt.BAK <== test text line 1 test text line 2 test text line 3 [johngg@ovs276 perl]$ [johngg@ovs276 perl]$ [johngg@ovs276 perl]$ mv abc.txt.BAK abc.txt [johngg@ovs276 perl]$ mv def.txt.BAK def.txt [johngg@ovs276 perl]$ ls abc.txt def.txt [johngg@ovs276 perl]$ head -99 * ==> abc.txt <== test text line 1 test text line 2 test text line 3 ==> def.txt <== test text line 1 test text line 2 test text line 3 [johngg@ovs276 perl]$ perl -i.BAK -pe 's/$/,$ARGV/;' *.txt [johngg@ovs276 perl]$ ls abc.txt abc.txt.BAK def.txt def.txt.BAK [johngg@ovs276 perl]$ head -99 * ==> abc.txt <== test text line 1,abc.txt test text line 2,abc.txt test text line 3,abc.txt ==> abc.txt.BAK <== test text line 1 test text line 2 test text line 3 ==> def.txt <== test text line 1,def.txt test text line 2,def.txt test text line 3,def.txt ==> def.txt.BAK <== test text line 1 test text line 2 test text line 3 [johngg@ovs276 perl]$

    Note how with your method the second file (and any subsequent files) is annotated the name of the first file processed, which was in the first element of @ARGV. Have a look at $ARGV vs. @ARGV in perlvar.

    Cheers,

    JohnGG

      Yes, I realized that when I provided the quick-and-dirty solution. With that, I merely wanted to point out the alternative way of changing the output terminator, especially when printing output is of concern.

      Beyond the OP, we really can't tell how thorough a solution is required. For instance, we had to assume that abc.txt was the filename in itself and not part of the file contents.
      Bundle of thanks to JohnGG, I get what I want. I am not best in perl but now trying to learn steadily form people like you. Can you give some clue how i can do this other than on command line. Regards, Imran
        #!/usr/bin/perl use warnings; use strict; $^I = '.BAK'; while ( <> ) { s/$/,$ARGV/; print; } __END__