views:

114

answers:

6

I got a file that looks like

dcdd62defb908e37ad037820f7  /sp/dir/su1/89/asga.gz
7d59319afca23b02f572a4034b  /sp/dir/su2/89/sfdh.gz
ee1d443b8a0cc27749f4b31e56  /sp/dir/su3/89/24.gz
33c02e311fd0a894f7f0f8aae4  /sp/dir/su4/89/dfad.gz
43f6cdce067f6794ec378c4e2a  /sp/dir/su5/89/adf.gz
2f6c584116c567b0f26dfc8703  /sp/dir/su6/895/895.gz
a864b7e327dac1bb6de59dedce  /sp/dir/su7/895/895.gz

How do i use sed to substitue all the su* such that I can replace with a single value like

sed "s/REXEXP/newfolder/g" myfile

thanks in advance

A: 

su[0-9] will match a single digit.

Ignacio Vazquez-Abrams
+1  A: 
$ sed -e 's|/su[^/]*|/newfolder|' /tmp/files\
dcdd62defb908e37ad037820f7  /sp/dir/newfolder/89/asga.gz
7d59319afca23b02f572a4034b  /sp/dir/newfolder/89/sfdh.gz
...

If you want to get rid of the checksums as well:

$ sed -r -e 's|/su[^/]*|/newfolder|' -e 's/^[^ ]+ +//' /tmp/files\
/sp/dir/newfolder/89/asga.gz
/sp/dir/newfolder/89/sfdh.gz
...
Wayne Conrad
As I said in my other comment, you don't need `/` to be the delimiter for `s`, so you can write: `sed -e 's#/su[^/]*#/newfolder#' /tmp/files` for more readability.
Alok
what if one of the directory looks like /sp/su4dir/su1/89/24.gz ?
ghostdog74
@ghostdog74: the problem is not very well-defined, so we can only guess about what pattern the OP really needs.
Alok
@Alok, Thanks .
Wayne Conrad
+2  A: 

I think you want

sed 's/su./newfolder/g'

If you actually want to keep the number in su1...su7 as a part of newfolder (for example newfolder1...newfolder7), you can do:

sed 's/su\(.\)/newfolder\1/g'

It also depends upon how "strict" do you want your patterns to be. The above will match su followed by any character and do the replacement. On the other hand, a command like s#/su\([0-9]\)/#/newfolder\1/#g will only match /su followed by a digit, followed by /. So you may need to adjust your pattern accordingly.

Alok
A: 

sed requires a dirty amount of metacharacter escaping, some of it may be slightly off.

sed -i -e 's/\/su[^\/]+\//\/newFolder\//g' myfile
Paul Creasey
You don't need `/` to be the character after `s`, so you can pick any character not in your pattern: so, `'sxsu[^/]+/x/newFolder/xg'` does the same (if I didn't make a typo).
Alok
@Alok: Thanks for the tip, i always knew that was possible in perl, didn't reaise you should do it in SED, very useful, since escapes make sed regex near unreadable!
Paul Creasey
it works when i changed + to .*. also, it doesn't take care of paths like : /sp/su4dir/su1/89/24.gz
ghostdog74
A: 

I vote for Wayne Conrad's answer as the most likely to be what the OP wants, but I'd suggest using an alternate character for the sed expression separator, thus:

sed 's|/su[^/]*|/newfolder|'  /tmp/files

That makes it a bit cleaner.

Note also that the trailing 'g' is probably not wanted.

BobS
A: 

use awk. since there is a delimiter you can use , '/'. after that, column 4 is what you want to change. So if you have paths like /sp/su3dir/su2/89/sfdh.gz , su3dir will not be affected.

awk -F"/" '{$4="newfolder";}1' OFS="/" file
ghostdog74