Разделить столбцы на строки с помощью разделителя

Кажется, это работает. Я не уверен, есть ли более простой способ сделать это:

grep OpenFin LoginExInternal.txt >> LoginExcInternal.txt && 
echo $'\r' >> LoginExcInternal.txt && 
grep Chrome LoginExInternal.txt >> LoginExcInternal.txt &&
echo $'\r' >> LoginExcInternal.txt &&
grep MemoryUnderThreshold.txt memoryInfo:jsHeapSizeLimit:1 >> LoginExcInternal.txt
0
16.06.2020, 12:46
1 ответ

Предположим, вы сохранили этот AWK-скрипт какscript:

BEGIN {
                                # Populate an array that lists (is indexed
                                # by) the position of "dynamic" fields.
  split(dynamic,temp,",")
  for (i in temp)
    tosplit[temp[i]] = i
}
{
                                # Determine how many times the current
                                # line will be repeated...
  times = 1
                                # by counting how many times, for each field,
  for (f = 1; f <= NF; f++) {
                                # the "|" separator is replaced by itself.
    repl = gsub(/\|/, "&", $f)
    if ( (repl + 1 ) > times)
      times = (repl + 1 )
  }
                                # For each time the line has to be repeated:
  for (i = 1; i <= times; i++) {
    for (f = 1; f <= NF; f++) {
                                # every "dynamic" field is split on "|", and
                                # only the component which belongs to the
                                # current line repetition is printed;
      if (f in tosplit) {
        split($f, p, "|")
        printf( (f == NF ? "%s"ORS : "%s"OFS), p[i] )
      }
                                # all other fields are printed unchanged.
      else
        printf( (f == NF ? "%s"ORS : "%s"OFS), $f )
    }
  }
}

вы можете вызвать его как:

awk -v FS=',' -v OFS=',' -v dynamic=3,4,5 -f script source_file

Индексы столбцов, подлежащих разбиению, ("динамические" поля )передаются в awkкак переменная, содержащая список, -разделенный запятыми.

0
18.03.2021, 23:27

Теги

Похожие вопросы