regex - sed 正则表达式提取字段并构建新的

标签 regex linux awk sed

我从 aws 得到了这个输出:

$ aws ec2 describe-instances --filters "Name=instance-type,Values=c4.xlarge" | egrep -e "Value" -e "PrivateIpAddress" | grep bot -B1
                            "PrivateIpAddress": "10.100.2.44"
                            "Value": "bot-30",
--
                            "PrivateIpAddress": "10.100.2.106"
                            "Value": "bot-29",
--
                            "PrivateIpAddress": "10.100.2.167"
                            "Value": "bot-1",
--
                            "PrivateIpAddress": "10.100.2.161"
                            "Value": "bot-14",
--
                            "PrivateIpAddress": "10.100.2.235"
                            "Value": "bot-17",
--
                            "PrivateIpAddress": "10.100.2.54"
                            "Value": "bot-28",
--
                            "PrivateIpAddress": "10.100.2.234"
                            "Value": "bot-12",
--
                            "PrivateIpAddress": "10.100.2.43"
                            "Value": "bot-5",
--
                            "PrivateIpAddress": "10.100.2.50"
                            "Value": "bot-27",
--
                            "PrivateIpAddress": "10.100.2.124"
                            "Value": "bot-20",
--
                            "PrivateIpAddress": "10.100.2.247"
                            "Value": "bot-26",
--
                            "PrivateIpAddress": "10.100.2.113"
                            "Value": "bot-8",
--
                            "PrivateIpAddress": "10.100.2.190"
                            "Value": "bot-11",
--
                            "PrivateIpAddress": "10.100.2.184"
                            "Value": "bot-13",
--
                            "PrivateIpAddress": "10.100.2.253"
                            "Value": "bot-25",
--
                            "PrivateIpAddress": "10.100.2.254"
                            "Value": "bot-2",
--
                            "PrivateIpAddress": "10.100.2.199"
                            "Value": "bot-21",
--
                            "PrivateIpAddress": "10.100.2.130"
                            "Value": "bot-10",
--
                            "PrivateIpAddress": "10.100.2.59"
                            "Value": "bot-18",
--
                            "PrivateIpAddress": "10.100.2.4"
                            "Value": "bot-7",
--
                            "PrivateIpAddress": "10.100.2.213"
                            "Value": "bot-9",
--
                            "PrivateIpAddress": "10.100.2.214"
                            "Value": "bot-3",
--
                            "PrivateIpAddress": "10.100.2.200"
                            "Value": "bot-24",
--
                            "PrivateIpAddress": "10.100.2.148"
                            "Value": "bot-15",
--
                            "PrivateIpAddress": "10.100.2.146"
                            "Value": "bot-6",
--
                            "PrivateIpAddress": "10.100.2.94"
                            "Value": "bot-4",
--
                            "PrivateIpAddress": "10.100.2.150"
                            "Value": "bot-23",
--
                            "PrivateIpAddress": "10.100.2.81"
                            "Value": "bot-16",
--
                            "PrivateIpAddress": "10.100.2.155"
                            "Value": "bot-22",
--
                            "PrivateIpAddress": "10.100.2.102"
                            "Value": "bot-19",

我想格式化成下面的

10.100.2.44    bot-30
10.100.2.106   bot-29
10.100.2.167   bot-1 

我需要这种格式的原因是因为我想在 /etc/hosts 文件中添加所有这些主机名和 IP 地址。这只是一个小列表,但我们也有很长的列表

最佳答案

您可以通过管道将您的 aws 命令传递给此 sed:

sed -n '/PrivateIpAddress/N;s/.*"\([^"]*\)"\n.*"\(.*\)",.*/\1\t\2/p'

关于regex - sed 正则表达式提取字段并构建新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42680529/

相关文章:

swift - 如何将模式中包含其他文本的文本个性化并使用正则表达式对其进行编辑

c# - 如何在 C# Regex 中使用 lookbehind 以跳过重复前缀模式的匹配?

mysql - 如何将命令输出附加到 MariaDB 数据库?

当嵌套在可选括号中时,正则表达式不会捕获变量

javascript - 字符串增量 : increment the last string by one

c++ - 我可以将可搜索字符设备 `/dev/mem_8` 映射到内存并使用 x86_64 指令访问它吗?

php - 保护数据库连接字符串的最佳方法是什么?

bash - awk 打印带空格的变量

awk - Awk 新手。与负数格式作斗争

linux - 使用awk提取数据并计数