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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - delete from csv all the row if the first column is length >
# 1  
Old 03-23-2016
Bash - delete from csv all the row if the first column is length >

Hi guys,
i have a csv file like:
Code:
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 Smilie
Thanks all

Last edited by kamose; 03-23-2016 at 08:35 AM..
# 2  
Old 03-23-2016
Please use code tags as required by forum rules!

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

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-23-2016
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

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

I'm sorry for code tag. Your suggestion it work fine!
Thanks!!!!!!!Smilie
# 4  
Old 03-23-2016
a bash example:
Code:
#!/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


Last edited by rdrtx1; 03-23-2016 at 01:09 PM..
# 5  
Old 03-23-2016
@rdrtx1, that's a really slick solution. A simpler solution for a beginner to understand.
Code:
#!/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:
Code:
mv outfile infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to delete row in csv file on date range?

I want to delete row in csv file , which row value from 2009-10-01 to 2011-06-03 using script.my csv row data look like: 2009-10-01 2011-03-30 2011-03-31 2011-04-01 2011-06-03 2011-06-30 2011-07-01 2011-09-28 ... (7 Replies)
Discussion started by: rakibul639
7 Replies

2. Shell Programming and Scripting

Delete row if column matches

Hi, I have a long file in the format below. I want to delete the consecutive lines that contain the same value in column 1. I have tried awk '!x++' FS="," filename This has not worked. 14,MM709_BHP_DM,BHP,BHP_MC709_DM 19,OFFLINE,CHE,CHEV_MC773_DM 20,M33,BP,BP_MIM775_NS_DM ... (2 Replies)
Discussion started by: ndnkyd
2 Replies

3. Shell Programming and Scripting

Delete a row if either of column value is zero

Hi, My input file is this way 1.1 0.0 2.4 3.5 7.9 1.8 22.3 4.7 8.9 0.9 1.3 0.0 3.4 5.6 0.0 1.1 2.2 0.0 0.0 1.1 0.0 0.0 3.4 5.6 I would like to delete the entire row, if either of 2nd and 3rd columns are 0.0. Please note that my values are all decimal values. So, my output would... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Shell Programming and Scripting

delete a row with a specific value at a certain column

Hi, I want to delete rows that have 0 at column 4. The file looks like this: chr01 13 61 2 chr01 65 153 0 chr01 157 309 1 chr01 313 309 0 chr01 317 469 1 chr01 473 557 0 I want to delete all rows with a 0 at column 4 chr01 13 61 2 chr01 157 309 1 chr01 ... (3 Replies)
Discussion started by: kylle345
3 Replies

5. Shell Programming and Scripting

Delete row if a a particular column has more then three characters in it

Hi i have a data like hw:dsfnsmdf:39843 chr2 76219829 51M atatata 51 872389 hw:dsfnsmdf:39853 chr2 76219839 51M65T atatata 51 872389 hw:dsfnsmdf:39863 chr2 76219849 51M atatata 51 872389 hw:dsfnsmdf:39873 chr2 ... (3 Replies)
Discussion started by: bhargavpbk88
3 Replies

6. Shell Programming and Scripting

awk: Transpose csv row to column.

Hello, am I new to awk, and I am tryint to: INPUT FILE: "73423555","73423556","73423557","73423558","73423559" OUTPUT FILE: 73423555 73423556 73423557 73423558 73423559 My useless code so far: #!/bin/awk -F ',' BEGIN { i=0; } (8 Replies)
Discussion started by: drbiloukos
8 Replies

7. Shell Programming and Scripting

delete a row in csv file based on the date

Hi, I have a csv file with old data..i need to have only last 30 days from the current dateof data in the file.The fourth field in the file is a date field.i need to write a script to delete the old data by comparing the the fourth field with the (current date -30).I need to delete the rows in... (2 Replies)
Discussion started by: pals70423
2 Replies

8. Shell Programming and Scripting

How to Delete Last Row from .csv file in perl

Hi , I've a perl script to convert .xls to .csv .After conversion I want to delete first 28 and the last row from .csv file.Is there any efficent way to achive this both together. I'm deleting first 28 rows by using folllowing my perl code: exec " tail -n+28 infile.csv > outfile.csv ... (7 Replies)
Discussion started by: ajaypatil_am
7 Replies

9. Shell Programming and Scripting

Delete first row last column

Hi All, I am having following file and I want to delete 1 row last column. Current File Content: ================ procedure test421 put_line procedure test321 test421 procedure test521 test321 procedure test621 test521 Expected File Content: =========================== procedure... (3 Replies)
Discussion started by: susau_79
3 Replies

10. UNIX for Dummies Questions & Answers

Delete first row of csv file

I have a csv file, which is > 2 Gigs. I need to BCP that file to Sybase db , but I cant upload that b'caz first row of the file is failing. ( having some errors probably.) I can manually insert the first line into db & then I can upload the rest of the data in file, if i can delete the first row. ... (2 Replies)
Discussion started by: kedar.mehta
2 Replies
Login or Register to Ask a Question