943,635 Members | Top Members by Rank

Ad:
May 19th, 2009
0

Inserting Lines Into A File

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darnoc is offline Offline
3 posts
since May 2009
May 20th, 2009
0

Re: Inserting Lines Into A File

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
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008
May 20th, 2009
0

Re: Inserting Lines Into A File

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) $
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darnoc is offline Offline
3 posts
since May 2009
May 22nd, 2009
0

Re: Inserting Lines Into A File

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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
May 23rd, 2009
1

Re: Inserting Lines Into A File

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
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
May 25th, 2009
0

Re: Inserting Lines Into A File

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darnoc is offline Offline
3 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Help with basic shell script
Next Thread in Shell Scripting Forum Timeline: UNIX Shell Scripting: Problem with "grep" command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC