2016-01-22 26 views

回答

2

你甚至嘗試實施?這是超級簡單..

你只需要逐行閱讀文件。你可以這樣做那樣:

while read line ; do 
    echo $line 
done < your_file 

然後你必須檢查你的線路以http://或https://:

if [[ $line =~ ^https?:// ]] ; then 
    echo $line 
fi 

..和添加http如果不:

if [[ $line =~ ^https?:// ]] ; then 
    echo $line 
else 
    echo http://$line 
fi 

全部放在一起你會得到:

while read line ; do                                               
    if [[ $line =~ ^https?:// ]] ; then                                          
     echo $line                                               
    else                                                  
     echo http://$line                                             
    fi                                                  
done < your_file 

您可能要更改保存到一個文件,然後將輸出重定向到一個臨時文件中的第一和結束時替換它現有的文件:

while read line ; do                                               
    if [[ $line =~ ^https?:// ]] ; then                                          
     echo $line                                               
    else                                                  
     echo http://$line                                             
    fi                                                  
done <data> data.tmp                                                                                             
mv data{.tmp,} 

替代awk的版本:

awk '{if($0 ~ /^https?/) print; else print "http://"$0}' data 

修改文件:

awk '{if($0 ~ /^https?/) print; else print "http://"$0}' your_file > your_file.tmp ; mv your_file{.tmp,} 
1

使用sed的

sed '\|^https\{0,1\}://|!s,^,http://,' file 

http://www.hello.by 
https://www.google.com 
http://www.stackoverflow.com 
http://www.fedia.com 
1

請嘗試以下sed命令

sed -i '/https\?:\/\//{t;} 
s/^/http:\/\// 
' YOUR_FILE