[Solved] String manuplation using sed

Hello All,
I have a file that has following contents ...

config INY_DEBUG
bool "Debug"
default n

source mod/ati/Kconfig
source mod/spi/Kconfig
source mod/kgi/Kconfig
source mod/mei/Kconfig
source mod/cai/Kconfig
source mod/stormi/Kconfig

I have a shell variable

Var=file:///home/techno/svnrep/MDTC/mdtc/te/

Now I want to replace

source

with

svn export $Var

so that file should contain ..

svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/ati/Kconfig Kconfig.ati
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/spi/Kconfig Kconfig.spi
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/kgi/Kconfig Kconfig.kgi
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/mei/Kconfig Kconfig.mei
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/cai/Kconfig Kconfig.cai
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/stormi/Kconfig Kconfig.stormi

I have base knowledge of doing find and replace using "sed" but really don't know how to extract shell variable in "sed"

Thank you very much in advance.

try..

 
awk -v var=$Var '/source/{v=$NF "." $(NF-1);sub("source","svn export"var)}{print $0" "v;v=""}' filename
Var="file:\/\/\/home\/techno\/svnrep\/MDTC\/mdtc\/te\/"

sed "s/source /svn export $Var/g" file

You could also do this with awk:

var="file:///home/techno/svnrep/MDTC/mdtc/te/"

awk -F'/' -v V="$var" '/source/{n=$NF "." $(NF-1);sub("source","svn export");sub(/[a-zA-Z]*\/.*/,V "& " n)}1' file

Dear Vidyadhar85,
Its doing half of the work .... but still i want to add

Kconfig.ati Kconfig.spi .....

etc at the end of each line. Anyway thanks for your quick reply.

When I try the code suggested by vidyadhar85, pamu, Yoda, and anand.shah, I don't end up with anything that looks close to the output requested:

svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/ati/Kconfig Kconfig.ati
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/spi/Kconfig Kconfig.spi
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/kgi/Kconfig Kconfig.kgi
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/mei/Kconfig Kconfig.mei
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/cai/Kconfig Kconfig.cai
svn export  file:///home/techno/svnrep/MDTC/mdtc/te/mod/stormi/Kconfig Kconfig.stormi

The following code seems to replace the contents of file (as specified in the original posting in this thread) with the output requested above:

Var=file:///home/techno/svnrep/MDTC/mdtc/te/
sed -n "s|^source mod/\([^/]*\)/Kconfig|svn export  ${Var}mod/\1/Kconfig Kconfig.\1|p" file > tmp$$ && cp tmp$$ file && rm tmp$$
1 Like

Many many thanks Don Cragun ..... its working perfectly.
Also thanks to PAmu , Yoda and Vidyadhar for precious inputs.

How to mark the thread as solved ?