Search code examples
linuxbashshelletl

Is there a way to convert one column in csv file to upper using shell commands?


Please help me with a BASH code which targets a particular column in a csv file and converts it to upper. For instance, if file_a.csv has the following columns:

man,woman,boy,girl
woman,man,boy,girl
boy,girl,man,woman
girl,boy,woman,man

I want to convert column 2 to upper in order to have:

man,WOMAN,boy,girl
woman,MAN,boy,girl
boy,GIRL,man,woman
girl,BOY,woman,man

Thanks for your help


Solution

  • You can accomplish this with sed:

    sed 's/[^,]*/\U&/2' file_a.csv
    
    • This will replace the 2nd string with with zero or more non-comma characters with it's uppercase equivalent