原始数据具有“ pool pool_name”,然后是0-n行“ member random_member_ip”,是否可以在bash中提取列表?我尝试了一些有条件的直到累了。我在这里先向您的帮助表示感谢。
pool string_name_1
member 172.0.0.1
member 172.0.1.1
pool string_name_2
member 172.2.1.2
member 172.0.2.2
pool string_name_3
member 172.2.1.3
member 172.3.2.3
member 172.25.25.3
member 172.25.40.3
pool string_name_4
pool string_name_5
member 5.5.50.5
预期结果
string_name_1 172.0.0.1 172.0.1.1
string_name_2 172.2.1.2 172.0.2.2
string_name_3 172.2.1.3 172.3.2.3 172.25.25.3 172.25.40.3
string_name_4
string_name_5 5.5.50.5
答案 0 :(得分:3)
这会将默认的RS
更改为pool
,因此现在每个记录都由pool
分隔。 gsub()
用于从输出中删除member
字符串。
awk -v RS='pool' '{$1=$1;gsub(/member/,"")}1' inputfile
string_name_1 172.0.0.1 172.0.1.1
string_name_2 172.2.1.2 172.0.2.2
string_name_3 172.2.1.3 172.3.2.3 172.25.25.3 172.25.40.3
string_name_4
string_name_5 5.5.50.5
答案 1 :(得分:3)
使用bash可以编写
unset line
while read -r key value; do
case $key in
pool) [[ $line ]] && echo "$line"; line=$value ;;
member) line+=" $value" ;;
esac
done < file
echo "$line"
awk中的相同技术可提高性能:
awk '
$1 == "pool" {if (line) print line; line = $2}
$1 == "member" {line = line OFS $2}
END {print line}
' file