Source File:
google.cz http://czechrepublic.google.com/
http://czechrepublic.google.cz http://czechrepublic.google.com/
http://brno.google.cz http://brno.google.com/
Fail Code
root@arisvm ~/g] $ cat trya
rm -f ss
for i in a.txt
do
#b=`cat $i|awk '{print $1}'`
#c=`cat $i|awk '{print $2}'`
echo "server {" >> ss
echo " listen 80;" >> ss
echo " server_name $(cat $i|awk '{print $1}'|head -1 ) " >> ss
echo " rewrite ^/(.*)$ $(cat $i|awk '{print $2}' | head -1 ) permanent;" >> ss
echo " } " >> ss
done
output of fail code:
root@arisvm ~/g] $ cat ss
server {
listen 80;
server_name google.cz
rewrite ^/(.*)$ http://czechrepublic.google.com/ permanent;
}
root@arisvm ~/g] $
Desired Output:
server {
listen 80;
server_name google.cz;
rewrite ^/(.*)$ http://czechrepublic.google.com permanent;
}
server {
listen 80;
server_name www.google.cz;
rewrite ^/(.*)$ http://czechrepublic.google.com permanent;
}
server {
listen 80;
server_name czechrepublic.google.cz;
rewrite ^/(.*)$ http://czechrepublic.google.com permanent;
}
server {
listen 80;
server_name brno.google.cz;
rewrite ^/(.*)$ http://brno.google.com permanent;
}
Aia
June 15, 2015, 3:57pm
2
Where do you extract the www.google.cz;
?
google.cz
czechrepublic.google.cz
brno.google.cz
Anyway, perhaps something like this would be close to a solution.
awk 'NF>1 {
sub("http://", "", $1);
print "server {";
print " listen 80;";
print " server_name", $1";";
print " rewrite ^/(.*)$", $2, "permanent;";
print " }";
}' source_file
1 Like
RudiC
June 15, 2015, 6:00pm
3
Starting from your "fail code", how would you like these adaptions:
while read SN RWT
do echo "server {"
echo " listen 80;"
echo " server_name $SN"
echo " rewrite ^/(.*)$ $RWT permanent;"
echo " }"
done <a.txt >ss
cat ss
server {
listen 80;
server_name google.cz
rewrite ^/(.*)$ http://czechrepublic.google.com/ permanent;
}
server {
listen 80;
server_name http://czechrepublic.google.cz
rewrite ^/(.*)$ http://czechrepublic.google.com/ permanent;
}
server {
listen 80;
server_name http://brno.google.cz
rewrite ^/(.*)$ http://brno.google.com/ permanent;
}
1 Like
In addition to RudiC's suggestion, you can use ${SN#*://}
instead of $SN
to strip of the URL protocol part if present..
1 Like
Guys, all is good, but i think.. you guys miss the 1st part and 2nd part of the output. the source file is constant..look closely above and see the output
source file look closely. the top 1 or the very top doesnt have http.compare to line number 2 and 3 and so forth.
google.cz http://czechrepublic.google.com/
http://czechrepublic.google.cz http://czechrepublic.google.com/
http://brno.google.cz http://brno.google.com/
server {
listen 80;
server_name google.cz;
rewrite ^/(.*)$ http://czechrepublic.google.com permanent;
}
server {
listen 80;
server_name www.google.cz;
rewrite ^/(.*)$ http://czechrepublic.google.com permanent;
}
server {
listen 80;
server_name czechrepublic.google.cz;
rewrite ^/(.*)$ http://czechrepublic.google.com permanent;
}
server {
listen 80;
server_name brno.google.cz;
rewrite ^/(.*)$ http://brno.google.com permanent;
}
---------- Post updated at 09:31 PM ---------- Previous update was at 09:25 PM ----------
Why?? $SN should be called right? whats the diff? care to share please??
thanks
---------- Post updated at 09:36 PM ---------- Previous update was at 09:31 PM ----------
i got you, to remove the http thanks