I got a text file containing lines separated in this manner:
Action: Add Parameter
Matched Parameter: ctl00_ContentPlaceHolderMain_RadSearchBoxNeId_ClientState
on [HTTPS] /ConsolePage/ConsolePageWeb.aspx Matched Wildcard: *
Action: Add Parameter
Matched Parameter: ctl00$ContentPlaceHolderMain$HiddenFieldSelectedFilter on
[HTTPS] /ConsolePage/ConsolePageWeb.aspx Matched Wildcard: *
I've written a small script in Python to get only the string after the "Matched Paramater: " and output it to a file, however the results are not sorted properly.
The script:
import re
pattern = "^Matched Parameter: ([^\s]+)"
new_file = []
with open(".\params.txt") as txtFile:
lines = txtFile.readlines()
for line in lines:
match = re.search(pattern, line)
if match:
new_line = match.group()
new_line = new_line.split(" ")
del new_line[0], new_line[0]
new_line = sorted(new_line)
print(new_line)
Output:
['ctl00_MainSplitter_ClientState']
['ctl00_RadWindowLicenseAggreemennt_C_RadButtonLicenseAggreemenntCancel_ClientState']
['ctl00_RadWindowLicenseAggreemennt_C_RadButtonLicenseAggreemenntOK_ClientState']
['ctl00_RadWindowLicenseAggreemennt_ClientState']
['ctl00$ScriptManagerMain']
['ctl00_RadStyleSheetManager1_TSSM']
['ctl00_ScriptManagerMain_TSM']
['__VIEWSTATE']
['ctl00_radwindow1_ClientState']
['ctl00_RadButtonLgout_ClientState']
['ctl00_TopPane_ClientState']
['ctl00_RadPanelBarMainMenu_ClientState']
['ctl00_LeftPane_ClientState']
['ctl00_ContentPlaceHolderMain_RadWindowManager1_ClientState']
['ctl00$ContentPlaceHolderMain$RadComboBoxTimeResolution']
['ctl00_ContentPlaceHolderMain_RadComboBoxTimeResolution_ClientState']
['ctl00_ContentPlaceHolderMain_RadSearchBoxNeId']
['ctl00_ContentPlaceHolderMain_RadSearchBoxNeId_ClientState']
['ctl00$ContentPlaceHolderMain$HiddenFieldSelectedFilter']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadButtonAlarmsFilterClose_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxRuleNames_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxSeverity_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxStatus_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxEntityType_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadButtonAlarmsFilterOK_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowFiltersList_C_RadButtonFiltersListClose_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowFiltersList_C_RadListBoxExistingFilters_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowFiltersList_C_RadButtonFiltersListOk_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowFiltersList_C_RadButtonFiltersListEdit_ClientState']
I need the output to be sorted by it's parameter name substring in alphabetical order, for example 'AlarmsUserFilters' before 'ClientState':
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadButtonAlarmsFilterClose_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxRuleNames_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxSeverity_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxStatus_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxEntityType_ClientState']
['ctl00_MainSplitter_ClientState']
Any help how to do this in the best possible way?, I need to be generic as possible, i.e there could be different strings that need to be sorted this way('ct100' etc.. is just one example)
Thanks!
NameError.. Should the lines after the if be indented?forloop goes through your data and prints out results in exactly the same order as they were present in the input file;new_linethat you pass tosortedis always just a single-element list.sorted(lines, key=lambda val: val[0])