在shell中修改此脚本

时间:2015-02-12 18:50:14

标签: shell

我有这个脚本需要读取coulmn中的所有字段并在它可以达到第二列之前进行验证

Name, City

Joe, Orlando
Sam, Copper Town
Mike, Atlanta

因此脚本应该读取整个名称列(从上到下)并在移动到第二列之前验证null。它不应该逐行阅读。请添加一些关于如何修改/更正

的指针
 # Read all files.  no file have spaces in their names


for file in /export/home/*.csv ; do
  # init two variables before processing a new file
 $date_regex = '~(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d~';
 FILESTATUS=GOOD
 FIRSTROW=true
# process file 1 line a time, splitting the line by the
# Internal Field Sep ,
 cat "${file}" | while IFS=, read field1 field2 field3 field4; do
  # Skip first line, the header row

  if [ "${FIRSTROW}" = "true" ]; then
     FIRSTROW=FALSE
     # skip processing of this line, continue with next record
     continue;
  fi

  #different validations
  if [[ ! -n "$field1" ]]; then
  ${FILESTATUS}=BAD
     # Stop inner loop
     break
  fi
  #somecheckonField2
      if [[ ! -n "$field2"]]  && ("$field2" =~ $date_regex) ; then
     ${FILESTATUS}=BAD
     # Stop inner loop
     break
  fi

      if [[ ! -n "$field3" ]] && (("$field3" != "S") || ("$field3" != "E")); then
     ${FILESTATUS}=BAD
     # Stop inner loop
     break
  fi

      if [[ ! -n "$field4" ]] || (( ${#field4} < 9 || ${#field4} > 11 )); then
     ${FILESTATUS}=BAD
     # Stop inner loop
     break
  fi


done

 if [ ${FILESTATUS} = "GOOD" ] ; then

  mv ${file} /export/home/goodFile


 else
  mv ${file} /export/home/badFile
fi

完成

2 个答案:

答案 0 :(得分:1)

这是尝试使用awk脚本执行原始脚本尝试执行的操作:

#!/usr/bin/awk -f

# fields separated by commas
BEGIN { FS = "," }

# skip first line
NR == 1 { next }

# check for empty fields
$1 == "" || $2 == "" || $3 == "" || $4 == "" { exit 1 }

# check for "valid" date (urk... doing this with a regex is horrid)
# it would be better to split it into components and validate each sub-field,
# but I'll leave that as a learning exercise for the reader
$2 !~ /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9][0-9]$/ { exit 1 }

# third field should be either S or E
$3 !~ /^[SE]$/ { exit 1 }

# check the length of the fourth field is between 9 and 11
length($4) < 9 || length($4) > 11 { exit 1 }

# if we haven't found problems up to here, then things are good
END { exit 0 }

保存在例如validate.awk,并在其上设置可执行位(chmod +x validate.awk),然后您可以执行以下操作:

if validate.awk < somefile.txt
then
  mv somefile.txt goodfiles/
else
  mv somefile.txt badfiles/
fi

答案 1 :(得分:1)

这个awk会读取整个文件,然后你可以在END块中进行验证:

for file in /export/home/*.csv ; do
    awk -F', ' '
        # skip the header and blank lines
        NR == 1 || NF == 0 {next}

        # save the data
        { for (i=1; i<=NF; i++) data[++nr,i] = $i }

        END {
            status = "OK"

            # verify column 1
            for (lineno=1; lineno <= nr; lineno++) {
                if (length(data[lineno,1]) == 0) {
                    status = "BAD" 
                    break
                }
            }
            printf "file: %s, verify column 1, status: %s\n", FILENAME, status

            # verify other columns ...
        }
    ' "$file"
done