sed add line to config file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed add line to config file
# 1  
Old 03-01-2018
sed add line to config file

what is the sed command line to add a line to a config file

config file name is "config"

line to be added

cpuid.7.edx = "----:00--:----:----:----:----:----:----"


thanks
# 2  
Old 03-01-2018
use acho and append >>
# 3  
Old 03-01-2018
gnu?
Code:
sed -i '$ a cpuid.7.edx = "----:00--:----:----:----:----:----:----"' config

# 4  
Old 03-01-2018
You can use this "replace_append" script:
Code:
#!/bin/sh
searchkey=$1
shift
sep=$1
shift
replaceval=$1
shift
sed '
# look for the searchkey
  /^'"$searchkey$sep"'/ {
# replace the value
    s#'"$sep"'.*#'"$sep$replaceval"'#
# copy the rest of the file
    :L
    n
    bL
  }
# not found? Append a line
  $a\
'"$searchkey$sep$replaceval"'
' "$@"

And run it with
Code:
./replace_append "puid.7.edx" " = " '"----:00--:----:----:----:----:----:----"' config > config.new

If config.new looks good then copy it back to config.

The script looks for the key, if it matches then it changes the value.
If it does not find the key then it appends a line.

If you have GNU sed then you can add a -i.bak option to sed in the script. Then sed will modify the original file and leave a .bak file. (And you don't need to redirect the output.)
# 5  
Old 03-01-2018
Quote:
Originally Posted by tdubb123
what is the sed command line to add a line to a config file

config file name is "config"

line to be added

cpuid.7.edx = "----:00--:----:----:----:----:----:----"


thanks
If all you need is to append, sed is not necessary.
If you need to insert, before or after a specific line, sed is not necessary.
If you need to insert before or after an specific pattern in a line, you could use sed, but other ways might work better.

Could you post an example representation of your desired output?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to add a line to a file

Hi, I am trying to add a line to a file using sed. tmp1: aaaa Hello bbbb Hello I need to add "testing" after the first match of Hello. So the output should be aaaa Hello testing bbbb Hello and the line to be added will be a variable (2 Replies)
Discussion started by: giri_luck
2 Replies

2. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

3. Shell Programming and Scripting

uncomment or comment one specific line in a config file

Hello. I want comment or uncomment a ligne in a config file. The file name : /etc/samba/smb.conf Normaly the ligne is uncomment :so the line begin with a tab character followed by passdb backend =\tpassdb backend = In that case I should comment this line ... (2 Replies)
Discussion started by: jcdole
2 Replies

4. Shell Programming and Scripting

sed add after line x new text from file

I've been playing with sed, trying to get it to insert the contents of somefile.txt after line 13 on anotherfile.txt. I tried searching for a line with regex and attempting to insert something on the next line with: find ./anotherfile.txt -type f -exec sed -i -e '/^dog/cat/' {} \; but it... (2 Replies)
Discussion started by: unclecameron
2 Replies

5. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

6. Shell Programming and Scripting

sed to add a new line

Hi In my sed version the interactive method of adding a new works : > sed '3a\ new line ' file_name But i want to do the same task in one command as it is a part of a script. i hav tried the following but no luck so far cat file_name |sed -e '3a\ new line ' cat file_name |sed -e... (4 Replies)
Discussion started by: ningy
4 Replies

7. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

8. Shell Programming and Scripting

sed command to parse Apache config file

Hi there, am trying to parse an Apache 'server' config file. A snippet of the config file is shown below: ..... ProxyPassReverse /foo http://foo.example.com/bar ..... ..... RewriteRule ^/(.*) http://www.example.com/$1 RewriteRule /redirect https://www.example1.com/$1 ........ (7 Replies)
Discussion started by: jy2k7ca
7 Replies

9. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

10. Shell Programming and Scripting

add new line using SED

Hi, I want to add two new lines to a file. I have: dn: uid=beele,ou=medewerker,dc=hva,dc=nl street: Wibautstraat 2-4 dn: uid=beelx,ou=medewerker,dc=hva,dc=nl street: Wibautstraat 2-4 I want to make: dn: uid=beele,ou=medewerker,dc=hva,dc=nl changetype: modify replace: street street:... (3 Replies)
Discussion started by: tine
3 Replies
Login or Register to Ask a Question