![](/img/trans.png)
[英]Bash Script Regular Expressions...How to find and replace all matches?
[英]Find / Replace Directory and Bash Script
我有一些這樣的路徑:
/www/site1.dev/public_html/test.htm /www/site2.dev/html/test.htm /www/site3.dev/public/test.htm
我想將它們傳遞給bash腳本並以以下格式返回結果:
http://site1.dev/test.htm http://site2.dev/test.htm http://site3.dev/test.htm
我不確定處理此正則表達式部分的最佳方法是:
#!/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)<<"
解
我的代碼基於Wes Hardaker的答案
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
最簡單的方法可能是使用“ sed”。 IE瀏覽器:
RET="http://"`echo $ARG | sed 's#/www##;s#/(public_|)html/#/#;'
假設您的路徑在文件中
$ 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
紅寶石(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
如果您是從www/..
開始的話,則是另一種方法www/..
pushd www
find * -type f | while read line; do echo http://${line%%/*}/${line##*/}; done
popd
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.