Extract specific strings from a single long line

rodlozarg

I'm trying to extract the ID from some network interfaces from a single long line containing several ID's. I already tried to use split without success. I will appreciate any help

This is a sample of the input, remember this is on a single line of text.

"Authentication success on Interface Gi1/0/20 AuditSessionID 0000000XXXXXXXXXX, Authentication success on Interface Gi1/0/24 AuditSessionID 0000000XXXXXXXXXX, Authentication not succeed on Interface Fi1/0/10 AuditSessionID 0000000XXXXXXXXXX"

I expecting output just Gi1/0/20 Gi1/0/24 Fi1/0/10

Will Da Silva

Regex is suited for this task:

import re

text = 'Authentication success on Interface Gi1/0/20 AuditSessionID 0000000XXXXXXXXXX, Authentication success on Interface Gi1/0/24 AuditSessionID 0000000XXXXXXXXXX, Authentication not succeed on Interface Fi1/0/10 AuditSessionID 0000000XXXXXXXXXX'
re.findall('Interface (.*?) ', text)

The re.findall() will return a list containing what you wanted.

['Gi1/0/20', 'Gi1/0/24', 'Fi1/0/10']

The pattern 'Interface (.*?) ' works by matching Everything beginning with the word "Interface", followed by a space, then something or nothing, then another space. That aforementioned something or nothing is represented by (.*?), which captures (i.e. it gets added to the output of re.findall()) whatever is matched by .*?, which is any character (.), any number of times (*), as few times as necessary to match (?). You can play around with regexes on sites like https://regex101.com/, which will allow you to run Python regexes, as well as explain them (better than I can).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract values from a long string based on delimiting strings

Using jq to extract specific property values and output on a single line

How can we extract specific part of the string from long string?

Extract specific data from line in python

Extract digits and strings from unspecific line format

Extract strings from character vector in R from/to specific words

How to extract line from the file on specific condition

How to extract single value from single line tuples?

With c# extract a specific date pattern reliably from a long string

Read integers and strings from a single line of a console

Extract specific characters from each line

how to capture specific strings from line

Shell scripting extract specific terms from a line

Extract single words or phrases from list of strings which are not in base string

Extract specific strings from text file

Extract single line from command output in terminal

Extract specific strings from the df

how to extract specific part from long string

I want to extract strings from a line

How to extract a specific line from multiple text file into a single txt file on Windows?

Get specific strings from one long string

Extract two strings from a line of text with Ansible

Extract strings from a Dataframe looping over a single row

How to extract Specific items from single line from R

How to extract only specific strings from each line of a file using awk?

How to extract this specific number from this set of strings?

Extract specific strings

Extract field from a specific line with reg expressions

Use sed to extract specific line from file?