| | |
Inserting Lines Into A File
![]() |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2008
Posts: 95
Reputation:
Solved Threads: 5
One way is to use an ed script. For example
To make the change in your file, do the following
Shell Scripting Syntax (Toggle Plain Text)
$ cat ed.script /ssl-qop-mgmt-default/a default = DES-168 default = RC2-128 default = RC4-128 #default = ALL . +1d wq $
Shell Scripting Syntax (Toggle Plain Text)
ed file < ed.script
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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) $
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) $
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
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: 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.
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)
od -c ed.script
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
use awk
Shell Scripting Syntax (Toggle Plain Text)
awk '/default = ALL/{ print "default = DES-168" print "default = RC2-128" print "default = RC4-128" print "#"$0 next}1' file
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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\
'
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.
![]() |
Similar Threads
- how to retrieve some specific set of lines from a file and store it in a char buffer. (C)
- read lines from file which gets updated at same time (C)
- Need to print certain lines from a file (Shell Scripting)
- How to delete last three lines in file (Shell Scripting)
- How can I delete empty lines from file? (Java)
- reading lines from file (C++)
- how to delete lines of a file in java? (Java)
- Find the total lines in the file (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: Help with basic shell script
- Next Thread: UNIX Shell Scripting: Problem with "grep" command
| Thread Tools | Search this Thread |





