awk - 尝试 grep 字符串的输出行,然后返回 $3(第 3 列)+ 找到的字符串

标签 awk grep

我在一个文件中有很多行文本 - 每行都采用这种格式:

blah blah filename1 blah blah blah blah text { ANY TEXT ANY HERE } blah blah blah

对于任何包含“text {whatever}”的行,我试图将第三列($3)附加在找到的文本“text{whatever}”前面

所以我已经完成了最后一部分,我只需要捕获那 3 美元:

cat data.txt | grep -oP 'text {/*?}'

*已更新以获得更准确的场景 以下是其扫描的实际代码行(它是 F5 负载均衡器配置):

ltm virtual www.testapp.com_80 { creation-time 2021-09-11:00:01:39 destination 10.105.34.20:http ip-protocol tcp last-modified-time 2021-09-11:19:56:16 mask 255.255.255.255 profiles { http { } tcp { } } rules { HTTP_to_HTTPS_Redirect } source 0.0.0.0/0 source-address-translation { type automap } translate-address enabled translate-port enabled vs-index 14 }

ltm virtual www.testapp2.com_443 { creation-time 2022-02-17:05:06:52 destination 10.105.34.23:https ip-protocol tcp last-modified-time 2022-02-17:05:06:52 mask 255.255.255.255 profiles { http { } tcp { } } rules { HTTP_to_HTTPS_Redirect } source 0.0.0.0/0 translate-address enabled translate-port enabled vs-index 20 }

这些只是代表一个巨大配置文件的两行。

我只需要该行中的 $3(第三列)(每行中出现的 VIP 网站名称),并打印出列出的 irules“rules { text in here }”

因此,对于这两行...我正在寻找如下所示的输出:

www.testapp.com_80 { HTTP_to_HTTPS_Redirect }
www.testapp2.com_443 { HTTP_to_HTTPS_Redirect }

感谢您的帮助!

最佳答案

$ awk 'match($0,/(^| )rules *{[^}]*}/){ x=$3; $0=substr($0,RSTART,RLENGTH); $1=x; print }' file
www.testapp.com_80 { HTTP_to_HTTPS_Redirect }
www.testapp2.com_443 { HTTP_to_HTTPS_Redirect }

关于awk - 尝试 grep 字符串的输出行,然后返回 $3(第 3 列)+ 找到的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73465797/

相关文章:

linux - 如何使用shell将字符串分割成正确的格式?

linux - 在 shell 脚本中计算平均值时如何处理缺失值?

linux - Grep 排除文件夹并使用 linux 搜索

linux - 如何在命令行中匹配另一个模式的目录中的 `find` 文件匹配模式?

regex - 从多个文本文件名中提取行,然后从文本文件中提取这些行 - Linux

awk - bash/grep : getting multiple matching elements from a json

linux - 在linux中将tab转换为fasta格式

grep - devnagari unicode 文本的正确区域设置

php - 在系统上的所有文件中查找一个字符串