сравнение строк с awk, по сравнению с в то время как считанная строка

Насколько я вижу, что это делает то, в чем я нуждаюсь (но я не знаю точно почему).

[Unit]
Description=Log Traffic
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target


[Service]
ExecStart=/usr/local/bin/perl /home/me/log_traffic.pl --stop
Type=oneshot
0
25.04.2013, 02:52
1 ответ

Следующее должно работать, обновленное для разделения пробела:

#!/usr/bin/awk -f
# NR is the current line number (doesn't reset between files)
# FNR is the line number within the current file
# So NR == FNR  takes only the first file
NR == FNR {
    # Mark the current line as existing, via an associative array.
    found[$0]=1

    # Skip to the next line, so we don't go through the next block
    next
}
{
    # Take the columns we're looking for
    cols = substr($0,115,11)

    # Strip whitespace (space and tab) from the beginning (^) and end ($) 
    gsub(/^[ \t]+/,"", cols)
    gsub(/[ \t]+$/,"", cols)

    # Check the associative array to see if this was in the first file
    # If so, print the full line
    if(found[cols]) print;
}       

Поместите его в файл и вызов с одним из следующих

awk -f script.awk patterns.txt full.txt
./script.awk patterns.txt full.txt
3
28.01.2020, 02:28
  • 1
    Ваш сценарий занял всего 3 секунды для завершения где шахта, принятая 17 минут. Спасибо somuch Kevin. –  user37774 24.04.2013, 19:48
  • 2
    Kevin, Может Вы объяснять код и его повторение особенно если (нашел [седла]), печать; и НОМЕР == FNR {нашел [0$] =1 следующий} –  user37774 24.04.2013, 20:07
  • 3
    @user37774 Несомненно, я добавил объяснение к коду. –  Kevin 24.04.2013, 20:17
  • 4
    Когда Вы делаете found[cols] Вы на самом деле выделяете и ни на что устанавливаете () found[cols]. Выполнение found[$0] (без =1), и затем протестируйте с if(cols in found) намного более эффективно (память и мудр CPU). –  Stéphane Chazelas 24.04.2013, 21:31

Теги

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