kamose
March 23, 2016, 6:07am
1
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
Thanks all
RudiC
March 23, 2016, 6:26am
2
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
kamose
March 23, 2016, 6:34am
3
I'm sorry for code tag. Your suggestion it work fine!
Thanks!!!!!!!
rdrtx1
March 23, 2016, 9:23am
4
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
jgt
March 23, 2016, 10:26am
5
@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