Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

i.e. for each .csv file, do the following:

  • Check if the file contains the string “HEADER” in the first line. If its missing, do the following:

    1. Display, “ERROR: Missing HEADER record for ”
    2. Rename the file from .csv to .head
    3. Proceed to process the next file
share|improve this question
1  
Ok, what's the question? –  Patrick Jul 4 '14 at 7:39

2 Answers 2

for f in *.csv; do
  IFS= read -r line < "$f" &&
    case $line in (*HEADER*) continue; esac

  printf >&2 'ERROR: Missing HEADER record for "%s"\n' "$f"
  mv -- "$f" "${f%.*}.head"
done
share|improve this answer
    
nice one! A question: Does -- need in mv when we have double quote? –  cuonglm Jul 4 '14 at 8:07
    
@Gnouc, yes, it's mv -- "$file" ... or mv "$option_or_file" ... or mv -- $file_patterns ... or mv $option_and_or_file_patterns ... –  Stéphane Chazelas Jul 4 '14 at 8:35

Try:

for _file in *.csv
do
    if ! grep -q 'HEADER' <(head -n 1 -- "$_file")
    then
        echo >&2 "ERROR: Missing HEADER record for $_file"
        # mv -- "$_file" "${_file%.*}.head"
        echo "$_file" "${_file%.*}.head"
    fi
done

Using echo line to verify if it works right, if everything ok, try mv line.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.