add to first line of file using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add to first line of file using perl
# 1  
Old 11-06-2006
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

#Dir where all host files are kept.
$dir = '/export/home/joe/scripts/testfiles/';

#Pull all files in dir to array.
opendir(DIR, "$dir") || die("Cannot open directory");
@dir = readdir(DIR);
closedir(DIR);

for $file(<@dir>) {
if ($file =~ /insertline\.(.*$)/) {

$addtofile = uc("!$1\n");
$modfile = "$dir$file";

open(FILE, $modfile) || die("Cannot open file");
#insert $addtofile var here but do it at line 1
#UGH!! Smilie
}
}
close FILE;
exit (0);
# 2  
Old 11-06-2006
Does it have to be perl?
Code:
#!/bin/ksh
a=file.txt
echo "${a#file.}" > tmp.tmp
cat "$a" >> tmp.tmp
mv tmp.tmp $file

# 3  
Old 11-06-2006
Yea,

I'd like to add it to my existing script I have that generates my host files for me.
# 4  
Old 11-06-2006
If you're not too concerned about the kludge, you could just embed that shell stuff into the perl script, and get the same effect.
# 5  
Old 11-06-2006
I suppose, but is it really that hard to do it in perl? I though I have done it before with a seek command or something but im drawing a blank.
# 6  
Old 11-06-2006
EXAMPLE: In case anyone needs to do something similar here is what I made. I will go back and change some var names so its more readable but you get the idea. I'd still like to know how to write to the first line of the file without having to make a duplicate using perl. If any one knows that be cool please post.

$ cat insertline.pl
#!/usr/local/bin/perl

#Dir where all host files are kept.
$dir1 = '/export/home/joe/scripts/testfiles/';
$dir2 = '/export/home/joe/scripts/testfiles/finalchanges/';

#Pull all files in dir to array.
opendir(DIR1, "$dir1") || die("Cannot open directory");
@dir1 = readdir(DIR1);
closedir(DIR1);

for $file(<@dir1>) {
if ($file =~ /insertline\.(.*$)/) {
$addtofile = uc("!$1\n");
$modfile1 = "$dir1$file";
$modfile2 = "$dir2$file";

open(FILE1, "$modfile1") || die("Cannot open file");
open(FILE2, ">$modfile2") || die("Cannot open file");
print FILE2 "$addtofile";
@copy_this = <FILE1>;
print FILE2 (@copy_this);
close (FILE1);
close (FILE2);
}
}
exit (0);

I guess I could dump thee current text file to an array like this and just write over the existing file instead of making a second file. I thought there was a cleaner way of doing it but whatever haha Smilie

Last edited by kingdbag; 11-07-2006 at 03:15 PM..
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. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Add specific string to last field of each line in perl based on value

I am trying to add a condition to the below perl that will capture the GTtag and place a specific string in the last field of each line. The problem is that the GT value used is not right after the tag rather it is a few fields away. The values should always be 0/1 or 1/2 and are in bold in the... (12 Replies)
Discussion started by: cmccabe
12 Replies

4. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: twistedpair
3 Replies

5. 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

6. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

7. Programming

Perl find text and add line

Hi All I need to add a line to a file but after a certain block of text is found The block of text looks like this <RDF:Description RDF:about="urn:mimetype:video/quicktime" NC:value="video/quicktime" and i need to add this in the next line down ( note there is... (4 Replies)
Discussion started by: ab52
4 Replies

8. 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

9. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

10. 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
Login or Register to Ask a Question