Linux lsof 需要帮助解析输出

标签 linux bash awk lsof

我在 linux 系统上使用以下命令:

lsof -i -n | egrep '\<ssh\>'|awk '{print $8,$9}'

它产生这样的输出:

192.168.199.52:ssh->192.168.199.254:17598 (ESTABLISHED)
192.168.199.52:ssh->192.168.199.254:17598 (ESTABLISHED)
192.168.199.52:56448->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56449->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56454->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56458->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56460->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56468->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:ssh->192.168.199.254:56671 (ESTABLISHED)
192.168.199.52:ssh->192.168.199.254:56671 (ESTABLISHED)
192.168.199.52:ssh->192.168.199.254:56672 (ESTABLISHED)

我只想从左侧提取 IP 地址,在“->”字段的右侧提取 IP 地址。我怎样才能轻松提取这两个文件并将它们重新组合成以下格式:

192.168.199.52->192.168.199.254

最佳答案

类似于:

lsof -i -n | awk '$9 ~ /:ssh(-|$)/{ gsub(/:[^-]*/, "", $9); print $9 }'

或者用 8 美元代替 9 美元。

awk 命令详细信息:

$9 ~ /:ssh(-|$)/ {           # when ":ssh" is at the end of field 9 or
                             # followed by an hyphen
    gsub(/:[^-]*/, "", $9);  # remove all the semi-colon followed by characters that
                             # are not an hyphen from the field 9
    print $9                 # and print it
}

关于Linux lsof 需要帮助解析输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248455/

相关文章:

bash - ls $FOLDER_PATH $FOLDER_PATH : No such file or directory 中有空格

linux - Shell 脚本(从日期明智的目录发送文件)- Linux

awk - 有没有办法完全删除 awk 中的字段,以便不打印多余的分隔符?

linux - 如何禁用根设备上的文件系统缓存?

php - 使用 nginx 和 fastcgi 捕获 PHP 404 错误

python - 如何找出Python脚本是从哪里调用的?

linux - 科学Linux : I need to take a shell script variable and use it to search in a awk script

linux - awk -F 不在 solaris 中搜索完整字符串

linux - EEPROM : uevent generated infinite times in linux kernel 4. 4.0

linux - 在 linux 中,如何获取我正在使用的命令的完整路径?