MyTetra Share
Делитесь знаниями!
How to print odd numbered fields from a text file using awk in Unix?
Время создания: 18.09.2015 11:19
Текстовые метки: awk
Раздел: root - Linux - Console - awk
Запись: Yurons/mytetra/master/base/1442564360i0ay0xs6nt/text.html на raw.github.com

t may be more clear if we reformat your awk code:

{
  s = "";
  for (i = 1; i <= NF; i += 2)
  {
    s = s ? s FS $i : $i
  }
  print s
}

First we have a loop over all odd numbers from 1 to NF inclusive. Inside that, we build a string s. Each loop iteration, if s is not empty we append FS $i; if it was empty we simply assign $i. The net effect of all this is to make s contain the text from all the odd-number fields, separated by FS. Finally, we print s.

A more concise form, without the ternary, would be:

{
  s = $1;
  for (i = 3; i <= NF; i += 2)
  {
    s = s FS $i
  }
  print s
}

 
MyTetra Share v.0.65
Яндекс индекс цитирования