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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED Question

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I want to add lines of text at the top of several files. The text to add:

@LABEL BEGIN
@SET LOGFILE=("/home/bham_mta/rehome_scripts/cmd_file1")
@LOG(LOGFILE)
@SET SWITCH="BH1BSC1"
@CONNECT (SWITCH)

To be added to the top of each file found named rxmop_input*

I tried:

sed '1i@LABEL BEGIN
@SET LOGFILE=("/home/bham_mta/rehome_scripts/cmd_file1")
@LOG(LOGFILE)
@SET SWITCH="BH1BSC1"
@CONNECT (SWITCH)
'
rxmop_input*

But obviously that didnt work. I cant even remember where I got that from. Please guide me....

Beaster
 
Create a file (e.g. master.txt) with the lines you want to insert at top.

Create a script with the following instrctions :

cat master.txt original_file > /tmp/filetemp
mv /tmp/filetemp original_file

There are surely better ways to do the same work, but this is simple and will work.

Erminio
 
Will this still work if there are multiple files for example :

rxmop_input1
rxmop_input2
rxmop_input3
rxmop_input4

I need the file snames to stay the same, but with the added text from above.
 
Create a file (e.g. master.txt) with the lines you want to insert at top.

Create a script with the following instructions :

cat master.txt rxmop_input > /tmp/filetemp
mv /tmp/filetemp rxmop_input

There are surely better ways to do the same work, but this is simple and will work.
Of course you'll need to refine the script so you can do the work to all file rxmop_input* in a batch.
(Maybe using find -exec)

Erminio
 
Sorry for my mistake; I sent my answer before I finished it.
Of course you can do the same work with all files you need.
If they are in a limited number and you know them in advance,
you can simply duplicate the command lines for each file.
hi
 
#!/bin/ksh

for file in rxmop_input*
do
ex -s ${file} <<EOF
1i
My header lines go here
More headers
EVEN more headers
.
w
x
EOF
done; vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
vlad,
That worked great, if I wanted to add it at the end of the file, I know to use:

a

but I dont know how many lines will be in the file, so how would I get it to add it at the end?
 
here's the 'end' version:

#!/bin/ksh

for file in rxmop_input*
do
ex -s ${file} <<EOF
\$a
headers
more headers
MORE headers
.
w
x
EOF
done;
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
vlad,
I tried it with just &quot;a&quot; and it worked ok. I will use yours though, because I am very sure it is more reliable.

Beaster
 
vlad,
With ex can I add text to every line in the file using the above by specifying 1,$ ??

My ex is very rusty. I have some other text I would like to add to every line using the above ex script. This is an awesome bit of script I can use later next week.
 
yes, 'ex' is very much vi/ed/sed family. If you're ok with sed/regex, 'ex' should be easy. From 'man ex':

DESCRIPTION
The ex utility is the root of a family of editors: ex and
vi . ex is a superset of ed(1), with the most notable
extension being a display editing facility. Display based
editing is the focus of vi .


BTW, there's a shortcut for '1,$' - '%' vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
vlad that was good reading, I printed it out and will have some use for it next week on a cell site plan I am doing.

I have played with it for a bit now and realize it is not what I need for the next part of my script.

I just need to insert with sed the following text at the beginning of each line and keep the same filename rxmop_input1, 2, 3, and so on as usual depending on how many files there are:

Text to insert:
rxmop:mo=


I tried to play with ex to do it, but it seems I cannot insert text at the begining of each line. Plus it does not seem like that is the best utilization when sed is available.
 
how 'bout that?

#!/bin/ksh

for file in rxmop_input*
do
ex -s ${file} <<EOF
%s/^/rxmop:mo=/
.
w
x
EOF
done;
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top