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

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.35
Яндекс индекс цитирования