Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto-inserting text using SED command

Status
Not open for further replies.

keithl

IS-IT--Management
Mar 7, 2001
12
MY
Hi,
I am trying to find a way to auto insert some lines of text into a text file at a specific location. Here is what i am trying to do-

Two lines of text to be inserted into file:-
[start]
original sender name

Location of file where the above text lines need to be inserted:-
1. From....
2. Return-path...
3. Received....
4.
5. ------=_NextPart....
6. Content-Type....
7. boundary....
8.
9. ------=_NextPart....
10. Content-Type....
11. charset....
12. Content-Transfer....
13.
14.
14+n.------=_NextPart.... <-To insert just above line
15+n.Content-Type

I would like to insert the 2 lines of text just above line# 14+n.

Appreciate all the help that I can get.
Thanks in advance.
KeithL
 

Hello, keithl!

If you have awk, you can use this command:

awk '{ print; if (NR == 14) { print &quot;[start]&quot;; print &quot;original sender name&quot; }}' file > newfile

This command adds 2 rows after 14. row of file (or, if you like so, before 14+n. row, suppose 15. row) and puts new contens in newfile.

Maybe this helps you or you get idea for sed.

Jesus loves you.

KP.
 
Hi krunek,
I could make use of the 'awk' command if I know exactly at which line# to be inserted. This problem is I don't. The text should be inserted just before the 3rd instance of the line '------=_NextPart' in the file.
Got any other ideas?

keithl
 
Oh, I see! Now has your problem better explanation.

You can try this awk script:


/------=_NextPart/ { counter ++ }

{ if (counter == 3) {
print &quot;[start]&quot;
print &quot;original sender name&quot;
print $0
}
else
print
}


I count occurrences of the pattern &quot;------=_NextPart&quot;. If counter is 3, I print &quot;start&quot;, &quot;orig. name&quot; and line, else print line.

KP.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top