删除txt中的特定字符
想象一下,我有下一个 txt 格式:
'20201': "a" ,
'20202': "e" ,
'20203': "i" ,
'20204': "o" ,
'20205': "u" ,
'20207': "ae" ,
'20209': "ai" ,
'20210': "ao"
当它是 0 时,我想擦除四位数字。所以预期的输出是:
'2021': "a" ,
'2022': "e" ,
'2023': "i" ,
'2024': "o" ,
'2025': "u" ,
'2027': "ae" ,
'2029': "ai" ,
'20210': "ao"
我在想这个:
awk -i inplace ' { for ( i = 1; i <= NF; ++i ) {
if ( $i == '0')
r = 1
}
}}
1 ' example.txt ```
回答
随着awk能不能请你以下,并写在GNU所示样品测试awk。
没有字段分隔符尝试:
awk 'substr($0,5,1)==0{ $0=substr($0,1,4) substr($0,6) } 1' Input_file
或使用字段分隔符尝试以下操作:要专门处理此处的第一个字段。
awk '
BEGIN{
FS=OFS=":"
}
substr($1,5,1)==0{
$1=substr($1,1,4) substr($1,6)
}
1
' Input_file
> temp && mv temp Input_file一旦您对上述命令的输出感到满意,就可以将输出保存到 Input_file 本身。
说明:为以上添加详细说明。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this program from here.
FS=OFS=":" ##Setting FS and OFS as colon here.
}
substr($1,5,1)==0{ ##Checking condition if 5th character is 0 then do following.
$1=substr($1,1,4) substr($1,6) ##Setting sub string of 1st 4 characters then mentioning characters from 6th character to last of 1st field here.
}
1 ##1 will print current line.
' Input_file ##Mentioning Input_file name here.