AWK脚本输出不正确-列中重复

时间:2018-10-19 15:44:16

标签: awk

我正在尝试提取多个脚本文件的描述,并在描述中包含脚本的文件名。我的问题是该命令在第一列中产生文件名的多个重复项。您能否建议如何删除重复项?

我也应该从stackoverflow复制此代码并对其进行修改,但是我不是100%理解它。如果您能解释gsub的功能以及$0e=1e==1的功能,将不胜感激。

我的代码

awk '/\]\]/{e=0}/description\ =\ \[\[/{gsub("^.*description ","",$0);e=1}{if(e==1){print FILENAME, $0}}' file

输入

local stdnse    = require "stdnse"
local shortport = require "shortport"
local tn3270    = require "tn3270"
local brute     = require "brute"
local creds     = require "creds"
local unpwdb    = require "unpwdb"
local nmap = require "nmap"
local string = require "string"

description = [[
TSO User ID enumerator for IBM mainframes (z/OS). The TSO logon panel
tells you when a user ID is valid or invalid with the message:
 <code>IKJ56420I Userid <user ID> not authorized to use TSO</code>.

The TSO logon process can work in two ways:
1) You get prompted with <code>IKJ56700A ENTER USERID -</code>
   to which you reply with the user you want to use.
   If the user ID is valid it will give you a normal
   TSO logon screen. Otherwise it will give you the
   screen logon error above.
2) You're given the TSO logon panel and enter your user ID
   at the <code>Userid    ===></code> prompt. If you give
   it an invalid user ID you receive the error message above.

This script relies on the NSE TN3270 library which emulates a
TN3270 screen for NMAP.

TSO user IDs have the following rules:
 - it cannot begin with a number
 - only contains alpha-numeric characters and @, #, $.
 - it cannot be longer than 7 chars
]]

---
-- @args tso-enum.commands Commands in a semi-colon seperated list needed
-- to access TSO. Defaults to <code>tso</code>.
--
-- @usage
-- nmap --script=tso-enum -p 23 <targets>
--
-- @usage
-- nmap -sV -p 9923 10.32.70.10 --script tso-enum --script-args userdb=tso_users.txt,tso-enum.commands="logon applid(tso)"

实际输出

tso-enum.nse = [[
tso-enum.nse TSO User ID enumerator for IBM mainframes (z/OS). The TSO logon panel
tso-enum.nse tells you when a user ID is valid or invalid with the message:
tso-enum.nse  <code>IKJ56420I Userid <user ID> not authorized to use TSO</code>.
tso-enum.nse 
tso-enum.nse The TSO logon process can work in two ways:
tso-enum.nse 1) You get prompted with <code>IKJ56700A ENTER USERID -</code>
tso-enum.nse    to which you reply with the user you want to use.
tso-enum.nse    If the user ID is valid it will give you a normal
tso-enum.nse    TSO logon screen. Otherwise it will give you the
tso-enum.nse    screen logon error above.
tso-enum.nse 2) You're given the TSO logon panel and enter your user ID
tso-enum.nse    at the <code>Userid    ===></code> prompt. If you give
tso-enum.nse    it an invalid user ID you receive the error message above.
tso-enum.nse 
tso-enum.nse This script relies on the NSE TN3270 library which emulates a
tso-enum.nse TN3270 screen for NMAP.
tso-enum.nse 
tso-enum.nse TSO user IDs have the following rules:
tso-enum.nse  - it cannot begin with a number
tso-enum.nse  - only contains alpha-numeric characters and @, #, $.
tso-enum.nse  - it cannot be longer than 7 chars

所需的输出

tso-enum.nse  
 description = [[
    TSO User ID enumerator for IBM mainframes (z/OS). The TSO logon panel
    tells you when a user ID is valid or invalid with the message:
     <code>IKJ56420I Userid <user ID> not authorized to use TSO</code>.

    The TSO logon process can work in two ways:
    1) You get prompted with <code>IKJ56700A ENTER USERID -</code>
       to which you reply with the user you want to use.
       If the user ID is valid it will give you a normal
       TSO logon screen. Otherwise it will give you the
       screen logon error above.
    2) You're given the TSO logon panel and enter your user ID
       at the <code>Userid    ===></code> prompt. If you give
       it an invalid user ID you receive the error message above.

    This script relies on the NSE TN3270 library which emulates a
    TN3270 screen for NMAP.

    TSO user IDs have the following rules:
     - it cannot begin with a number
     - only contains alpha-numeric characters and @, #, $.
     - it cannot be longer than 7 chars
    ]]

1 个答案:

答案 0 :(得分:2)

了解

使用这些块,作者在进入/退出该范围时通过切换标志e来定义范围:

# end of range
/\]\]/ {
    e = 0
}

# start of range
/description = \[\[/ {
    gsub("^.*description ", "", $0)
    e = 1
} 

字符串函数gsub将第一个自变量的 any 替换为在第三个自变量中找到的第二个自变量。在这种情况下,正则表达式"^.*description "与当前的记录 $0的开头(在这种情况下,记录是 line )匹配到字符串description,并用一个空字符串替换它,从而将其删除,并保留实际的描述。

下面的最后一块仅打印行和FILENAME(如果它在描述范围内):

{
    if (e == 1) {
        print FILENAME, $0
    }
}

修改

所需的修改是在范围的开始处打印FILENAME,缩进范围内的行,并对块重新排序:

# end range
/\]\]/ {
    e = 0
}

# in range
e == 1 {
    printf "\t%s\n", $0
}

# start range
/description = \[\[/ {
    print FILENAME
    e = 1
}

我删除了无用的(至少对于示例数据而言)gsub()调用。

调用:

$ awk -f script.awk data.txt

输出:

data.txt
        TSO User ID enumerator for IBM mainframes (z/OS). The TSO logon panel
        tells you when a user ID is valid or invalid with the message:
         <code>IKJ56420I Userid <user ID> not authorized to use TSO</code>.

        The TSO logon process can work in two ways:
        1) You get prompted with <code>IKJ56700A ENTER USERID -</code>
           to which you reply with the user you want to use.
           If the user ID is valid it will give you a normal
           TSO logon screen. Otherwise it will give you the
           screen logon error above.
        2) You're given the TSO logon panel and enter your user ID
           at the <code>Userid    ===></code> prompt. If you give
           it an invalid user ID you receive the error message above.

        This script relies on the NSE TN3270 library which emulates a
        TN3270 screen for NMAP.

        TSO user IDs have the following rules:
         - it cannot begin with a number
         - only contains alpha-numeric characters and @, #, $.
         - it cannot be longer than 7 chars