Search code examples
bashshellfor-loopawkcut

Matching length of each column of csv in bash


data.dat:
001,Sam,SA
002,Tom,SA
003,Lara,WI
004,Mhd,Pak

lookup.dat:
p_id,3
p_name,3
p_team,2

How can i write a shell script to validate the lenght of each field in data.dat by checking the respective lenths of the columns in lookup file.

if error(length not matching) as in case of p_id 003 & 004 display the record no and the column name.


Solution

  • You can try this :

    source <(sed 's/,/=/' lookup.dat)
    while IFS=',' read -r id name team; do
      (( i++ ))
      line="$id $name $team, line $i : different size"
      [ ${#id} == $p_id ] || echo "$line id"
      [ ${#name} == $p_name ] || echo "$line name"
      [ ${#team} == $p_team ] || echo "$line team"
    done < data.dat
    

    Output :

    003 Lara WI, line 3 : different size name
    004 Mhd Pak, line 4 : different size team
    

    The loop compares for each lines the size of fields with the sizes referenced in lookup.dat.

    If size differs the field is printed with line number.

    Update :

    As suggested, i added -r to the read command to prevent backslash interpretation while reading datas.

    Variable names have been hard coded for better readability. If number and/or type of variables/datas can change, prefer the Ed Morton answer.