grep - Show a match (grep) and show one column of the matched line
问题描述
Say I have a following text file (Nmap's .gnmap) and what I want is to show my match and corresponding IP addres. I.e. only show 22/open
and 2000/open
and also IP of those two ports. I need this result:
10.10.10.1 22/open 2000/open
10.10.10.2 2000/open
To get IPs is simple grep -iE "22/open|2000/open" file
, but how do i also display IP? I need following output (exact extra characters does not matter, as long as each line contains IP and port):
Example source file:
Host: 10.10.10.1 () Ports: 22/open/tcp//ssh///, 80/open/tcp//http///, 113/closed/tcp//ident///, 443/open/tcp//https///, 541/open/tcp//uucp-rlogin///, 2000/open/tcp//cisco-sccp/// Ignored State: filtered (4994)
Host: 10.10.10.2 () Ports: 113/closed/tcp//ident///, 2000/open/tcp//cisco-sccp/// Ignored State: filtered (4998)
解决方案
Could you please try following.
awk '
match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){
ip_found=substr($0,RSTART,RLENGTH)
}
{
while(match($0,/22\/open|2000\/open/)){
val=(val?val OFS:"")substr($0,RSTART,RLENGTH)
$0=substr($0,RSTART+RLENGTH)
}
if(ip_found){
print ip_found,val
}
ip_found=val=""
}
' Input_file
Output will be as follows.
10.10.10.1 22/open 2000/open
10.10.10.2 2000/open
推荐阅读
- sql - Postgresql 左连接
- java - 使用hikari连接池时如何解决“Socket read timed out”
- c++ - Unordered_map 或 hashMap 现有散列函数修改?
- anylogic - 功能决定在哪里存放托盘
- vb.net - 在文本文件中查找数字序列
- date - 我正在使用 Sun OS,我希望我的脚本从文件中读取日期(格式为 %Y%m%d)并在该日期上添加 1 天
- python - 如何为“AttributeError:模块'matplotlib.cbook'没有属性'is_string_like'”创建属性?
- lotus-notes - 无法从 Lotus 中标记为“将多个值显示为单独的条目”的列中检索对话列表中的多个值
- python-3.x - '[Errno 26] 文本文件忙:'/home/*****/.wdm/drivers/chromedriver/83.0.4103.39/linux64/chromedriver''
- c# - 参考 DLL 未被替换