inserting a string to a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting inserting a string to a text file
# 1  
Old 05-21-2009
inserting a string to a text file

Hello

Can somebody please help me with the following script?
I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it doesn't work.
Can somebody please tell me what am I doing wrong?

Thanks a lot

Code:
system ("touch /home/yanni/scripts/intxtfile"); 
my ($fileread, $filein, $fileout);
$filein = '/home/yanni/scripts/intxtfile';
$fileout = 'file.tmp';


open $fileread, '>', $filein || die ("Can't open: $filein $!");
print $fileread "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n"; 
close $fileread;

 open ($fileread, '<', $filein)  || die "Can't open: $filein $!";
 open ($filewrite,'>', $fileout) || die "Can't open: $fileout $!";
 
 while (<$fileread>) {
                      print $filewrite $_;
                      if ($.==2) { print $fileread "blablabla";}

                             }              

close ($fileread);
close ($filein);
close ($fileout);
rename ($fileout, $filein) || die "Unable to rename: $!";

# 2  
Old 05-21-2009
don't understand what you are doing. If you want to insert something on second line, then do it while you are creating the blank lines. also, there is no need to call system() to touch a file. Perl has its own touch. see perldoc -f touch
# 3  
Old 05-21-2009
I agree with ghostdog, your script seems to be poorly written and your requirements are vague, but maybe that is because you lack perl experience. But one problem is clearly evident and you should see it assuming you wrote the code you posted:

Code:
if ($.==2) { print $fileread "blablabla";}

In the above line you are trying to print to $fileread, which is a file opened for reading, not writing.
# 4  
Old 05-21-2009
Quote:
Originally Posted by goude
...I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it doesn't work.
...
Not sure about your code, but is this what you are looking for ?

Code:
$
$ perl -e '{open(OUT,">file.tmp"); print OUT "\nBLABLABLA\n"."\n"x18; close(OUT)}'
$
$ cat -n file.tmp
     1
     2  BLABLABLA
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
$
$

tyler_durden
# 5  
Old 05-22-2009
Thank you all for your replies

To KevinADC: I don't understand why you said "the file is opened for reading not for writing". I thought the ">" symbol means write to file, isn't it?. You confused me there.

Code:
 open ($fileread, '<', $filein)  || die "Can't open: $filein $!"; 
 open ($filewrite,'>', $fileout) || die "Can't open: $fileout $!";

To Tyler: Your perl line is brilliant. That's exactly what I'm looking for but I'd rather do that in a Perl script. I'm long way from writing Perl liners. Smilie

Last edited by goude; 05-22-2009 at 05:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Inserting information from old text file into a new text file

Hello, I'm trying to take information from a list of hundreds of subject ids (where each line is a new subject id), and insert each line into a new text file that contains the pathnames for each subject. To clarify, all subject have a similiar path name (e.g., C:\data\SUBJECT_ID\) that contains... (4 Replies)
Discussion started by: invisibledwarf
4 Replies

2. Shell Programming and Scripting

Inserting text in file names while copying them.

I'm trying to find a Bourne shell script that will copy files from one directory using a wild card for the file name (*) and add some more characters in the middle of the file name as it is copied. As an example: /u01/tmp-file1.xml => /u02/tmp-file1-20130620.xml /u01/tmp-file2.xml => ... (6 Replies)
Discussion started by: Tony Keller
6 Replies

3. UNIX for Dummies Questions & Answers

Inserting text into a file with awk or sed

Hello, I've been trying to get a script working that fetches weather-data and converts it into an .ics file. The script works so far put I'm stuck at the point where I need to add specific static data. A thorough search through the forum did not point me into the right direction. #!/bin/bash... (3 Replies)
Discussion started by: Schubi
3 Replies

4. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

5. Shell Programming and Scripting

Inserting text into a new file

Hi all, I want to create a file and then insert some text into it. I'm trying to create a .sh script that will create a new python file from a template. Can someone tell me why this won't work, touch $1 | sed -e '1i\Some test code here' Sorry I'm quite new to all this! Just as a side... (3 Replies)
Discussion started by: ahodgson
3 Replies

6. UNIX for Dummies Questions & Answers

Inserting a column into a text file

I have a tab delimited text file with multiple columns (data.txt). I would like to insert a column into the text file. The column I want to insert is in a text file (column.txt). I want to insert it into the 5th column of data.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

Inserting text and modifying the file

I am in a dire need of doing this job , please help from shell script or perl script. It will be highly appreciated. Please have a look at the following INPUT file; The first 14 rows are not of interest but I want them to be included in the output file as they are. From the row 14... (3 Replies)
Discussion started by: digipak
3 Replies

8. Shell Programming and Scripting

inserting subscriber no in text file using KSH....

:confused:Dears , I have text file I need to insert the subscriber number at position 32, and need to keep the next field at position 53 (no increasing of the record lenght), I mean I just want to replace the spaces at position 32 with subscirber number . for example A B A ... (1 Reply)
Discussion started by: atiato
1 Replies

9. Shell Programming and Scripting

Inserting a String in a file header.

Dear all, I have a file created in the name sample.txt in UNIX with header and footer. How to insert a required string (for example "FILE1") in the header part after the file has been created. What kind of command can i use to do the same. Thanks in advance Hari (3 Replies)
Discussion started by: Hari123
3 Replies

10. Shell Programming and Scripting

inserting a String in the file(s)

Hi, I'm a newbee to Unix shell scripting. I want to write a shell script that inserts a new String(name&value pair) into a file(s) at a particular place.I willl have to write one script which when executed should insert a new variable in all the files in that particular directory. Say for eg:... (4 Replies)
Discussion started by: 2tbee
4 Replies
Login or Register to Ask a Question