私は、コマンドから3つのパターンをすべてマッチさせてスペースを置くことを楽しみにしています。最初の試合だけが得られた場合は残りの2試合を行い、その後はその試合を全く印刷しません....awkまたはsed 3つの一致するパターンの後にスペースを配置するために
以下は実際のコマンド出力です。最初に一致する "dn"が検索され、それには2つのパターンの検索が含まれません。 それは素晴らしいことだろうと同時に、私達はawkのを持つことができますやgrep自体の代わりに自分自身をsedの...
$ 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)i「はけれどもメートル以下のようにsedを & awkを持つすべての3のoccuranceの後にスペースを配置することができ..
$ 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)回答君たちからの入力に基づいて遵守し、使用可能ですある時点で他の人々のために!あなたはこの
sed
使用することができ、すべての入力&提案
$ 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
ldapsearchの出力でスクリプトを動作させたい場合は、ldapsearchの出力を表示します。不要なコマンドにパイプした出力は表示しません。言い換えれば、作成したいコマンドの入力例を表示して、それが何であるかを推測しないようにしてください。 –