How to add a line in every file with perl commandline?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add a line in every file with perl commandline?
# 1  
Old 03-04-2014
How to add a line in every file with perl commandline?

Hi, i want to add a line at the beginning of every file in a directory.
perl -i.bkp -p -e 'print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n" if $. == 1' *.reset

But this command is updating only the first file in the directory. I think this is because $. is not resetting for next file.

How to modify all the files.
# 2  
Old 03-04-2014
If you have GNU sed available you could do
Code:
sed -i '1i\
#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset' *

This User Gave Thanks to hergp For This Post:
# 3  
Old 03-04-2014
Code:
perl -i.bkp -p -e 'BEGIN { $p="" } if( $ARGV != p ) { print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n"; $p=$ARGV } ' *.reset

This User Gave Thanks to anbu23 For This Post:
# 4  
Old 03-04-2014
Bad practice(?) but seems to work:
Code:
perl -i.bkp -p -e 'print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n" if $. == 1; $.=0 if eof' *.reset

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. UNIX for Dummies Questions & Answers

Load config file at commandline

Hello, This would be very basic question to most of you, but for me as a newbie it is an un-know. I would like to load a configuration file into memory before executing a specific command. For example: I have a config file that holds all the database connectivity information and I'd like to... (5 Replies)
Discussion started by: babyPen1985
5 Replies

3. Shell Programming and Scripting

Quick Perl "s2p" Commandline Q . . .

Greetings! Looking to clear up a rather simple matter which seems to nonetheless elude me: How does one call Perl's s2p from the commandline? I've tried such as this:perl -e s2p '-i 's/first/last/g' ./somefile.txt'...to convert a similarly-formed GNU sedline over to the Perl equivalent (without... (2 Replies)
Discussion started by: LinQ
2 Replies

4. Shell Programming and Scripting

Perl split and add new line

Hi All, I find the below code printing the output of a particular field in same line, which i want it to be printed on a new line. ---CODE START--- foreach $defectRec (@outLs) { my($def_id,$status,$files_mod) = split(/,/, $defectRec); print "DEFECT ID: $def_id, Status:... (3 Replies)
Discussion started by: nmattam
3 Replies

5. Shell Programming and Scripting

How to add a line in a file using perl script?

Hi all, I want to search for a line in a file using perl script and add a line above that line.Can any one suggest me command or code to that using script. Thanks BHarath (5 Replies)
Discussion started by: bharathece
5 Replies

6. Shell Programming and Scripting

Help using perl commandline for XML matching

I have a file that contains this. <NAME>/bob</NAME> I'm trying to print just the /bob part to my screen. I have a command line example I really think should work. Keep in mind that the content between the <NAME> </NAME> is always changing. $/tmp> perl -ne 'print /<NAME>($.)<\/NAME>/'... (2 Replies)
Discussion started by: x96riley3
2 Replies

7. Shell Programming and Scripting

perl script to add a line into a file

hi all need a perl script to add a line into a file thanks with anticipation (2 Replies)
Discussion started by: karthikn7974
2 Replies

8. Shell Programming and Scripting

add to first line of file using perl

Hey guys, I have a text file full of data and all I want to do with the thing is add the file extension name to the first line of the file. Here is what I have so far. I can't remember how to append to the first line of the file without deleting the exsisting data. #!/usr/local/bin/perl ... (5 Replies)
Discussion started by: kingdbag
5 Replies

9. Shell Programming and Scripting

How to determine if a script (perl) was called from a CRON job or commandline

Hi, Is there a way to determine if a Script is called from a CRON job or from a commandline Gerry. (2 Replies)
Discussion started by: jerryMcguire
2 Replies

10. UNIX for Dummies Questions & Answers

trying to strip the first 4 char. of a file out via commandline

im kind alost. i beleave its a sed command. but i cant seem to find it in my book. can someone point me in the write direction. i know this is extreamly sloppy. but this is what i did untill i can figure out how to manipulate the filename namespace. an ls on the directory where this would run... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question