Kvadrat qavslar ichidagi qiymatlarni chiqarib olish

Quyida keltirilgan server nomlari to'plami bilan fayl testfile bor.

app-server-l11[2-5].test.com
server-l34[5-8].test.com
dd-server-l[2-4].test.com

Iltimos, chiqishni quyidagi tarzda olishda yordam bering.

app-server-l112.test.com
app-server-l113.test.com
app-server-l114.test.com
app-server-l115.test.com
server-l345.test.com
server-l346.test.com
server-l347.test.com
server-l348.test.com
dd-server-l2.test.com
dd-server-l3.test.com
dd-server-l4.test.com
5
Men OP'lar uchun bir-liners yozishni o'ylamayman, lekin bu biroz ko'proq murakkabdir ...
qo'shib qo'ydi muallif Jean-François Fabre, manba
Siz nimani sinab ko'rdingiz? Muammoga harakat qildingizmi?
qo'shib qo'ydi muallif HarryCBurn, manba

7 javoblar

GNU awk bilan 3-arg() ga mos keladigan uchun:

$ awk 'match($0,/(.*)\[([0-9]+)-([0-9]+)\](.*)/,a){for (i=a[2]; i<=a[3]; i++) print a[1] i a[4]}' file
app-server-l112.test.com
app-server-l113.test.com
app-server-l114.test.com
app-server-l115.test.com
server-l345.test.com
server-l346.test.com
server-l347.test.com
server-l348.test.com
dd-server-l2.test.com
dd-server-l3.test.com
dd-server-l4.test.com
2
qo'shib qo'ydi
++: Bu nafaqat javob, balki o'yin funksiyasi haqida mini awk ta'limi.
qo'shib qo'ydi muallif George Vasiliou, manba

GNU awk-da:

$ awk -F"[][]" '{split($2,a,"-"); for(i=a[1];i<=a[2];i++) print $1 i $3}' file
app-server-l112.test.com
app-server-l113.test.com
app-server-l114.test.com
app-server-l115.test.com
server-l345.test.com
server-l346.test.com
server-l347.test.com
server-l348.test.com
dd-server-l2.test.com
dd-server-l3.test.com
dd-server-l4.test.com
  • split to fields by [ and ] using FS
  • use split the get the range start (a[1]) and end (a[2])
  • iterate the range with for and output

Birorta oraliq yoki yo'qligini tekshirib bo'lmaydi. Buni quyidagi kabi amalga oshirish mumkin: print (NF == 3 = $ 1 $ 3: $ 1) .

1
qo'shib qo'ydi

Eng yomon va eng yomon misol:

var='app-server-l11[2-5].test.com'
for i in range(int(var[(var.find('[') +1)]), int(var[(var.find("]") - 1)])+1):
     print 'app-server-l11' + str(i) + '.test.com'

Tasavvur kuchingizdan foydalaning!

1
qo'shib qo'ydi
ser_nm = ['app-server-l11[2-5].test.com','server-134[5-8].test.com','dd-server-[2-4].test.com']

for nm in ser_nm:
    for i in range(int(nm[nm.find('[')+1 : nm.find('-',(nm.find('[')+1))]), int(nm[nm.find('-',(nm.find('[')+1))+1:nm.find(']')  ] )+1):
        print(nm[:nm.find('[')] + str(i) + nm[nm.find(']')+1:])

Bu shuningdek, server nomlari quyidagicha bo'lgan holatlarga e'tibor beradi: 'server-134 [52-823] .test.com'

1
qo'shib qo'ydi

eng yaxshi yechim emas, balki u ishlaydi ...

inp = open('input.txt', 'r+').read()
print(inp)
result= ''
for i in inp.split('\n'):
    if len(i) > 1:
        print(repr(i))
        f1 = i.find('[')
        f2 = i.find(']')+1
        b1 = i[:f1]
        b2 = i[f2:]
        ins = i[f1:f2]
        ins = ins[1:-1]
        for j in range(int(ins.split("-")[0]),int(ins.split("-")[1])+1):
            result+=b1+str(j)+b2+'\n'

outp = open('output.txt', 'w')
outp.write(result)
outp.close()
0
qo'shib qo'ydi

Maktab topshirig'i kabi ko'rinadi, men juda noma'lum bo'laman.

Raqamli diapazonni va qolgan qismini komponentlarini disk raskadrovka qilish uchun muntazam ifodani ishlatardim, so'ngra har bir manzilni (boshqa olingan manzil komponentlarini ishlatib) hosil qilish uchun chiqarilgan raqam oralig'ida yineleme uchun pastadir foydalaning.


Bir haftadan beri buyon:

    import re

    inputs = [ "app-server-l11[2-5].test.com", "server-l34[5-8].test.com", "dd-server-l[2-4].test.com" ]

    pattern = r"\s*(?P[a-zA-Z0-9-_.]+)\[(?P\d+)-(?P\d+)\](?P\S+)"

    expr = re.compile( pattern )

    def expand_domain( domain ):
        mo = expr.match( domain )
        if mo is not None:
            groups = mo.groupdict()
            subdomain = groups[ "subdomain" ]
            domain = groups[ "domain" ]
            range_start = int( groups[ "range_start" ] )
            range_end = int( groups[ "range_end" ] )
            result = [ "{}{:d}{}".format( subdomain, index, domain ) for index in range( range_start, range_end + 1 ) ]
            return result
        else:
            raise ValueError( "'{}' does not match the expected input.".format( domain ) )

    for domain in inputs:
        print( "'{}':".format( domain ) )
        for exp_dom in expand_domain( domain ):
            print( "--> {}".format( exp_dom ) )
0
qo'shib qo'ydi

Quyidagi buyruqni kerakli chiqish uchun murakkab iboralarisiz ishlatishingiz mumkin.

awk -f test.awk file.txt

test.awk quyidagi satrlarni o'z ichiga olishi kerak:

{
    if(a=match($0,"\\["))
    {
            start=strtonum(substr($0,a+1,1));
            end=strtonum(substr($0,a+3,1));
            copy=$0;
            for(i=start;i<=end;i++)
            {
                    sub("\\[[0-9]{1,}-[0-9]{1,}\\]",i,copy);
                    print copy;
                    copy = $0;
            }
    }
    else
    {
            print $0;
    }
}

file.txt sizning kirish faylingizni quyidagi satrlarni o'z ichiga oladi:

app-server-l11[2-5].test.com
server-l34[5-8].test.com
dd-server-l[2-4].test.com

chiqdi:

app-server-l112.test.com
app-server-l113.test.com
app-server-l114.test.com
app-server-l115.test.com
server-l345.test.com
server-l346.test.com
server-l347.test.com
server-l348.test.com
dd-server-l2.test.com
dd-server-l3.test.com
dd-server-l4.test.com
0
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming