LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Something not working quite right in arrays/parsing arrays.. (https://www.linuxquestions.org/questions/linux-newbie-8/something-not-working-quite-right-in-arrays-parsing-arrays-4175459928/)

sysmicuser 04-28-2013 08:26 PM

Something not working quite right in arrays/parsing arrays..
 
Guys,

Here is what is not working is I am unable to parse the array :(

Code:

N=0;
                for i in $(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -name "EAReturn*" -mmin +60 -print) ; do
                    input_files_array[$N]="$i"
                #  echo "$i"
                #  echo $file_name
                    let "N= $N + 1"
                done >> ${input_files}
                for i in "${input_files_array[@]}"
                do
                file_name=${input_file_array}|cut -d'/' -f7
                echo {file_name[$i]}
                done
            fi
            exit 0

Code:

+ N=0
++ find /data/integration/ -path '*/input/*' -type f -not -name 'EAReturn*' -mmin +60 -print
find: /data/integration/file.controldir/fileftp: Permission denied
+ for i in '$(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -name "EAReturn*" -mmin +60 -print)'
+ input_files_array[$N]=/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT
+ let 'N= 0 + 1'
+ for i in '$(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -name "EAReturn*" -mmin +60 -print)'
+ input_files_array[$N]=/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT
+ let 'N= 1 + 1'
+ for i in '$(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -name "EAReturn*" -mmin +60 -print)'
+ input_files_array[$N]=/data/integration/BulkVehicleFileProcessor/validate/input/BVUInputFilePOL000004.txt
+ let 'N= 2 + 1'
+ for i in '"${input_files_array[@]}"'
+ file_name=
+ cut -d/ -f7
+ echo '{file_name[/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT]}'
{file_name[/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT]}
+ for i in '"${input_files_array[@]}"'
+ file_name=
+ cut -d/ -f7
+ echo '{file_name[/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT]}'
{file_name[/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT]}
+ for i in '"${input_files_array[@]}"'
+ file_name=
+ cut -d/ -f7
+ echo '{file_name[/data/integration/BulkVehicleFileProcessor/validate/input/BVUInputFilePOL000004.txt]}'
{file_name[/data/integration/BulkVehicleFileProcessor/validate/input/BVUInputFilePOL000004.txt]}
+ exit 0

I would like to understand why my file_name variable is empty when I just want the name of file? I don't see anything wrong from syntax point of view.

Please assist here.

Thank you.

linosaurusroot 04-28-2013 08:42 PM

Your line
Code:

file_name=${input_file_array}|cut -d'/' -f7
seems to have some problems.

You have here input_file_array and everywhere else input_files_array.
You are piping to cut from a left hand side that produces no output.
Maybe you meant
Code:

file_name=$(echo ${input_file_array}|cut -d'/' -f7)
or similar?

chrism01 04-28-2013 08:54 PM

2nd loop; see embedded comments
Code:

for i in "${input_files_array[@]}"
do
  # Typo in array name, and in any case I think you want $i instead,
  # and enclose in $() to get name: file_name=$(echo $i|cut -d'/' -f7)
  file_name=${input_file_array}|cut -d'/' -f7
  # echo $file_name ...
  echo {file_name[$i]}
done

I'd also use single quotes instead of backslashes in 1st loop find param; easier to read (see the output you've got).
You might also use arithmetic (( )) instead of 'let'
http://wiki.bash-hackers.org/syntax/expansion/arith

EDIT: use basename instead of cut

sysmicuser 04-28-2013 08:59 PM

Thank you for your improvement , works better but still not quite right. It just outputs same file name :(

grail 04-28-2013 09:32 PM

Well I am curious about:
Code:

echo {file_name[$i]}
As file_name does not appear to be an array and $i is not an index but rather the name of a path, I am not sure I see how this would work??

sysmicuser 04-28-2013 09:38 PM

@chrism01.

Many thanks for the assistance. It seems there is a problem with how we are defining a file name with cut command.

Have a look at these ones.

Code:

++ echo /data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT
++ cut -d/ -f7
+ file_name=DDDHWBC0602012112012093600.TXT <-- correct
+ echo '{file_name[/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT]}'
{file_name[/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT]}
+ for i in '"${input_files_array[@]}"'
++ echo /data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT
++ cut -d/ -f7
+ file_name=Asset<-- incorrect finfact file name should be 280060340997.TXT
+ echo '{file_name[/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT]}'
{file_name[/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT]}
+ for i in '"${input_files_array[@]}"'
++ echo /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/temp/EAReturn20130423050714.dat
++ cut -d/ -f7
+ file_name=temp<-- incorrect , file name should be EAReturn20130423050714.dat
+ echo '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/temp/EAReturn20130423050714.dat]}'
{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/temp/EAReturn20130423050714.dat]}
+ for i in '"${input_files_array[@]}"'
++ echo /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat
++ cut -d/ -f7
+ file_name=restore<-- incorrect filename should be EAReturn20130410015021.dat
+ echo '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat]}'
{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat]}
+ for i in '"${input_files_array[@]}"'
++ echo /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat
++ cut -d/ -f7
+ file_name=EAReturn20130425013515.dat
+ echo '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat]}'
{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat]}

SO how to get around with these ones? I am not sure if there any way to extract the last last part of the string OR with some regular expression which defines file froma string as the one with extension as .TXT or .dat or *.jpg? how should we go about this?

chrism01 04-28-2013 09:46 PM

See my late EDIT: use basename cmd instead of cut; its what its designed to do.

sysmicuser 04-28-2013 09:47 PM

Slowly and steady wins up the race.
Code:

for i in "${input_files_array[@]}"; do  file_name=basename[$i]; echo ${file_name[$i]}; done

sysmicuser 04-28-2013 10:03 PM

doesn't work chrism01 :(

Code:

                for i in "${input_files_array[@]}"
                do
                file_name=$(echo $i|basename)
                echo {file_name[$i]}
                done

Output

Code:

basename: missing operand
Try `basename --help' for more information.
+ file_name=


sysmicuser 04-28-2013 10:32 PM

Guys,

Now I am becoming a little knowledgeable as to why are we using *two* loops when the job can be done within one loop itself ! ?

sysmicuser 04-28-2013 10:54 PM

What is the issue now?

Code:

                                N=0;
                                for i in $(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -wholename "*/restore/EAReturn*" -mmin +60 -print) ; do
                                input_files_array[$N]="$i"
                            #  echo "$i"
                                #      echo $file_name
                                        let "N= $N + 1"
                                echo -e "file_name=$(basename $i)\n"
                                echo -e {file_name[$i]}<-- I am interested in array so I can use it to insert in database ! hopefully...
                                done >> ${input_files}
                    fi

But output is not that smart enough !

Code:

+ let 'N= 165 + 1'
++ basename /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat
+ echo -e 'file_name=EAReturn20130410015021.dat\n'
+ echo -e '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat]}'
+ for i in '$(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -wholename "*/restore/EAReturn*" -mmin +60 -print)'
+ input_files_array[$N]=/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat
+ let 'N= 166 + 1'
++ basename /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat
+ echo -e 'file_name=EAReturn20130425013515.dat\n'
+ echo -e '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat]}' <here what i am getting is not interested in....

Probably time to give up....

chrism01 04-28-2013 11:20 PM

You seem confused about when you've got an array & when you don't...
If you only want the bare filenames, stored in an array arr1
Code:

N=0;
for i in $(find ${BASE_DIRECTORY} -path '*/input/*' -type f -not -wholename '*/restore/EAReturn*' -mmin +60 -print)
do
    # Store filename in new array                           
    arr1+=($(basename $i))
    (( N++ ))
done
echo "${arr1[@]}"

NB: single quotes are safe from interpolation by the shell when using eg wildcards,

sysmicuser 04-28-2013 11:24 PM

Yes chrism01 I am confused can you enlighten me please? I want to extract string(directory paths) and file names? What should I do. Please help me understand.

Thank you very much.

chrism01 04-29-2013 12:19 AM

Well, you can see how I got the filename using basename cmd; use dirname cmd to get the path if you want it as well.
You should bookmark and READ these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Use 'man cmdname' to lookup a cmd syntax or google it.

grail 04-29-2013 04:37 AM

I also like to use the parameter substitution method instead of basename:
Code:

file_name=${i##*/}

path_name=${i%/*}/



All times are GMT -5. The time now is 10:52 AM.