Search code examples
xmlbashawkcygwingawk

Make a .sh file reading from a xml file


I have one file channels.xml with one structure like this, is long 4000 lines

<!--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
.
.

I need make one file channels.sh like this:

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

I need do it with cygwin, I have one script can do it under ubuntu, but with cygwin is not working. This is the script:

#!/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}"

I think the error is about this line

awk "/$line/,/<!--begin_channel-->/ " $1 | grep -v "$line" | grep -v "^<" > tmp

To run it I use the command

{ bash }  »./script.sh channels.xml channels.sh /cygdrive/c/ProgramData/ServerCare/data/picons/ /cygdrive/c/ProgramData/ServerCare/data/picons/duplicati/

Can someone help me to fix it or write one new?


Solution

  • @Tapiocapioca: Try: You could take output into a file or script as per your requirement and let us know then.

    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
    

    NON-one liner form of solution as follows too:

    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