Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You can use awk:

$ awk -F',' '{sub(/[[:upper:]]+/,"",$2)}1' OFS=',' file
R1C1, 123abc, R1C3
R2C1, 456defg, R2C3
R3C1, 789hijkl, R3C3
share|improve this answer
    
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 at 3:22
2  
In this case, it can be sub(/[[:upper:]]+[[:digit:]]+/,"",$2). –  cuonglm Aug 1 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 at 3:49
1  
[[:upper:]]+ matches all of them. If you want string literal, ABC|DEFG|HIJK. –  cuonglm Aug 1 at 3:53

if the second field has capitals scattered,

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

Bash in combination with sed:

#!/bin/bash
cnt=0;
while IFS=, read a b c; 
do 
 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

produces:

R1C1 123abc R1C3
R2C1 456defg R2C3
R3C1 789hijkl R3C3
share|improve this answer
    
Downvoter comment? –  Ruslan Gerasimov Aug 1 at 10:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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