删除空格并用空格更改符号

时间:2019-03-07 09:00:18

标签: shell unix awk

我有一个包含数千行的文件。行示例是:

205.188.213.249:193.219. 43. 13:193.219. 62.126:   82   76:         12:       2868
193.219.168. 18:206.126.  6. 38: 62. 40.103.217:    4   82:         11:        701
193.219. 52.163:217. 44.206.181: 62. 40.103.217:   76   82:          9:        531
193.219. 74.113:195. 22.175.  2:193.219. 62.126:    0   76:         29:      10396
193.219. 32. 13:195. 34. 96.  3: 62. 40.103.217:    0   82:          1:        227

我需要:
1)删除IP地址编号之间的空格;
2)将此符号:更改为space

它应该看起来像这样:

205.188.213.249 193.219.43.13 193.219.62.126 82 76 12 2868

我正在尝试使用以下命令:tr -d ' ' | tr ':' ' ' myfile,但不起作用。

4 个答案:

答案 0 :(得分:0)

请您尝试以下操作(已通过提供的示例和GNU awk进行了测试)。

awk '{gsub(/:/," ");gsub(/\. +/,".");gsub(/ +/," ")} 1' Input_file

说明: 在此处使用gsub进行全局替换。第一个用空格全局替换冒号,然后用DOT全局替换DOT。最后用单个空格全局替换一个或多个空格。然后提及1,让awk知道要打印已编辑/未编辑的行。

awk处理正则表达式/条件然后操作的方法。如果任何正则表达式/条件为TRUE,则将执行其旁边提到的操作。在这种情况下,我没有提到任何操作,因此默认情况下会打印当前行。

关于gsub的人工录入中的awk

   gsub(r, s [, t])        For each substring matching the regular expression r in the string t, substitute the string s, and return the number of substitutions.  If t is not supplied, use $0.   An  &  in  the
                           replacement text is replaced with the text that was actually matched.  Use \& to get a literal &.  (This must be typed as "\\&"; see GAWK: Effective AWK Programming for a fuller dis-
                           cussion of the rules for &’s and backslashes in the replacement text of sub(), gsub(), and gensub().)


EDIT1: :现在添加sed解决方案(已在GNU sed中进行了测试)。

sed -E 's/:/ /g;s/\. +/\./g;s/ +/ /g' Input_file

答案 1 :(得分:0)

尝试一下:

awk -F: '{for (i=1;i<=NF;i++) if (split($i,tmp,".") == 4) gsub(/[[:space:]]/,"",$i); else {gsub(/^[[:space:]]+|[[:space:]]+$/,"",$i);gsub(/[[:space:]]+/,OFS,$i);}}1' myfile

放入几行并添加说明:

awk -F: '{                                             ## -F: assign : as separator
    for (i=1;i<=NF;i++) {                              ## iterate every field
        if (split($i,tmp,".") == 4) {                  ## If split with . yields four parts, then it is an ip.
            gsub(/[[:space:]]/,"",$i);                 ## Remove every space in the IP field.
        } else {                                       ## otherwise it is not an IP
            gsub(/^[[:space:]]+|[[:space:]]+$/,"",$i); ## replace the spaces at the beginning or ending of each fields. Equals trim in some languages.
            gsub(/[[:space:]]+/,OFS,$i);               ## Replace multiple places between with one space.
        }                                              
    }                                                  ## Below 1 is to print line
}1' myfile                                             

这将更加兼容,也可以处理TAB,并且空格的位置不受限制。

答案 2 :(得分:0)

使用Perl

 perl -lpe 's/\.\s+(?=\d+)/./g;s/:/ /g; s/\s+/ /g ' input_file

使用您给定的输入

$ cat edgaras.txt
205.188.213.249:193.219. 43. 13:193.219. 62.126:   82   76:         12:       2868
193.219.168. 18:206.126.  6. 38: 62. 40.103.217:    4   82:         11:        701
193.219. 52.163:217. 44.206.181: 62. 40.103.217:   76   82:          9:        531
193.219. 74.113:195. 22.175.  2:193.219. 62.126:    0   76:         29:      10396
193.219. 32. 13:195. 34. 96.  3: 62. 40.103.217:    0   82:          1:        227

$ perl -lpe 's/\.\s+(?=\d+)/./g;s/:/ /g; s/\s+/ /g ' edgaras.txt
205.188.213.249 193.219.43.13 193.219.62.126 82 76 12 2868
193.219.168.18 206.126.6.38 62.40.103.217 4 82 11 701
193.219.52.163 217.44.206.181 62.40.103.217 76 82 9 531
193.219.74.113 195.22.175.2 193.219.62.126 0 76 29 10396
193.219.32.13 195.34.96.3 62.40.103.217 0 82 1 227

$

答案 3 :(得分:0)

您可以尝试使用sed:

sed '
  :A
    s/\([^[:blank:]]*\)[[:blank:]]\(.*\.[^\.]*\)/\1\2/
    tA
  s/:\?[[:blank:]]\{1,\}\|:/ /g
' infile