0

My Script:

 cd /var/www/try/
 sort -u
 files="$(find -L "/var/www/try/" -type d)"
 echo "Count: $(echo -n "$files" | wc -l)"
 echo "$files" | while read file; do
 echo $file >> filename.csv
 done

Output :

/var/www/try
/var/www/try/cat

Output Should be :

var-www-try
var-www-try-cat

Second Case : any character except / as some fo my folder name contain / like for e.g.

"tv/dvd"

Output genrated :

 /var/www/try/cat/tv/dvd

Output Should be :

 var-www-try-cat-tv/dvd-
loop
  • 9,002
  • 10
  • 40
  • 76
Terence
  • 729
  • 1
  • 7
  • 17

1 Answers1

2

Have a look at sed, which is a popular tool for string replacing tasks.

>> x=$(echo '/var/www/try/cat/tv/' | sed 's/\//-/g')
>> echo $x
-var-www-try-cat-tv-
>> x=${x:1}
>> echo $x
var-www-try-cat-tv-

edit: In response to your second case:

as some fo my folder name contain /

Maybe there's a misunderstanding here but you should not have a / in a filename. See Is it possible to use "/" in a filename?

Community
  • 1
  • 1
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • 1
    can u please help me with how can i replace every first slash of the line with something else using sed command – Terence May 24 '14 at 08:42
  • 1
    sure, here's an example: echo '/this/starts/with/a/slash/' | sed 's/^\//somethingelse/' will yield somethingelsethis/starts/with/a/slash/ – timgeb May 24 '14 at 10:47