Bash - delete from csv all the row if the first column is length >

Hi guys,
i have a csv file like:

USERID;COG;DESCR;FIL;OFF
user001;user;test1;001;A01
user002;user;test2;002;A02
user0003;user;test3;003;A03
user004;user;test4;004;A04
user0005;user;test5;005;A05

etc..

I need to read line for line and, if value of first column is > 7 char (in this example are user0003 and user0005), i need to delete row (in the same file) and print on log file this deleted rows.

I'm noob of bash.... be patient :slight_smile:
Thanks all

Please use code tags as required by forum rules!

awk -F";" 'length($1) > 7 {print > "log";next} 1' file > TMP && mv TMP file
1 Like

I'm sorry for code tag. Your suggestion it work fine!
Thanks!!!!!!!:b:

a bash example:

#!/bin/bash

rm -f log
ex infile << EDIT
$(
w="q!"
while IFS=";" read a b
do
   [[ ${#a} -gt 7 ]] && {
      echo "$a;$b" >> log
      echo "/^$a;/d"
      w="wq!"
   }
done < infile
echo "$w"
)
EDIT

@rdrtx1, that's a really slick solution. A simpler solution for a beginner to understand.

#!/bin/bash 
rm -f log
rm -f output
while IFS=";" read a b 
do    
   if [ ${#a} -gt 7 ]
   then
      echo "$a;$b" >> log
   else
      echo "$a;$b" >>output
   fi
done < infile 

Once you are satisfied that log and outfile contain the correct results:

mv outfile infile