in reply to Re^2: How to enter a filename in each line
in thread How to enter a filename in each line
[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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: How to enter a filename in each line
by repellent (Priest) on Sep 12, 2008 at 22:59 UTC | |
|
Re^4: How to enter a filename in each line
by imran77 (Initiate) on Sep 15, 2008 at 05:16 UTC | |
by jwkrahn (Abbot) on Sep 15, 2008 at 05:36 UTC |