发现只有空白列在一个文本文件,并用独特的分离器替换它们 [英] Finding columns with only white space in a text file and replace them with a unique separator
问题描述
我有一个这样的文件:
AAA B B CCC 345
DDD FGT˚Fü3456
ËřDER德5 674
正如你可以看到,我们可以分开列唯一方法是通过发现只有一个或多个空格列。我们怎样才能识别这些列,像独特的分离,
替换它们。
AAA,B B,CCC,345
DDD,FGT,女U,3456
ËR,德,德,5 674
请注意:结果
如果我们发现所有的连续列与一个或多个空格(没有别的)和替换它们,
(所有列),问题会得到解决。
对这个问题的解释更好 josifoski
:
每字符点阵块,如果都是'空间',那么所有的块应具有一个垂直替换,在每一行。
$猫tst.awk
BEGIN {FS = OFS =; ARGV [ARGC] = ARGV [ARGC-1]; ARGC ++}
NR == FNR {
对于(i = 1; I< = NF;我++){
如果($ I ==){
空间[I]
}
其他{
非空格[I]
}
}
下一个
}
FNR == 1 {
为(以非空格我){
删除空间[I]
}
}
{
为(i的空间){
$ I =,
}
GSUB(/ + /,,)
打印
}$ AWK -f tst.awk文件
AAA,B B,CCC,345
DDD,FGT,女U,3456
ËR,德,德,5 674
I have a file like this:
aaa b b ccc 345
ddd fgt f u 3456
e r der der 5 674
As you can see the only way that we can separate the columns is by finding columns that have only one or more spaces. How can we identify these columns and replace them with a unique separator like ,
.
aaa,b b,ccc,345
ddd,fgt,f u,3456
e r,der,der,5 674
Note:
If we find all continuous columns with one or more white spaces (nothing else) and replace them with ,
(all the column) the problem will be solved.
Better explanation of the question by josifoski
:
Per block of matrix characters, if all are 'space' then all block should be replaced vertically with one , on every line.
$ cat tst.awk
BEGIN{ FS=OFS=""; ARGV[ARGC]=ARGV[ARGC-1]; ARGC++ }
NR==FNR {
for (i=1;i<=NF;i++) {
if ($i == " ") {
space[i]
}
else {
nonSpace[i]
}
}
next
}
FNR==1 {
for (i in nonSpace) {
delete space[i]
}
}
{
for (i in space) {
$i = ","
}
gsub(/,+/,",")
print
}
$ awk -f tst.awk file
aaa,b b,ccc,345
ddd,fgt,f u,3456
e r,der,der,5 674
这篇关于发现只有空白列在一个文本文件,并用独特的分离器替换它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!