Inserting Lines Into A File

Reply

Join Date: May 2009
Posts: 3
Reputation: darnoc is an unknown quantity at this point 
Solved Threads: 0
darnoc darnoc is offline Offline
Newbie Poster

Inserting Lines Into A File

 
0
  #1
May 19th, 2009
Hi,

I am having trouble inserting lines into a file via a Unix shell script, can anyone out there help me with this?

Part of my file content looks like this:

# default ssl qop
[ssl-qop-mgmt-default]
default = ALL

I now need this part to read as follows:

# default ssl qop
[ssl-qop-mgmt-default]
default = DES-168
default = RC2-128
default = RC4-128
#default = ALL
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 95
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 5
fpmurphy fpmurphy is offline Offline
Junior Poster in Training

Re: Inserting Lines Into A File

 
0
  #2
May 20th, 2009
One way is to use an ed script. For example
Shell Scripting Syntax (Toggle Plain Text)
  1. $ cat ed.script
  2. /ssl-qop-mgmt-default/a
  3. default = DES-168
  4. default = RC2-128
  5. default = RC4-128
  6. #default = ALL
  7. .
  8. +1d
  9. wq
  10. $
To make the change in your file, do the following
Shell Scripting Syntax (Toggle Plain Text)
  1. ed file < ed.script
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: darnoc is an unknown quantity at this point 
Solved Threads: 0
darnoc darnoc is offline Offline
Newbie Poster

Re: Inserting Lines Into A File

 
0
  #3
May 20th, 2009
Hi,
I attempted your solution and it did not work, or maybe I ran it wrong:

(/home/cap) $ cat ed.script
/ssl-qop-mgmt-default/a
default = DES-168
default = RC2-128
default = RC4-128
#default = ALL
.
+1d
wq

(/home/cap) $ cat tempfile
# this is a test file

# Enable/Disable SSL Quality of Protection management
ssl-qop-mgmt = no


# Legal cipher values for qop in the following stanzas are:
# NONE, ALL, NULL, DES-56, FIPS-DES-56, DES-168, FIPS-DES-168,
# RC2-40, RC2-128, RC4-40, RC4-56, RC4-128, AES-128, AES-256
#
# Notes:
# - NONE = No SSL connection allowed.

# default ssl qop
[ssl-qop-mgmt-default]
default = ALL

# Notes:
# - NONE = No FTP connection allowed.

(/home/cap) $ ed tempfile < ed.script
386
?
?

(/home/cap) $
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: Inserting Lines Into A File

 
0
  #4
May 22nd, 2009
Check your ed script. I'll bet there is at least one space at the beginning of most of the lines, which happens when one copy/pastes from a web browser.

The ed() commands should be at the beginning of the line. The solo '.' that indicates the end of new data must be the only character on its line.

Oh, perhaps we are incorrectly assuming you are using UNIX/Linux for this. If using Winders, your editor might be putting other characters in the ed script, like a <CR> at the end of each line. To see invisible characters that might be screwing up the ed script, try:
Shell Scripting Syntax (Toggle Plain Text)
  1. od -c ed.script
You should see exactly the characters you entered, '\n' for the new lines and blanks for spaces. If you see any '\r's, this might explain your trouble.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Inserting Lines Into A File

 
1
  #5
May 23rd, 2009
use awk
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '/default = ALL/{
  2. print "default = DES-168"
  3. print "default = RC2-128"
  4. print "default = RC4-128"
  5. print "#"$0
  6. next}1' file
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: darnoc is an unknown quantity at this point 
Solved Threads: 0
darnoc darnoc is offline Offline
Newbie Poster

Re: Inserting Lines Into A File

 
0
  #6
May 25th, 2009
I am using Unix, AIX to be exact. Played with that ed.script and got nowhere, do not know where my troubles layed so I skipped it.

Fiddled with "sed" over the weekend and got it working. Created 2 scripts. "Script A" creates a copy of the file(s) and then edits the original while calling "Script B" to make the changes. Could not figure out how to make it all one script but it does the trick. Hope this helps someone else who may have been having the same troubles:

Script A
======
today=`date +"%m-%d-%y-%T"`

ls *.conf > conf.out
cat conf.out | while read data
do
`cp $data BKP/$data."$today"`
`ScriptB < $data > $data.newone`
`cp $data.newone $data`
done

rm -f *.newone

Script B
======
sed '
/ssl-qop-mgmt = no/ c\
ssl-qop-mgmt = yes\

/default = ALL/ c\
default = DES-168\
default = RC2-128\
default = RC4-128\
default = AES-128\
default = AES-256\
\#\default = ALL\
'
Last edited by darnoc; May 25th, 2009 at 11:38 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC