Command awk under CYGWIN

One my friend wrote one script on his machine linux, when I try to use it under cygwin I recive one error about the command awk. Is there someone can suggest me the way to fix the error? The script is wrote using gawk and I have no idea what kind of comand is used by cygwin.

This is the script:

#!/bin/bash
#credits xxx
#per info email: xxx

#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 "^<' > xxx
        for channel in `cat xxx`
        do
            channel=`echo $channel | tr ")" "_" | tr "(" "_" | tr ":" "_"`
            echo  "cp $3/$line $4/$channel"
        done

done <temp >> $2
rm temp
rm xxx
echo -e "${GREEN}File $2 creato correttamente${NC}"

This is the command not executed correctly

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

Thank you in advice if someone can help me.

I don't quite follow/understand what you're trying to do and this is way too verbose, but...

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

No, is still not working :frowning:

I have one input file like this: channels.xml

<!--begin_channel-->
Rai_1.png
<!--end_channel-->
1:0:1:EDF:16A:0:8D68:0:0:0:.png
Rai_1_+2HD.png
Rai_1_+1HD.png
.
.
.

This script need give me back one file like this: channels.sh

cp /cygdrive/c/ProgramData/data/picons/Rai_1.png /cygdrive/c/ProgramData/data/picons/duplicati/1_0_1_EDF_16A_0_8D68_0_0_0_.png
cp /cygdrive/c/ProgramData/data/picons/Rai_1.png /cygdrive/c/ProgramData/data/picons/duplicati/Rai_1_+2HD.png
cp /cygdrive/c/ProgramData/data/picons/Rai_1.png /cygdrive/c/ProgramData/data/picons/duplicati/Rai_1_+1HD.png
.
.

The command to use it is like:

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

/cygdrive/c/ProgramData/data/picons/ is the source of the files
/cygdrive/c/ProgramData/data/picons/duplicati/ is the destination of the files

I hope is more clear :slight_smile:

would something like this be helpful?
awk -f tapi..awk channels.xml where tapi.awk is:

BEGIN {
  srcD="/cygdrive/c/ProgramData/data/picons"
  dstD="/cygdrive/c/ProgramData/data/picons/duplicati"
}
/<!--begin_channel-->/ {srcT=1; next}
/<!--end_channel-->/ {srcT=0; next}
srcT {srcF=$0;next}
srcF {
   gsub(":","_",$1)
   print "cp " srcD "/" srcF " " dstD "/" $1
}

you can work on this code to make it more flexible integrating with the shell of your choice!

2 Likes

Maybe I was wrong to explain, the script is working under Ubuntu but not under CYGWIN.

Your cygwin's bash and awk versions probably differ from Ubuntu's. The exact output including error messages would seriously help us help you!

1 Like

No. No. No. No way! The command that you say is not executed correctly on CYGWYN cannot possibly work on any Linux or CYGWIN system.

As long as there are no slash ( / ) characters in the file named by the 1st command-line argument given to your script, the code vgersh99 suggested should work for you (as long as the note at the end of this post does not apply). If there could be one or more slash characters in that file, try replacing that line with:

awk -v Line="$line" '$0 ~ Line,/<!--begin_channel-->/' "$1" | grep -v "$line" | grep -v "^<" > xxx

or (using one process instead of 3):

awk -v Line="$line" '
/<!--begin_channel-->/ { p = 0 }
p && ! /^</
$0 ~ Line { p = 1 }' "$1" > xxx

Note, however, that none of these suggestions will work if the text in the file named by the 1st command-line argument to your script contains characters that are "special" in an extended or basic regular expression if the intent is to match them as literal characters.

But, of course, now that I see post #3, it is obvious that absolutely no chance that this script is going to produce any output at all similar to the output you say you want. With the sample input you have shown us, the output from the 1st awk command in your script will produce the output:

Rai_1.png

in the file named temp and the 1st grep in the 2nd awk pipeline will discard that output. There is nothing in your code that makes any attempt to create cp commands as output.

It is also interesting to note that your script requires four command-line arguments, but only the 1st two are used???

1 Like

I tried to follow the suggestions but I still can't run it :frowning: I am really bad about. If someone want also suggest a different solution is ok. What I need is one file like I published before and the oportunity to replace the character : with _ and the | with _

Wrong post