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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to enter a filename in each line
by johngg (Canon) on Sep 11, 2008 at 13:43 UTC
    Do you mean something like this?

    [johngg@ovs276 perl]$ cat abc.txt test text line 1 test text line 2 test text line 3 [johngg@ovs276 perl]$ perl -i.BAK -pe 's/$/,$ARGV/;' abc.txt [johngg@ovs276 perl]$ cat abc.txt test text line 1,abc.txt test text line 2,abc.txt test text line 3,abc.txt [johngg@ovs276 perl]$ cat abc.txt.BAK test text line 1 test text line 2 test text line 3 [johngg@ovs276 perl]$

    I hope I have guessed right and this is of some use.

    Cheers,

    JohnGG

      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().
        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

Re: How to enter a filename in each line
by moritz (Cardinal) on Sep 11, 2008 at 12:04 UTC
    It would be much easier for us if we could see where a line ends. See Writeup Formatting Tips on how you can format your post.

    Also show some effort, ie tell us what you have tried so far to achieve your goal.

Re: How to enter a filename in each line
by eighty-one (Curate) on Sep 11, 2008 at 13:05 UTC
    In addition to the suggestions moritz gave, it would be useful to know where the filenames come from, and what kind of format they're in before you need to print them.