StillLearningHowtodoit
StillLearningHowtodoit

Reputation: 141

Modify in place All column on a csv file except the Header

I have a csv file like this:

   Id EMAIL Service Number
    1 [email protected] http 345 
    2 [email protected] dns 4 
    3 [email protected] ftp 4568 

I want the output to be like this:

   Id EMAIL Service Number
    1 [email protected] http 345 
    2 [email protected] dns 4 
    3 [email protected] ftp 4568 

I want to change the second field into lower-case and only the second field.

At the moment i managed to do it with awk, but it modify the second field of the first line too by lowering it to email ( i want the EMAIL to stay uppercase in this case but lower all the column )

awk '$2 = tolower($2)' test.csv >  tmp & mv tmp test.csv 

Output:

   Id email Service Number
    1 [email protected] http 345 
    2 [email protected] dns 4 
    3 [email protected] ftp 4568 

Question 2:

Is there a way after that to trigger the Field

EMAIL instead of doing $2 ?

edit to clear things:

In the case which the Field EMAIL ( and all the values in this column ) are not in the second field but in n-field, is there a way to trigger this field directly by searching the EMAIL and lower case the column ?

Upvotes: 1

Views: 288

Answers (1)

dash-o
dash-o

Reputation: 14432

Possible to use awk NR to select only records 2 and above

Side note: the file is not "CSV", on surface, From the sample data (and the awk script), it is space separated or tab separated

The script is written for verbosity. Could be made more compact, if needed.

# Single line
awk 'NR > 1 { $2 = tolower($2) } { print }' < input-file-name > tmp && mv tmp input-file-name

# Multi-line
awk '
# Replace $2 on line 2+ with the lowercae
NR > 1 { $2 = tolower($2) }
# Print all lines
{ print }
' < input-file-name

Upvotes: 1

Related Questions