sed - How to insert line before the first blank line following a token

Hello.

I have a config file (/etc/my_config_file) which may content :

#
# port for HTTP (descriptions, SOAP, media transfer) traffic
port=8200

# network interfaces to serve, comma delimited
network_interface=eth0

# set this to the directory you want scanned.
# * if have multiple directories, you can have multiple media_dir= lines
# * if you want to restrict a media_dir to a specific content type, you
#   can prepend the type, followed by a comma, to the directory:
#   + "A" for audio  (eg. media_dir=A,/home/jmaggard/Music)
#   + "V" for video  (eg. media_dir=V,/home/jmaggard/Videos)
#   + "P" for images (eg. media_dir=P,/home/jmaggard/Pictures)
#media_dir=V,/srv/media/film
#media_dir=A,/srv/media/musique
#media_dir=P,/srv/media/photo
#media_dir = /home/some_user/my_personal_old_data

# set this if you want to customize the name that shows up on your clients
friendly_name=SJL DLNA Server MY_SERVER OPENSUSE 13.1

I have a variable which contains the token to search :

MY_TOKEN="media_dir"

I have a variable which contains the file to search in

MY_FILE="/etc/my_config_file"

I have a variable which contains the value for the searched token

MY_PARAMETER_VALUE="media_dir = /srv/media/my_personal_new_data"

I want to insert $MY_PARAMETER_VALUE, before the blank line and after the line : #media_dir = /home/some_user/my_personal_old_data
and the search token is $MY_TOKEN using some function like :

my_insert  $MY_TOKEN  $MY_PARAMETER_VALUE $MY_FILE

Any help is welcome

$ echo $MY_FILE
beforeblank.conf
$ echo $MY_TOKEN
media_dir
$ echo $MY_PARAMETER_VALUE
media_dir = /srv/media/my_personal_new_data
$ cat $MY_FILE
#
# port for HTTP (descriptions, SOAP, media transfer) traffic
port=8200

# network interfaces to serve, comma delimited
network_interface=eth0

# set this to the directory you want scanned.
# * if have multiple directories, you can have multiple media_dir= lines
# * if you want to restrict a media_dir to a specific content type, you
#   can prepend the type, followed by a comma, to the directory:
#   + "A" for audio  (eg. media_dir=A,/home/jmaggard/Music)
#   + "V" for video  (eg. media_dir=V,/home/jmaggard/Videos)
#   + "P" for images (eg. media_dir=P,/home/jmaggard/Pictures)
#media_dir=V,/srv/media/film
#media_dir=A,/srv/media/musique
#media_dir=P,/srv/media/photo
#media_dir = /home/some_user/my_personal_old_data

# set this if you want to customize the name that shows up on your clients
friendly_name=SJL DLNA Server MY_SERVER OPENSUSE 13.1
$ sed -e "/$MY_TOKEN/,/^[[:blank:]]*$/{" -e "\|^[[:blank:]]*$|s|.*|${MY_PARAMETER_VALUE}\n&|g" -e "}" $MY_FILE
#
# port for HTTP (descriptions, SOAP, media transfer) traffic
port=8200

# network interfaces to serve, comma delimited
network_interface=eth0

# set this to the directory you want scanned.
# * if have multiple directories, you can have multiple media_dir= lines
# * if you want to restrict a media_dir to a specific content type, you
#   can prepend the type, followed by a comma, to the directory:
#   + "A" for audio  (eg. media_dir=A,/home/jmaggard/Music)
#   + "V" for video  (eg. media_dir=V,/home/jmaggard/Videos)
#   + "P" for images (eg. media_dir=P,/home/jmaggard/Pictures)
#media_dir=V,/srv/media/film
#media_dir=A,/srv/media/musique
#media_dir=P,/srv/media/photo
#media_dir = /home/some_user/my_personal_old_data
media_dir = /srv/media/my_personal_new_data

# set this if you want to customize the name that shows up on your clients
friendly_name=SJL DLNA Server MY_SERVER OPENSUSE 13.1

Last line of file must a blank line.
To modified directly the file, you can use -i option if your sed support.
You just create a function that call this type de sed command.
Regards.

1 Like

Awk approach

$ cat beforeblank.conf 
#
# port for HTTP (descriptions, SOAP, media transfer) traffic
port=8200

# network interfaces to serve, comma delimited
network_interface=eth0

# set this to the directory you want scanned.
# * if have multiple directories, you can have multiple media_dir= lines
# * if you want to restrict a media_dir to a specific content type, you
#   can prepend the type, followed by a comma, to the directory:
#   + "A" for audio  (eg. media_dir=A,/home/jmaggard/Music)
#   + "V" for video  (eg. media_dir=V,/home/jmaggard/Videos)
#   + "P" for images (eg. media_dir=P,/home/jmaggard/Pictures)
#media_dir=V,/srv/media/film
#media_dir=A,/srv/media/musique
#media_dir=P,/srv/media/photo
#media_dir = /home/some_user/my_personal_old_data

# set this if you want to customize the name that shows up on your clients
friendly_name=SJL DLNA Server MY_SERVER OPENSUSE 13.1
$ cat test.sh
#!/bin/bash

MYFILE="beforeblank.conf"
MY_TOKEN="media_dir"
MY_PARAMETER_VALUE="media_dir = /srv/media/my_personal_new_data"

echo $MY_FILE
echo $MY_TOKEN
echo $MY_PARAMETER_VALUE

awk -v token="$MY_TOKEN" \
    -v parva="$MY_PARAMETER_VALUE" '
     p ~ token && !NF{print parva;p="";x=1}!x{p=$0}1' $MYFILE
$ bash test.sh

media_dir
media_dir = /srv/media/my_personal_new_data
#
# port for HTTP (descriptions, SOAP, media transfer) traffic
port=8200

# network interfaces to serve, comma delimited
network_interface=eth0

# set this to the directory you want scanned.
# * if have multiple directories, you can have multiple media_dir= lines
# * if you want to restrict a media_dir to a specific content type, you
#   can prepend the type, followed by a comma, to the directory:
#   + "A" for audio  (eg. media_dir=A,/home/jmaggard/Music)
#   + "V" for video  (eg. media_dir=V,/home/jmaggard/Videos)
#   + "P" for images (eg. media_dir=P,/home/jmaggard/Pictures)
#media_dir=V,/srv/media/film
#media_dir=A,/srv/media/musique
#media_dir=P,/srv/media/photo
#media_dir = /home/some_user/my_personal_old_data
media_dir = /srv/media/my_personal_new_data

# set this if you want to customize the name that shows up on your clients
friendly_name=SJL DLNA Server MY_SERVER OPENSUSE 13.1
1 Like

Yes it works.
I prefer sed because I know a little about SED, but I know nothing about AWK
Just for my knowledge, How to edit inline ?

Thank you very much.

---------- Post updated at 13:08 ---------- Previous update was at 13:05 ----------

Yes it works.
Thank you very much

@ jcdole I didn't get you sorry, what do you want to edit ?

Yes it works.
Thank you very much.

Another option would be:

awk '$0~s,!NF{if(!NF)print v}1' s="$MY_TOKEN" v="$MY_PARAMETER_VALUE" file