开发者

Find / Replace Directory and Bash Script

开发者 https://www.devze.com 2023-02-20 18:14 出处:网络
I have some paths like this: /www/site1.dev/public_html/test.htm /www/site2.dev/html/test.htm /www/site3.dev/public/test.htm

I have some paths like this:

/www/site1.dev/public_html/test.htm
/www/site2.dev/html/test.htm
/www/site3.dev/public/test.htm

I'd like to pass them to a bash script and get a result back in this format:

http://site1.dev/test.htm
http://site2.dev/test.htm
http://site3.dev/test.htm

I'm not sure what the best way to handle the regex part of this is:

#!/bin/sh

RET='';

function trim() { echo $1; }


for ARG in "$@"
    do
        //do match and add existing RET value
        RET= 'http://'(regular expression or find/replace here) RET 
done


echo ">>$(trim $RET)<<"

Solution

My code based on Wes Hardaker's an开发者_Python百科swer

        DOMAIN=`echo $ARG | sed 's#.*www/##'`
        DOMAIN=`echo $DOMAIN | sed -E 's#/(public|html).*##'`
        POST=`echo $ARG | sed -E 's#.*html##'`
        echo 'http://'$DOMAIN$POST


The easiest way is probably to use 'sed'. IE:

RET="http://"`echo $ARG | sed 's#/www##;s#/(public_|)html/#/#;'


assuming your paths are in a file

$ awk 'BEGIN{OFS=FS="/"}{print "http:/"$1,$3,$NF}' file
http://site1.dev/test.htm
http://site2.dev/test.htm
http://site3.dev/test.htm

Ruby(1.9+)

$ ruby -F"/" -ane 'print "http:/"+[$F[0],$F[2],$F[-1]].join("/")' file


command to generate list of pathnames | while read path; do 
  IFS=/
  set -- $path
  echo "http://$3/$5"
done


Another approach, if you're starting out at www/..

pushd www
find * -type f | while read line; do echo http://${line%%/*}/${line##*/}; done
popd
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号