File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ # cmd: python sort_ioc.py [input.ioc] [formatted_output.ioc]*
3+ # formatted_output.ioc is optional, if not provided, input.ioc will be overwritten
4+
5+
6+ import sys
7+
8+ def sort_ioc_file (input_file , output_file = None ):
9+ if output_file is None :
10+ output_file = input_file # overwrite original by default
11+
12+ with open (input_file , 'r' , encoding = 'utf-8' ) as f :
13+ lines = f .readlines ()
14+
15+ # Filter out empty lines and strip whitespace
16+ lines = [line .rstrip () for line in lines if line .strip ()]
17+
18+ # Sort lines alphabetically by key (everything before '=')
19+ def key_func (line ):
20+ if '=' in line :
21+ return line .split ('=' , 1 )[0 ].strip ()
22+ return line .strip ()
23+
24+ sorted_lines = sorted (lines , key = key_func )
25+
26+ # Write back
27+ with open (output_file , 'w' , encoding = 'utf-8' ) as f :
28+ for line in sorted_lines :
29+ f .write (line + '\n ' )
30+
31+ print (f"Sorted { input_file } -> { output_file } " )
32+
33+ if __name__ == "__main__" :
34+ if len (sys .argv ) < 2 :
35+ print ("Usage: python sort_ioc.py <input_file> [output_file]" )
36+ sys .exit (1 )
37+
38+ input_file = sys .argv [1 ]
39+ output_file = sys .argv [2 ] if len (sys .argv ) > 2 else None
40+ sort_ioc_file (input_file , output_file )
You can’t perform that action at this time.
0 commit comments