I am having a CSV file containing 3000+ lines in the following format:

R1C1, ABC123abc, R1C3
R2C1, DEFG456defg, R2C3
R3C1, HIJKL789hijkl, R3C3

I need to remove all the capital letters from the second column of the file so that it reads as follows:

R1C1, 123abc, R1C3
R2C1, 456defg, R2C3
R3C1, 789hijkl, R3C3

How can this be done?


You can use awk:

$ awk -F',' '{sub(/[[:upper:]]+/,"",$2)}1' OFS=',' file
R1C1, 123abc, R1C3
R2C1, 456defg, R2C3
R3C1, 789hijkl, R3C3
  • Thanks! What if I want to remove string before first alphabet in small caps? For example: R1C1, abc, R1C3 in this case. – Mandar Shinde Aug 1 '14 at 3:22
  • 2
    In this case, it can be sub(/[[:upper:]]+[[:digit:]]+/,"",$2). – cuonglm Aug 1 '14 at 3:27
  • In case, if I need to mention multiple strings in the regex part, like ABC,DEFG,HIJKL, then how can I do that? – Mandar Shinde Aug 1 '14 at 3:49
  • 1
    [[:upper:]]+ matches all of them. If you want string literal, ABC|DEFG|HIJK. – cuonglm Aug 1 '14 at 3:53

if the second field has capitals scattered,

awk -F ',' '{gsub(/[[:upper:]]/, X, $2)}1' OFS=, file

Bash in combination with sed:

while IFS=, read a b c; 
 echo -n $a ; cnt=$[cnt+1]; 
 sed  -e 's/[^,]*,\([^,]*\).*/\1/' file | 
 sed  -e 's/\([[:upper:]]\)//g'| 
 sed  -e  $cnt'q;d'|
 tr '\n' ' ' ;
 echo  $c; 
done  < file


R1C1 123abc R1C3
R2C1 456defg R2C3
R3C1 789hijkl R3C3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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