[Solved] String manuplation using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] String manuplation using sed
# 1  
Old 06-20-2013
[Solved] String manuplation using sed

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

Code:
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
Code:
Var=file:///home/techno/svnrep/MDTC/mdtc/te/

Now I want to replace
Code:
source

with
Code:
svn export $Var

so that file should contain ..

Code:
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.

Last edited by Don Cragun; 06-20-2013 at 07:56 AM..
# 2  
Old 06-20-2013
try..

Code:
 
awk -v var=$Var '/source/{v=$NF "." $(NF-1);sub("source","svn export"var)}{print $0" "v;v=""}' filename


Last edited by vidyadhar85; 06-20-2013 at 03:18 AM..
# 3  
Old 06-20-2013
Code:
Var="file:\/\/\/home\/techno\/svnrep\/MDTC\/mdtc\/te\/"

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

# 4  
Old 06-20-2013
You could also do this with awk:
Code:
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

# 5  
Old 06-20-2013
Dear Vidyadhar85,
Its doing half of the work .... but still i want to add
Code:
Kconfig.ati Kconfig.spi .....

etc at the end of each line. Anyway thanks for your quick reply.
# 6  
Old 06-20-2013
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:
Code:
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:
Code:
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$$


Last edited by Don Cragun; 06-20-2013 at 03:36 AM.. Reason: Add missing ).
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 06-20-2013
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 ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Last occurrence of a string

I apologize if it was asked before but I couldn't find something related. I want to replace 2 strings in a file e.g pwddb=Lar1wod (need to replace string after =) pwdapp=Wde2xe (need to replace string after =) AND in same file want to find last occurrence of a string (SR2-134561),... (2 Replies)
Discussion started by: J_ang
2 Replies

2. Shell Programming and Scripting

[Solved] How to get file name which contains certain string?

Hi Gurus, I need find which file contains certain string in one directory. when using cat * |grep "string", I can get the string name but no file name displayed. I am wondering if there is any option I can use to get which file contains this certain string. Thanks in advance. (3 Replies)
Discussion started by: ken6503
3 Replies

3. Shell Programming and Scripting

[Solved] String integer comparison

I am trying to execute something like this file=/tmp/test.txt firstline=$(head -n 1 $file) value=`echo $firstline | cut -d'=' -f2` if then echo true fi i read the first line of a file, cut to the numeric value in the first line and check if it greater than 2 but for some... (11 Replies)
Discussion started by: madhan_dc
11 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Length of each string

Hi I have a file which has sequences which look like this >String1 aqwertrtrytytyuuijhgddfghhhghhgjhjhhsswekrkmygppdslxmvbnhkwqalldrtjbllnlnlnnnvc >String2 qwwerrtyuiopasdfghjmnbvfklzxerbvcwghjjkoowwqerrtggbddqsdfgaqwcxzakjtyugfsdefrtgyhujiknbbbbcdcdcxsxsx zxzxcvcfcdcg >String3... (5 Replies)
Discussion started by: sa@@
5 Replies

5. Shell Programming and Scripting

[Solved] String Comparison

Hi I am beginner in writing shell scripting please tell me how to compare a string in Unix shell. i have two variables in a shell script, var1="00101 00201 00301 303 401 405" var2="101 201 301" i want to compare var1 with var2 . for example if 101 from Var1 present in Var2 or not. similarly... (5 Replies)
Discussion started by: nikesh29
5 Replies

6. Shell Programming and Scripting

[Solved] sed

sed -e 's/console/raw/g' this command will replace the letter pradeep with rawat what if i want to replace a word like FRIENDS with a space simultaneously from the same file i m replacing pradeep. im doing this sed -e 's/console/raw/g' && sed 's/FRIENDS//g' but i dono why this is not happening. (2 Replies)
Discussion started by: console
2 Replies

7. Shell Programming and Scripting

[Solved] Decoding a base 64 string

Is it possible to decode a base 64 string in linux or unix. If so, related commands or reference notes would be really helpful. (1 Reply)
Discussion started by: chandu123
1 Replies

8. Shell Programming and Scripting

[Solved] awk string with spaces

Why does this work: awk -v s="this is a string" 'index($0, s)' file while the following doesn't? s="this is a string" awk -v s=$s 'index($0, s)' file How do I search for a string with spaces in it? ---------- Post updated at 01:18 AM ---------- Previous update was at 01:15 AM ----------... (0 Replies)
Discussion started by: locoroco
0 Replies

9. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

10. Shell Programming and Scripting

[SOLVED] String length in kornshell

In the kornshell you can get the length of a string with $ x=abc $ print ${#x} 3 If the current locale is a multibyte locale, like de_AT.UTF-8, you get the length of the string in bytes, not characters: $ x=für $ print ${#x} 4 Is there an easy way to get the length of a... (8 Replies)
Discussion started by: hergp
8 Replies
Login or Register to Ask a Question