awk或sed在三个匹配模式之后放置空格,但有一些例外

时间:2017-02-08 14:30:29

标签: awk sed

我期待从命令匹配所有三个模式并放置一个空格,它应该继续所有3匹配模式并收到整个输出但是如果它只获得第一个匹配它没有得到休息两个人根本不打印那一场比赛......

下面是我想要得到的实际命令输出,如果你看到第一个匹配“dn”被搜索到它并且它不包含其他两种搜索模式。 与此同时,我们可以用awk或sed代替grep本身就很棒......

$ ldapsearch -h myldapserver -x -LLL -b "ou=profile,o=ferry.com" "cn=*" | grep -Ei "^dn|defaultServerList|preferredServerList"
    dn: cn=proxyagent,ou=profile,o=ferry.com
    dn: cn=default,ou=profile,o=ferry.com
    preferredServerList: 192.68.8.15 192.68.8.16
    defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.27
    dn: cn=austin, ou=profile, o=ferry.com
    defaultServerList: 192.68.63.10 10.209.208.23
    preferredServerList: 192.68.88.14 10.28.15.10
    dn: cn=sanjose, ou=profile, o=ferry.com
    preferredServerList: 192.68.8.15 192.68.8.16
    defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.38

预期输出为:

dn: cn=proxyagent,ou=profile,o=ferry.com  (--> This is single matched found without 2 others, which i don't want to be printed if its alone without 2 others)

dn: cn=default,ou=profile,o=ferry.com
preferredServerList: 192.68.8.15 192.68.8.16
defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.27

dn: cn=austin, ou=profile, o=ferry.com
defaultServerList: 192.68.63.10 10.209.208.23
preferredServerList: 192.68.88.14 10.28.15.10

dn: cn=sanjose, ou=profile, o=ferry.com
preferredServerList: 192.68.8.15 192.68.8.16
defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.38
  

2)虽然我能够在每3次出现后用sed放置空间   &安培; awk如下..

$ ldapsearch -h myldapserver -x -LLL -b "ou=profile,o=ferry.com" "cn=*" | egrep  "^dn|defaultServerList|preferredServerList" | sed '0~3 a\\'

$ ldapsearch -h myldapserver -x -LLL -b "ou=profile,o=ferry.com" "cn=*" | egrep  "^dn|defaultServerList|preferredServerList" |awk ' {print;} NR % 3 == 0 { print ""; }'

   dn: cn=proxyagent,ou=profile,o=ferry.com
   dn: cn=default,ou=profile,o=ferry.com
   preferredServerList: 192.68.8.15 192.68.8.16

    defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.27
    dn: cn=austin, ou=profile, o=ferry.com
    defaultServerList: 192.68.63.10 10.209.208.23

    preferredServerList: 192.68.88.14 10.28.15.10

=============================================== ==================

实际命令输出:

$ ldapsearch -h myldapserver -x -LLL -b "ou=profile,o=ferry.com" "cn=*"

dn: cn=proxyagent,ou=profile,o=ferry.COM
userPassword:: e2NyeXB0fTBmVVVjSTI1SDZINS4=
objectClass: top
objectClass: person
sn: proxyagent
cn: proxyagent

dn: cn=default,ou=profile,o=ferry.COM
preferredServerList: 192.68.8.15 192.68.8.16
defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.27
objectClass: top
bindTimeLimit: 10
credentialLevel: proxy
cn: default
profileTTL: 120

dn: cn=austin, ou=profile, o=ferry.com
defaultServerList: 192.68.63.10 10.209.208.23
preferredServerList: 192.68.88.14 10.28.15.10
attributeMap: printers:printer-uri-supported=printer-xri-supported
objectClass: top
objectClass: DUAConfigProfile
objectClass: kdsdirobjectinfo
description: Austin Default Profile
  

3)根据你们的意见编写答案,在某些时候可能对其他人有用!欣赏您的所有输入&建议

$ cat ldaphostprofile.sh
#!/bin/bash
#Author : karn Kumar (08/02/2017)
# This is Just to check what is are prefered Ldap server's and default for authentication by sitewise
# There is contribution from some of folks over open forums
# s=1; s*=2; s*=3 here using math, the value s will be divisible by 6 only if both 2 and 3 factors are there, here multiple occurrences won't change the condition but only the latest values encountered are used.

# s && !(s%6) checks for divisibility by 6 and whether value is initialized in "dn" check.

# s=0 reset value after printing, so that printing will be suspended until the next group.

# sep you want the triples separated by an empty line, we don't want to add after every group, since it will leave an empty line at the end, or similarly at the beginning. Alternative is, using a late initialized variable (after first use). So there won't be an empty line at the beginning or the end, but in between groups.
# mapfile is bash build in function can be used with BASH Version >= 4.0 onwards

set -f      # to prevent filename expansion

mapfile -t PLIST < <(ldapsearch -h myldapserver -x -LLL -b "ou=profile,o=ferry.com" "cn=*" | awk '/^dn/ {s =1; dn=$0} /^preferredServerList/ {s*=2; ps=$0}/^defaultServerList/ {s*=3; ds=$0} s && !(s%6) {print sep dn ORS ps ORS ds; sep=ORS; s=0}' | awk '/preferredServerList/ { print $2,$3,$4 }')

mapfile -t DLIST < <(ldapsearch -h myldapserver -x -LLL -b "ou=profile,o=ferry.com" "cn=*" | awk '/^dn/ {s =1; dn=$0} /^preferredServerList/ {s*=2; ps=$0}/^defaultServerList/ {s*=3; ds=$0} s && !(s%6) {print sep dn ORS ps ORS ds; sep=ORS; s=0}' | awk '/defaultServerList/ { print $2,$3,$4 }')

mapfile -t LLIST < <(ldapsearch -h myldapserver -x -LLL -b "ou=profile,o=ferry.com" "cn=*" | awk '/^dn/ {s =1; dn=$0} /^preferredServerList/ {s*=2; ps=$0}/^defaultServerList/ {s*=3; ds=$0} s && !(s%6) {print sep dn ORS ps ORS ds; sep=ORS; s=0}' | awk '/dn/ {print $2}'| cut -d "," -f1 | cut -d"=" -f2)

count_x=${#PLIST[@]}
count_y=${#DLIST[@]}
count_l=${#LLIST[@]}

echo $count_x
echo $count_y
echo $count_l

# Find out which of the two is larger in size, assuming that's a possibility
if [[ $count_x -lt $count_y ]]
  then
    count=$count_y
else
  count=${count_x}
#elif
# count=${count_l}
fi

printf "=%.0s"  $(seq 1 150)
printf "\n"
printf "%-50s : %-50s : %-50s\n"         "PreferredList IP's"  "DefaultServerList IP's"   "Location"            # print header
printf "=%.0s"  $(seq 1 150)                                                                                    # print separator
printf "\n"                                                                                                     # print newline

for i in $(seq $count);
do
  printf "%-50s : %-50s : %-50s\n"  "${PLIST[i-1]}"    "${DLIST[i-1]}" "${LLIST[i-1]}"
done

[root ~/SCRIPTS]$ ./ldaphostprofile.sh
455
455
455
======================================================================================================================================================
PreferredList IP's                                 : DefaultServerList IP's                             : Location
======================================================================================================================================================
192.218.88.14 10.28.15.10                           : 192.20.63.10 10.209.208.23                         : austin
192.168.8.15 192.168.8.16                            : 192.168.8.15 192.168.8.16 192.218.88.38           : sanjose
192.168.8.15 192.168.8.16                            : 192.168.8.16 192.168.8.15                            : India
192.162.167.9 192.162.167.8                          : 192.168.8.16 192.218.88.38                           : japan
192.162.167.9 192.162.167.8                          : 192.168.8.15 192.218.88.38                           : China
192.162.167.9 192.162.167.8                          : 192.168.8.16 192.218.88.38                           :  Franse
192.162.167.9 192.162.167.8                          : 192.168.8.16 192.168.8.15                            :  Brazil
192.168.8.16 192.168.8.15 192.168.8.6                 : 192.168.8.16 192.218.88.38                          :  Tiwan
192.168.8.15 192.168.8.16                            : 192.168.8.15 192.218.88.38                           :  Russia
192.162.167.9 192.162.167.8                          : 192.168.8.16 192.218.88.38                           :  Germany
192.133.208.24 192.135.200.10                        : 192.135.200.10 172.23.39.200                        :   Poland

3 个答案:

答案 0 :(得分:1)

您可以使用此sed

ldapsearch '...' | grep '...' | sed 's/^dn/\n&/g'

答案 1 :(得分:1)

$ ... | awk 'NR>1 && /^dn/{print ""} /^dn|(preferred|default)ServerList/' file

应该这样做。要仅打印三个组,您必须添加更多逻辑

$ awk '/^dn/                  {s =1; dn=$0} 
       /^preferredServerList/ {s*=2; ps=$0} 
       /^defaultServerList/   {s*=3; ds=$0} 
       s && !(s%6)            {print sep dn ORS ps ORS ds; sep=ORS; s=0}' file

备注

  

s=1; s*=2; s*=3这里使用数学,只有当2和3因子都存在时,值s才会被6整除,此处多次出现不会改变条件但只会更新最新值遇到了。

     

s && !(s%6)检查可分性为6,以及是否在&#34; dn&#34;中初始化了值。检查。

     打印后

s=0重置值,以便打印将暂停,直到下一组。

     

sep你希望三元组以空行分隔,我们不想在每个组之后添加,因为它会在结尾处留下空行,或者在开头处类似。替代方案是,使用后期初始化变量(首次使用后)。所以在开始或结束时不会是空行,而是在群组之间。

假设dn是组启动器,将打印第一个三元组,直到下一个dn行出现。

使用输出文件

dn: cn=default,ou=profile,o=ferry.com
preferredServerList: 192.68.8.15 192.68.8.16
defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.27

dn: cn=austin, ou=profile, o=ferry.com
preferredServerList: 192.68.88.14 10.28.15.10
defaultServerList: 192.68.63.10 10.209.208.23

dn: cn=sanjose, ou=profile, o=ferry.com
preferredServerList: 192.68.8.15 192.68.8.16
defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.38

答案 2 :(得分:1)

为了证明我试图了解的重点,为什么显示Options +FollowSymLinks RewriteEngine On RewriteRule ^automation/?$ /index.php [NC,L] 的输出而不是ldapsearch的输出作为脚本的输入非常重要你想要创建,这个简单的脚本将产生你已经向我们展示的示例输入中显示的预期输出:

ldapseach | grep -Ei...

如果没有$ awk '/dn:/{if (cnt==3) print rec; cnt=0; rec=""} {rec = rec $0 ORS; cnt++} END{if (cnt==3) print rec}' file dn: cn=default,ou=profile,o=ferry.com preferredServerList: 192.68.8.15 192.68.8.16 defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.27 dn: cn=austin, ou=profile, o=ferry.com defaultServerList: 192.68.63.10 10.209.208.23 preferredServerList: 192.68.88.14 10.28.15.10 dn: cn=sanjose, ou=profile, o=ferry.com preferredServerList: 192.68.8.15 192.68.8.16 defaultServerList: 192.68.8.15 192.68.8.16 192.68.88.38 ,它会单独用于ldapsearch的输出吗?是否有一个更简单的脚本可以单独用于...| grep -Ei...的输出?我不知道这两个问题的答案是什么,因为你还没有告诉我们ldapsearch的输出是什么样的。