Whole word with sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Whole word with sed?
# 1  
Old 12-23-2009
Whole word with sed?

Hi experts,

need a help in SED

Code:
file=counter.c
module=abcd
i=http://svn.company.com/svn/${module}/trunk/counter/${file}

When i do
Code:
echo ${i}| sed "s|http://svn.company.com/svn/${module}/trunk/||g"| sed "s|${file}||g"

it results in
Code:
ounter.c

wherein the expected is
Code:
counter/

Can anyone pls help here

Last edited by zaxxon; 12-23-2009 at 06:05 AM.. Reason: use code tags please, thank you
# 2  
Old 12-23-2009
Code:
$> echo $i
http://svn.company.com/svn/abcd/trunk/counter/counter.c
$> echo $i| sed 's|.*/\([^/]\{1,\}\/\)[^/]*|\1|g'
counter/

# 3  
Old 12-23-2009
Or:
Code:
$ echo 'http://svn.company.com/svn/abcd/trunk/counter/counter.c'|
sed 's|.*/\(.*/\).*|\1|' 
counter/
$

# 4  
Old 12-23-2009
you can use "cut" instead.

Code:
echo ${i}| sed "s|http://svn.company.com/svn/${module}/trunk/||g" | cut -d"/" -f2


Last edited by zaxxon; 12-23-2009 at 07:54 AM.. Reason: use code tags please, thank you
# 5  
Old 12-23-2009
The problem was in your last sed statement:
Code:
sed "s|/${file}||g"

A more efficient way is:
Code:
$ j=${i%/*}
$ echo ${j##*/}
counter

# 6  
Old 12-23-2009
thanks all for your replies..

i tried all your commands.. it works well for counter and anything after trunk if that is a directory.. But i have many of these url's with filenames..

Ex:
i=http://svn.company.com/svn/abcd/trunk/counter/counter.c
i=http://svn.company.com/svn/abcd/trunk/xyz.xml
i=http://svn.company.com/svn/abcd/trunk/mno/scripts/diary/example.java

What i had tried to accomplish from my initial commands is to basically take out the filenames ($file) and http://svn.company.com/svn/abcd/trunk from $i. So that i can get as below:

1) counter/
2) this should give me a blank because there is nothing found after removing http://svn.company.com/svn/abcd/trunk/ and xyz.xml
3) mno/scripts/diary/

So that i can create directories of whatever is left out. In case of 2, i will create it on the parent directory where the command is running.. So i thought if i can just take out the $file and http://svn.company.com/svn/abcd/trunk , it should give me the dirs..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

2. Shell Programming and Scripting

Remove word with sed

How can I use sed or any utility to remove any word that begins with TRS-, I have tried sed 's/ERA.*//g' but seems not to be working Input: 23 TRS-458-9 345 235 45 TRS-42-5 423 000 76 300 234 Output: 23 345 235 45 423 000 76 300 234 (5 Replies)
Discussion started by: aydj
5 Replies

3. Shell Programming and Scripting

Replace a word using sed

Hi, I have the following line in file1 export NAME="NEW_NAME" I'm writing a shell script which reads the NEW_NAME from the user and replace in the file. I have used the following command for that read Name_replace sed -i 's/NEW_NAME/$Name_replace/' file1 but it is not... (2 Replies)
Discussion started by: Ananthdoss
2 Replies

4. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

5. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

6. Shell Programming and Scripting

Replace word with sed

Hi there.! I'm trying to make a script that corrects wrong spelling. I want to use sed to replace wrong word with the correct one, but this must be made in a while loop that reads the wrong word from file (with read line) and the correct one from another file. I can't find a way to run sed like... (3 Replies)
Discussion started by: spiii
3 Replies

7. Shell Programming and Scripting

Extract word using sed

Hello, I am new to sed and am trying to extract a word using sed. for example i have a line "const TotalAmount& getTotalAmount() const; " in the file test.txt I am trying to extract getTotalAmount() from the line. For this i tried cat test.txt | sed -n 's/.*get*\(\)//p But... (8 Replies)
Discussion started by: prasbala
8 Replies

8. Shell Programming and Scripting

Word wrap with sed

Hi, I got some timetable in a file but it is all mixed up like this 01:00 hgrtwhrt #104:00 tyergethr05:00 tqqrthd qrth #107:00 qhtrhqerth10:00 qerthrthqr qtrqthr qthrrt11:00 thqrthqrthrr rthgreth #212:00 trhrthrth14:00 wrthwrtwrqrthwrthwr #2116:00 trqhthtr: rthrthr17:00 rtwhtrhwrth rthwrt... (6 Replies)
Discussion started by: stinkefisch
6 Replies

9. Shell Programming and Scripting

whole word substitution in SED

I am trying to substitute something with sed and what I want is to substitute a whole word and not part of a word. ie sed 's/class/room/g' filename will substitute both class and classes into room and roomes which is not what i want Grep for instance can use the -w option or <> grep -w... (7 Replies)
Discussion started by: gikay01
7 Replies

10. Shell Programming and Scripting

sed for adding word

hello i have a file (myfile) contains line as follows /home/mytarget/myproject i want to add a pattern at the end of this line. my pattern is- /.*mk i want like it - /home/mytarget/myproject/*.mk i tried sed like sed 's/$//*.mk/' myfile > newfilename, but not wrking. pls... (2 Replies)
Discussion started by: shailesh_arya
2 Replies
Login or Register to Ask a Question