3

我有一个有点长的 awk 脚本,我想在文件的“右侧”附加两列。我正在处理以下文件:

Node    temporary_Temperature   steady_Temperature  temporary_Sight steady_Sight    
1                   x                   x                   -                   -                   
2                   x                   x                   -                   -                   
3                   -                   -                   -                   -                   
4                   -                   -                   -                   -                   
5                   x                   x                   x                   -                   
6                   -                   -                   -                   -                   
7                   -                   -                   -                   -                   
8                   -                   -                   -                   -                   
9                   -                   -                   -                   -                   
10                  -                   -                   -                   -                   
11                  -                   -                   -                   -                   
12                  -                   -                   -                   -                   
13                  x                   x                   -                   -                   
14                  x                   x                   -                   -                   
15                  x                   -                   -                   -                   
16                  -                   -                   -                   -                   

数据已经写入一个文件,我想使用 awk 迭代这个文件中的行,比方说,追加两列。在标题行,我想附加列fooand bar,然后根据其他内容附加xor到每一行。-我怎样才能做到这一点?由于我处于较长脚本的中间,除非有某种方法可以从 awk 脚本中调用 sed 并打印到同一个文件,否则我不想使用 sed?

4

2 回答 2

4

您在这里可能面临的问题是,每当 awk 修改字段时,它都会重写您的$0.,用 OFS 替换 IFS。如果您的 FS 是“任意数量的空白”(默认 FS),并且它被替换为“空格”(默认 OFS),您将丢失格式。

展示:

[ghoti@pc ~]$ printf 'one    two\tthree\n'
one    two      three
[ghoti@pc ~]$ printf 'one    two\tthree\n' | awk '{$4 = "four"} 1'
one two three four

如果您真的不想修改每一行的字段,而只想附加一个字符串,则可以这样做而无需重写字段分隔符:

[ghoti@pc ~]$ printf 'one    two\tthree\n' | awk '{print $0 "\tfour"}'
one    two      three   four

如果没有看到您的脚本或您想要附加的数据类型,我很难提供我知道将适用于您的情况的具体示例,但这里有一个示例可以帮助您入门:

  NR == 1 {
    print $0 "\tfoo\tbar";
    next;
  }
  {
    print $0 "\t" (conditionone ? "x" : "-") "\t" (conditiontwo ? "x" : "-");
  }

If you need help crafting the conditions, please include them in your question. I've used short-forms in the code above; you could obviously space things out and make multiple-line prints using printf(), or set variables based on the two conditions to be included in your print.

As an alternative, you could just re-do your formatting as you go through the input data. For example (assuming the trailing spaces in your question are really what your input file looks like):

BEGIN {
  fmt="%-20s%-20s%-20s%-20s%-20s%-20s%s\n";
}

NR == 1 {
  printf("%s%12s%-20s%-20s\n", $0, " ", "foo", "bar");
  next;
}

{ foo="-"; bar="-"; }

conditionone { foo="x"; }
conditiontwo { bar="x"; }

{
  printf(fmt, $1, $2, $3, $4, $5, foo, bar);
}

You'd probably have to fiddle with your spacing on line 1.

于 2013-02-07T15:36:28.627 回答
1

If it's the formatting that's a problem, pipe the output through column -t

Example:

awk '
    NR==1 {print $0, "foo", "bar"; next} 
    {print $0, ($2=="x"?"-":"x"), ($4=="x"?"-":"x")}
' file | column -t

outputs

Node  temporary_Temperature  steady_Temperature  temporary_Sight  steady_Sight  foo  bar
1     x                      x                   -                -             -    x
2     x                      x                   -                -             -    x
3     -                      -                   -                -             x    x
4     -                      -                   -                -             x    x
5     x                      x                   x                -             -    -
6     -                      -                   -                -             x    x
7     -                      -                   -                -             x    x
8     -                      -                   -                -             x    x
9     -                      -                   -                -             x    x
10    -                      -                   -                -             x    x
11    -                      -                   -                -             x    x
12    -                      -                   -                -             x    x
13    x                      x                   -                -             -    x
14    x                      x                   -                -             -    x
15    x                      -                   -                -             -    x
16    -                      -                   -                -             x    x
于 2013-02-07T23:39:21.233 回答