我有这样的文件,前三行是正确的,以下行是错误的。我需要在第一列和第二列之间为错误的行添加空格。没有别的东西可以改变。我试着用awk。请帮帮我。谢谢。
答案 0 :(得分:1)
如果只有空格,则使用反向字段:
awk -F'[^ ]*' 'length($2)<4{sub($2," ")}1' file
或更改每一行,将第一行作为参考:
awk -F'[^ ]*' 'NR==1{f=$2}{sub($2,f)}1' file
或者只是将每一行更改为第一列和第二列之间的4个空格:
awk -F'[^ ]*' '{sub($2," ")}1' file
---- ----编辑
第一个的解释。使用&#34;列&#34;在这个解释中我的意思是列,就好像使用了默认字段分隔符:
awk -F'[^ ]*' ' # Use any set of non-space characters ([^ ]*) as field separator.
# This means that the first field ($1) becomes an empty field
# (before the first column and the second field ($2) contains the
# spaces between the first column and the second column.
length($2)<4 { # If the length of that field is less then 4 (the nr. of spaces)
sub($2," ") # Then substitute the first occurrence of those spaces in the
# record with 4 spaces (this does not change the format).
}
1 # Print the record.
' file
答案 1 :(得分:0)
您可以使用此sed:
$ sed -r '4,$s/^([^ ]*)/\1 /' file
060A 2012075 2014035 27.0361 -80.3618 0.0090 Indiantown, FL, USA - - 0.0000 0.0000 04/04/14 11:36:27
060A 2012075 2014035 27.0361 -80.3618 0.0090 Indiantown, FL, USA - - 0.0000 0.0000 04/04/14 11:36:27
058A 2012071 2013313 27.0569 -81.8049 0.0150 Arcadia, FL, USA - - 0.0000 0.0000 04/09/14 10:20:19
059A 2012072 2013313 26.9671 -81.1440 0.0110 Moore Haven, FL, USA - - 0.0000 0.0000 04/09/14 10:20:19
059Z 2012073 2013315 26.3373 -81.4432 0.0080 Ave Maria, FL, USA - - 0.0000 0.0000 04/09/14 10:20:19
060A 2012075 2014035 27.0361 -80.3618 0.0090 Indiantown, FL, USA - - 0.0000 0.0000 04/09/14 10:20:19
060Z 2012134 2013317 26.4062 -80.5560 0.0090 West Palm Beach, FL, USA - - 0.0000 0.0000 04/09/14 10:20:19
-r
允许使用()
代替\( \)
来抓取群组。如果你不使用它,只需使用另一种方式。4,$s/^([^ ]*)/\1 /'
捕获,从第4行到第一行,第一个块并用一个空格打印回来。