Insert lines at specific location in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert lines at specific location in file
# 1  
Old 03-28-2007
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)

Now, I know how to do this by grep'ing tailing, appending etc using temporary files to put stuff in, but I wanted to do it in one simple command rather than 5 or 6 commands with other temp files involved that I have to remove afterwards (it looks very messy)

Is there any way to do this simply ????


Code:
stuff
stuff
stuff
<NEED TO ENTER LINE HERE>
}

# 2  
Old 03-28-2007
Code:
ed file<<!
-1a
my new line
.
w
q
!


Last edited by radoulov; 03-28-2007 at 06:48 AM..
# 3  
Old 03-28-2007
Try this...

Code:
sed '$ i this is  line you want to add' filename

Output:

u142115@linux2alm:~/aps/aps4/product/den> cat sc.txt
stuff
stuff
stuff
}
u142115@linux2alm:~/aps/aps4/product/den> sed '$ i this is the line' sc.txt
stuff
stuff
stuff
this is the line
}
# 4  
Old 03-28-2007
Check out http://tldp.org/LDP/abs/html/here-docs.html

In exaple 18-2 a file is opend with vi and some lines are added. You could check for the last } in your file and then use the O to insert lines above this one.

If you always know that your } is on the last line (without trailing newline) you can also use G (jumping to the end of the file) and the use O to insert above this line.

The content as well the vi commands are embedded in the script as a so called here document as described in the example.
# 5  
Old 03-28-2007
# try this
# inserting the string "after 10" after the line beginning with 10

'/^10 /i\
after 10' file
# 6  
Old 03-28-2007
Quote:
Originally Posted by jacoden
Try this...

Code:
sed '$ i this is  line you want to add' filename

Output:

u142115@linux2alm:~/aps/aps4/product/den> cat sc.txt
stuff
stuff
stuff
}
u142115@linux2alm:~/aps/aps4/product/den> sed '$ i this is the line' sc.txt
stuff
stuff
stuff
this is the line
}

when I run this I get

Code:
[my.server] # sed '$ i this is the line' testfile
sed: command garbled: $ i this is the line
[my.server] #

# 7  
Old 03-28-2007
Give an attempt on this... All works well here...

Code:
x="this is the line"
sed '$ i '"$x"'' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Possible to insert a few lines of code into a file at a specific point?

Hi Folks - How would I go about inserting a chunk of lines (3) into a specific portion of a file? The background is I have a script (non shell) that it executed daily, however on Sundays, I uncomment a section of code so that piece can be run as well. So I was hoping to write a piece of... (9 Replies)
Discussion started by: SIMMS7400
9 Replies

2. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

3. Shell Programming and Scripting

Find and Copy file of specific location

Dear All, I need to transfer all files present in one location to another but those files should be of specific extension like. Find and copy all files of extension .xls, .pdf, .txt from location usr/tmp to location /per/Treat (6 Replies)
Discussion started by: yadavricky
6 Replies

4. Shell Programming and Scripting

Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines. I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it. There are two lines as one Header and one trailer which should remain same. Can somebody... (5 Replies)
Discussion started by: mohit kanoongo
5 Replies

5. Shell Programming and Scripting

Insert charactera in 1st position of specific lines using vi editor or sed command

Dear all, i am having text file like below surya rama ranga laxman rajesh reddy i want add string (OK) before a text from line 3 to 5 the result will be surya rama OK ranga OK laxman OK rajesh reddy (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Shell Programming and Scripting

Insert text line to specific location CSV

In Perl. ***edited question below*** Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file: age,name,locationIs it possible to INSERT data into the CSV into the correct age order. For example, if I had... (1 Reply)
Discussion started by: whyte_rhyno
1 Replies

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

8. Shell Programming and Scripting

insert data into specific lines of a CSV

So I work in a 1 to 1 laptop deployment and sometimes we need to mass order parts. The vendor will send us a text file and we have to manually input serial numbers. Well I have a full blown web based inventory system which I can pull serial number reports from. I then have to input the part... (4 Replies)
Discussion started by: tlarkin
4 Replies

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

10. Shell Programming and Scripting

Remove duplicates from File from specific location

How can i remove the duplicate lines from a file, for example sample123456Sample testing123456testing XXXXX131323XXXXX YYYYY423432YYYYY fsdfdsf123456gsdfdsd all the duplicates from column 6-12 , must be deleted. I want to consider the first row, if same comes in the given range i want to... (1 Reply)
Discussion started by: gopikgunda
1 Replies
Login or Register to Ask a Question