我有一个文件channels.xml,有一个这样的结构,长4000行
<!--begin_channel-->
Rai 1.png
<!--end_channel-->
Rai 1 +2HD.png
Rai 1 +1HD.png
<!--begin_channel-->
Rai 2.png
<!--end_channel-->
Rai 2 +2HD.png
Rai 2 +1HD.png
.
.
我需要像这样制作一个文件channels.sh:
cp /cygdrive/c/ProgramData/ServerCare/data/picons/Rai_1.png /cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/Rai_1_+2HD.png
cp /cygdrive/c/ProgramData/ServerCare/data/picons/Rai_1.png /cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/Rai_1_+1HD.png
cp /cygdrive/c/ProgramData/ServerCare/data/picons/Rai_2.png /cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/Rai_2_+2HD.png
cp /cygdrive/c/ProgramData/ServerCare/data/picons/Rai_2.png /cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/Rai_2_+1HD.png
我需要用cygwin做,我有一个脚本可以在ubuntu下做,但是cygwin无法正常工作。这是脚本:
#!/bin/bash
#colori
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
if [ $# -ne 4 ]
then
echo -e "${RED}ERRORE${NC}, inserire $0 filein fileout pathlogo_from pathlogo_to"
echo -e "${RED}Ricorda${NC} le enstensioni!"
exit 0
fi
if [ -f $2 ]
then
echo -n -e "File $2 esistente, vuoi ${RED}sovrascrivere${NC}?(Y/n) "
read scelta
if [ $scelta != 'Y' ]
then
exit 0
else
rm $2
fi
fi
awk '/\<!--begin_channel--\>/{flag=1; next} /\<!--end_channel--\>/{flag=0} flag' $1 > temp
while read line
do
awk "/$line/,/<!--begin_channel-->/ " $1 | grep -v "$line" | grep -v "^<" > tmp
for channel in `cat tmp`
do
channel=`echo $channel | tr ")" "_" | tr "(" "_" | tr ":" "_"`
echo "cp $3/$line $4/$channel"
done
done <temp >> $2
rm temp
rm tmp
echo -e "${GREEN}File $2 creato correttamente${NC}"
我认为错误是关于这一行
awk "/$line/,/<!--begin_channel-->/ " $1 | grep -v "$line" | grep -v "^<" > tmp
要运行它,我使用命令
{ bash } »./script.sh channels.xml channels.sh /cygdrive/c/ProgramData/ServerCare/data/picons/ /cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/
有人可以帮我修理或写一个新的吗?
答案 0 :(得分:0)
@Tapiocapioca:尝试:您可以根据您的要求将输出输入文件或脚本,然后告诉我们。
awk -vlines=$(cat Input_file | wc -l) -vsource="/cygdrive/c/ProgramData/ServerCare/data/picons/" -vtarget="/cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/" '/<!--end_channel/{getline;while($0 !~ /<!--begin_channel/){print "cp " source SOURCE OFS target $0;if(NR==lines){exit};getline}} /<!--begin_channel/{getline;SOURCE=$0;next}' Input_file
非单一衬里形式的解决方案如下:
awk -vlines=$(cat Input_file | wc -l) -vsource="/cygdrive/c/ProgramData/ServerCare/data/picons/" -vtarget="/cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/" '
/<!--end_channel/{
getline;
while($0 !~ /<!--begin_channel/){
print "cp " source SOURCE OFS target $0;
if(NR==lines){
exit
};
getline
}
}
/<!--begin_channel/{
getline;
SOURCE=$0;
next
}
' Input_file
答案 1 :(得分:0)
虽然我不是蛮力的粉丝,但有以下几种选择:
awk -vsource="/cygdrive/c/ProgramData/ServerCare/data/picons/"
-vtarget="/cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/"
'/begin_channel/{getline src;gsub(/ /,"_",src)}
/end_channel/{for(i=1;i<3;i++){
getline dest;gsub(/ /,"_",dest);
print "cp",source src,target dest}}' xml_file
现在关于上述内容的好处是,一旦您对输出正确感到满意,您可以简单地在打印和&#34; cp&#34;之间放置一个管道(|)。并且它会将输出传递给shell并使副本发生,或者如果您确实需要出于某种原因制作中间脚本,请将管道留出并简单地将awk重定向到channels.sh
这里也是一个bash替代品:
xml="$1"
script="$2"
src_path="$3"
dest_path="${src_path}duplicati/"
get_src=0
get_dest=0
while read -r line
do
if [[ "$line" =~ begin_channel ]]
then
get_src=1
get_dest=0
elif [[ "$line" =~ end_channel ]]
then
get_dest=1
get_src=0
else
(( get_src == 1 )) && src="${src_path}${line// /_}"
if (( get_dest == 1 ))
then
dest="${dest_path}${line// /_}"
echo "cp $src $dest"
fi
fi
done<"$xml">"$script"