Sharad Shrestha
Sharad Shrestha

Reputation: 169

How to replace name of a folder in a path?

Replace name of a folder (ab) ahead of anime or movie folder in a path. Replace ab folder with ac.

File has list of paths:
C://file1/file2/file3/file4/file5/film
C://file1/file2/file3/file4/file5/ab/cartoon
C://file1/file2/file3/file4/file5/ab/cartoon1
C://file1/file2/file3/file4/ab/movie/adjeifjeo/movie
C://file1/file2/file3/file4/ab/anime/adjeifjeo/anime
C://file1/file2/file3/ab/anime/adjeifjeo/anime1
C://file1/file2/ab/anime/adjeifjeo/anime2
C://file1/file2/file3/file4/file5/file6/ab/anime/adjeifjeo/anime3


Output:
C://file1/file2/file3/file4/file5/film
C://file1/file2/file3/file4/file5/ab/cartoon
C://file1/file2/file3/file4/file5/ab/cartoon1
C://file1/file2/file3/file4/ac/movie/adjeifjeo/movie
C://file1/file2/file3/file4/ac/anime/adjeifjeo/anime
C://file1/file2/file3/ac/anime/adjeifjeo/anime1
C://file1/file2/ac/anime/adjeifjeo/anime2
C://file1/file2/file3/file4/file5/file6/ac/anime/adjeifjeo/anime3

ab folder is always present 1 folder ahead of movie or anime folder.

Name of current ab folder always changes. Sometimes its ad, ae, af, ag (it's random).

Only path with movie or anime folder should replace name of ab folder with ac.

sed 's/\/ab\//\/ac\//'

This does not solve as "ab" keeps changing and all path with "ab" will be replaced with "ac".

Upvotes: 0

Views: 85

Answers (3)

The fourth bird
The fourth bird

Reputation: 163277

Or using sed -E and match a single char a-z after matching /a

sed -E 's~/a[a-z]/(movie|anime)/~/ac/\1/~' file

Output

C://file1/file2/file3/file4/file5/film
C://file1/file2/file3/file4/file5/ab/cartoon
C://file1/file2/file3/file4/file5/ab/cartoon1
C://file1/file2/file3/file4/ac/movie/adjeifjeo/movie
C://file1/file2/file3/file4/ac/anime/adjeifjeo/anime
C://file1/file2/file3/ac/anime/adjeifjeo/anime1
C://file1/file2/ac/anime/adjeifjeo/anime2
C://file1/file2/file3/file4/file5/file6/ac/anime/adjeifjeo/anime3

Upvotes: 1

sseLtaH
sseLtaH

Reputation: 11217

Using sed

$ sed 's;[[:alpha:]]*\(/movie/\|/anime/\);ac\1;' input_file
C://file1/file2/file3/file4/file5/film
C://file1/file2/file3/file4/file5/ab/cartoon
C://file1/file2/file3/file4/file5/ab/cartoon1
C://file1/file2/file3/file4/ac/movie/adjeifjeo/movie
C://file1/file2/file3/file4/ac/anime/adjeifjeo/anime
C://file1/file2/file3/ac/anime/adjeifjeo/anime1
C://file1/file2/ac/anime/adjeifjeo/anime2
C://file1/file2/file3/file4/file5/file6/ac/anime/adjeifjeo/anime3

Upvotes: 2

Dudi Boy
Dudi Boy

Reputation: 4865

suggesting:

 sed 's|/ab/|/ac/|'

Upvotes: 0

Related Questions