insert multiple lines into a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert multiple lines into a file
# 8  
Old 05-18-2008
It prints the whole file and adds a new line 980 if it gets that far.
# 9  
Old 05-18-2008
Hi Era,

I think 980 line will be as it is ,

The content we entered will be 981 line
# 10  
Old 05-18-2008
Yes, sorry for being inexact. The point I wanted to make was actually that if the file has less than 980 lines, it will do nothing.
# 11  
Old 05-18-2008
Quote:
U can know the difference between the two files and also how sed works.
Yes I know the difference between 2 files, but I don't know how sed works Smilie ..
Quote:
The point I wanted to make was actually that if the file has less than 980 lines, it will do nothing
is it possible that it writes to a line number 981 but the file has only 980 lines up to now.. without printing the whole file if that can be avoid it would be awesome and it doesn't have to be sed .. whatever does the trick is good for me awk, sed, cat, head whatever :P

Thank you
# 12  
Old 09-04-2008
Java you can do it in perl

find here below a perl script to do what you want

print "\n Enter the line number you want to enter the data to\n";
$line = <STDIN>;
chop($line);
print "\nEnter the text to be inserted\n";
$word = <STDIN>;
chop($word);
open (OUT,">outputfile");
open (IFILE,"inputfile");
$count = 2;
while (<IFILE>) {
chop($_);
print OUT ("$_\n");
if($count == $line){
print OUT ("$word\n");
}
$count++;
}
close INFILE;
close OUT;
# 13  
Old 09-04-2008
find here below a perl script to do what you want

print "\n Enter the line number you want to enter the data to\n";
$line = <STDIN>;
chop($line);
print "\nEnter the text to be inserted\n";
$word = <STDIN>;
chop($word);
open (OUT,">outputfile");
open (IFILE,"inputfile");
$count = 2;
while (<IFILE>) {
chop($_);
print OUT ("$_\n");
if($count == $line){
print OUT ("$word\n");
}
$count++;
}
close INFILE;
close OUT;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Want to Insert few lines which are stored in some file before a pattern in another file

Hello, I have few lines to be inserted in file_lines_to_insert. In another file final_file, I have to add lines from above file file_lines_to_insert before a particular pattern. e.g. $ cat file_lines_to_insert => contents are abc def lkj In another file final_file, before a... (6 Replies)
Discussion started by: nehashine
6 Replies

3. Shell Programming and Scripting

Insert few lines above a match using sed, and within a perl file.

Greetings all, I am trying to match a string, and after that insert a few lines above that match. The string is "Version 1.0.0". I need to insert a few lines ONLY above the first match (there are many Version numbers in the file). The rest of the matches must be ignored. The lines I need to... (2 Replies)
Discussion started by: nagaraj s
2 Replies

4. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

5. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

6. Shell Programming and Scripting

split row into lines and insert file name

I have a directory with several hundred files. The file format is a space delimited row with an unknown number of columns: A B C D E F G ... I need to turn this format File1 A File1 B File2 A File3 A File3 B File3 C ... I can use grep to display the filename next to each row of... (2 Replies)
Discussion started by: newreverie
2 Replies

7. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

8. Shell Programming and Scripting

Insert 2 lines in a file at a specific location

Hi, I need to insert two new lines in a file: The file: "..... ...... ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`" .... .... " I need to add the lines: LD_LIBRARY_PATH='$LD_LIBRARY_PATH:$APACHE_HOME/modules' DOWNLOADMODULE_CONF_PATHNAME='$APACHE_HOME/conf/DWLModule.cfg' right... (2 Replies)
Discussion started by: potro
2 Replies

9. Shell Programming and Scripting

(sed) parsing insert statement column that crosses multiple lines

I have a file with a set of insert statements some of which have a single column value that crosses multiple lines causing the statement to fail in sql*plue. Can someone help me with a sed script to replace the new lines with chr(10)? here is an example: insert into mytable(id, field1, field2)... (3 Replies)
Discussion started by: jjordan
3 Replies

10. Shell Programming and Scripting

Insert lines at specific location in file

Hi There I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket) ... (17 Replies)
Discussion started by: hcclnoodles
17 Replies
Login or Register to Ask a Question