使用 bash 查找 Sed

使用 bash 查找 Sed

我正在尝试使用 bash 和 sed 实现以下目标,其中包含以下字符串的文件

qa2.test.m.xyz.com

554
1723
21

qa88.test.nj.xyz.com

1723
554
21
443
80

qa2.lite.xyz.com

554
1723
21

qa2.www.nj.xyz.com

80
443
1723
21
554

tadmins.xyz.com


meadmin.xyz.com


real.xyz.com


responsible.xyz.com

554
1723
21
80
443


应该看起来像这样,但如果域名后的端口号列表不存在,则只回显域名

qa2.test.m.xyz.com
qa2.test.m.xyz.com:554
qa2.test.m.xyz.com:1723
qa2.test.m.xyz.com:21

qa88.test.nj.xyz.com
qa88.test.nj.xyz.com:1723
qa88.test.nj.xyz.com:554
qa88.test.nj.xyz.com:21
qa88.test.nj.xyz.com:443
qa88.test.nj.xyz.com:80

.
.
.
.
like wise.

我不知道应该选择哪种分隔符以及应该写哪种逻辑。

答案1

以下是使用 GNU sed 的一种方法:

解析器

# Assume first line holds the first domain address and save it `h` to
# hold-space, print it `b` and move on to next input line `b`
1 { h; p; b; }

# When domain line found, save it to hold-space `h` and print it with a gap 
/^[[:alnum:]]+(\.[[:alnum:]]+)+/ { h; s/^/\n/p; }

# When we have a number, fetch domain name from hold-space `G` and
# swap them around with `s///`
/^[[:digit:]]+$/ {
  G
  s/([^\n]+)\n(.*)/\2:\1/p
}

像这样运行:

sed -nEf parse.sed infile

输出:

qa2.test.m.xyz.com
qa2.test.m.xyz.com:554
qa2.test.m.xyz.com:1723
qa2.test.m.xyz.com:21

qa88.test.nj.xyz.com
qa88.test.nj.xyz.com:1723
qa88.test.nj.xyz.com:554
qa88.test.nj.xyz.com:21
qa88.test.nj.xyz.com:443
qa88.test.nj.xyz.com:80

qa2.lite.xyz.com
qa2.lite.xyz.com:554
qa2.lite.xyz.com:1723
qa2.lite.xyz.com:21

qa2.www.nj.xyz.com
qa2.www.nj.xyz.com:80
qa2.www.nj.xyz.com:443
qa2.www.nj.xyz.com:1723
qa2.www.nj.xyz.com:21
qa2.www.nj.xyz.com:554

tadmins.xyz.com

meadmin.xyz.com

real.xyz.com

responsible.xyz.com
responsible.xyz.com:554
responsible.xyz.com:1723
responsible.xyz.com:21
responsible.xyz.com:80
responsible.xyz.com:443

答案2

我会用awk这个。

awk '
    # Skip blank lines
    $0=="" { next }

    # If the value is numerically zero (it's probably a string) so save the domain
    # Print a blank line to separate from the previous group (unless first line)
    !($0+0) { if (domain>"") { print "" }; domain = $0 }

    # If the value is numerically non-zero prepare to print the domain and this port number
    $0+0>0 { $0 = domain ":" $0 }

    # Print whatever we've got
    1
' input_file

输出(截断)

qa2.test.m.xyz.com
qa2.test.m.xyz.com:554
qa2.test.m.xyz.com:1723
qa2.test.m.xyz.com:21

qa88.test.nj.xyz.com
qa88.test.nj.xyz.com:1723
qa88.test.nj.xyz.com:554
qa88.test.nj.xyz.com:21
qa88.test.nj.xyz.com:443
qa88.test.nj.xyz.com:80

qa2.lite.xyz.com
qa2.lite.xyz.com:554
...

相关内容