SED- 根据脚本中的 2 个条件将列从小写更改为大写

SED- Change column from lower to upper case based on 2 conditions from script

我必须根据运动列是射击或柔道的情况,将“名称”列从小写更改为大写。

到目前为止,anubhava 提供的以下代码在终端中有效,但在脚本中无效:

sed -E 's/^([^,]*,)([^,]+)((,[^,]*){5},(shooting|judo),)/\U\L/'

部分输入:

id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
388896171,Abdelraouf Benguit,ALG,male,1985-07-03,1.83,90,judo,0,0,0,
285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
969824503,Abdullah Alrashidi,IOA,male,1963-08-21,1.83,84,shooting,0,0,1,
897549624,Abdullah Hel Baki,BAN,male,1989-08-01,,,shooting,0,0,0,
153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,

预期输出:

    id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
    388896171,ABDELRAOUF BENGUIT,ALG,male,1985-07-03,1.83,90,judo,0,0,0,
    285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
    545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
    969824503,ABDULLAH ALRASHIDI,IOA,male,1963-08-21,1.83,84,shooting,0,0,1,
    897549624,ABDULLAH HEL BAKI,BAN,male,1989-08-01,,,shooting,0,0,0,
    153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,

此代码将进入脚本并使用 sed -f 执行。它需要与 sed.

脚本基本上是以下几行:

s/ESP/Spain/g
s/DEN/Denmark/g
s/NED/netherlands/g
s/^([^,]*,)([^,]+)((,[^,]*){5},(shooting|judo),)/\U\L/
/[0],[0],[0],$/d
1 c id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info

我这样调用脚本:

$ sed -f script3_3.sed athletesv22.csv

谢谢,

sed 不是正确的工具。你应该考虑 awk.

您可以使用这样的 awk 解决方案:

awk 'BEGIN {FS=OFS=","} NR > 1 &&  ~ /^(shooting|judo)$/ {
 = toupper()} 1' file.csv

id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
388896171,ABDELRAOUF BENGUIT,ALG,male,1985-07-03,1.83,90,judo,0,0,0,
285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
969824503,ABDULLAH ALRASHIDI,IOA,male,1963-08-21,1.83,84,shooting,0,0,1,
897549624,ABDULLAH HEL BAKI,BAN,male,1989-08-01,,,shooting,0,0,0,
153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,

要将更改内联保存到输入文件,请使用此 gnu-awk 解决方案:

awk -i inplace 'BEGIN {FS=OFS=","}
NR > 1 &&  ~ /^(shooting|judo)$/ { = toupper()} 1' file.csv

或者,如果您没有 gnu-awk,则使用:

awk 'BEGIN {FS=OFS=","} NR > 1 &&  ~ /^(shooting|judo)$/ {
 = toupper()} 1' file.csv > _tmp_ && mv _tmp_ file.csv

这个 gnu-sed 解决方案应该适合您:

sed -E 's/^([^,]*,)([^,]+)((,[^,]*){5},(shooting|judo),)/\U\L/' file.csv

id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
388896171,ABDELRAOUF BENGUIT,alg,male,1985-07-03,1.83,90,judo,0,0,0,
285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
969824503,ABDULLAH ALRASHIDI,ioa,male,1963-08-21,1.83,84,shooting,0,0,1,
897549624,ABDULLAH HEL BAKI,ban,male,1989-08-01,,,shooting,0,0,0,
153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,

Working Demo