简体   繁体   中英

Find / Replace Directory and Bash Script

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)<<"

My code based on Wes Hardaker's answer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM