Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2003
    Location
    Los Angeles
    Posts
    18

    Unanswered: Prevent word splitting with file with spaces in name

    Hello,

    I have a script that "validates" a ZIP file that look like this

    Code:
    AAA_20120801.zip =>
    x~back end~20120801.TXT
    y~time in~20120801.TXT
    z~heat_chamber~20120801.TXT
    AAA_20120801.ctl
    My task is to compare its contents (i.e the list of files contained inside) with the control file that is provided inside the ZIP file itself.

    Since we've started receiving files with spaces in their name, I prevented the OS from word splitting file names when I view the ZIP's contents like so

    Code:
    FIRST_Array=(); while read length date time filename; do FIRST_Array+=( "$filename" ); echo -e "$filename"; 
    done < <(/usr/bin/unzip -qql AAA_20120801.zip)
    When I try to do the same with the control file,

    Code:
    SECOND_Array=(); while read filename; do SECOND_Array+=( "$filename" ); echo -e "$filename"; done < <(/usr/bin/unzip -p AAA_20120801.zip AAA_20120801.ctl )
    SECOND_Array() correctly outputs the file names in the control files but it also output the file sizes listed in the control file
    Code:
    x~back end~20120801.TXT 2KB
    y~time in~20120801.TXT 2KB
    z~heat_chamber~20120801.TXT 2KB
    and my array comparison (diff -q) fails.

    I tried adding this bit of Awk() code to remove the file size field but it brings word splitting back!

    Code:
    SECOND_Array=(); while read filename; do SECOND_Array+=( "$filename" ); echo -e "$filename"; done < <(/usr/bin/unzip -p AAA_20120801.zip AAA_20120801.ctl |awk '{print $1}')

    How can I remove the file size info and prevent word splitting? Any ideas?

    Thank you.

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Set IFS equal to ~.

    data
    Code:
    abc~def ghi~123
    Code:
    IFS="~"
    while read a b c
    do
    echo $a
    echo $b
    echo $c
    done<data
    Output
    Code:
    abc
    def ghi
    123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •