替换文本第一栏中的特定字符 [英] Replacing specific characters in first column of text
问题描述
我有一个文本文件,我试图将第一列中的特定字符(.)替换为另一个字符(-).每个字段均以逗号分隔.有些行的最后3列为空,因此末尾有3个逗号.
I have a text file and I'm trying to replace a specific character (.) in the first column to another character (-). Every field is delimited by comma. Some of the lines have the last 3 columns empty, so they have 3 commas at the end.
文本文件示例:
abc.def.ghi,123.4561.789,ABC,DEF,GHI
abc.def.ghq,124.4562.789,ABC,DEF,GHI
abc.def.ghw,125.4563.789,ABC,DEF,GHI
abc.def.ghe,126.4564.789,,,
abc.def.ghr,127.4565.789,,,
我尝试使用awk替换'.'.在第一列中用'-',然后打印出内容.
What I tried was using awk to replace '.' in the first column with '-', then print out the contents.
ETA:尝试了sarnold的建议,并得到了我想要的输出.
ETA: Tried out sarnold's suggestion and got the output I want.
ETA2:我本来可以有更长的第一列.有没有办法只更改前3个'.第一列中的-",因此我得到了输出
ETA2: I could have a longer first column. Is there a way to change ONLY the first 3 '.' in the first column to '-', so I get the output
abc-def-ghi-qqq.www,123.4561.789,ABC,DEF,GHI
abc-def-ghq-qqq.www,124.4562.789,ABC,DEF,GHI
abc-def-ghw-qqq.www,125.4563.789,ABC,DEF,GHI
abc-def-ghe-qqq.www,126.4564.789,,,
abc-def-ghr-qqq.www,127.4565.789,,,
推荐答案
.
是任何字符"的正则表达式.用\
进行转义,表示.
:
.
is regexp notation for "any character". Escape it with \
and it means .
:
$ awk -F, '{gsub(/\./,"-",$1); print}' textfile.csv
abc-def-ghi 123.4561.789 ABC DEF GHI
abc-def-ghq 124.4562.789 ABC DEF GHI
abc-def-ghw 125.4563.789 ABC DEF GHI
abc-def-ghe 126.4564.789
abc-def-ghr 127.4565.789
$
默认情况下,输出字段分隔符为空格.设置OFS = ","
进行设置:
The output field separator is a space, by default. Set OFS = ","
to set that:
$ awk -F, 'BEGIN {OFS=","} {gsub(/\./,"-",$1); print}' textfile.csv
abc-def-ghi,123.4561.789,ABC,DEF,GHI
abc-def-ghq,124.4562.789,ABC,DEF,GHI
abc-def-ghw,125.4563.789,ABC,DEF,GHI
abc-def-ghe,126.4564.789,,,
abc-def-ghr,127.4565.789,,,
这仍然允许更改多个字段:
This still allows changing multiple fields:
$ awk -F, 'BEGIN {OFS=","} {gsub(/\./,"-",$1); gsub("1", "#",$2); print}' textfile.csv
abc-def-ghi,#23.456#.789,ABC,DEF,GHI
abc-def-ghq,#24.4562.789,ABC,DEF,GHI
abc-def-ghw,#25.4563.789,ABC,DEF,GHI
abc-def-ghe,#26.4564.789,,,
abc-def-ghr,#27.4565.789,,,
我不知道-OFS,
做什么,但是它不是受支持的命令行选项;我用它来设置输出字段分隔符是我的一个错误.在awk
程序中设置OFS
效果很好.
I don't know what -OFS,
does, but it isn't a supported command line option; using it to set the output field separator was a mistake on my part. Setting OFS
within the awk
program works well.
这篇关于替换文本第一栏中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!