提取动态输入的特定部分,然后将它们组合在一起?

huangapple go评论99阅读模式
英文:

how to extract specific parts from a dynamic input and then put them together?

问题

  1. cmd1 = "dir"
  2. args1 = "/a"
  3. path = "c:\\users\\"
  4. cmd2 = '| find "stuff"'
  5. cmd3 = "more"
  6. path2 = "c:\\windows\\win.ini"
  7. cmd4 = '| findstr "fonts"'
  8. cmd5 = "certutil.exe"
  9. args2 = "-encode \\dir\\file"
  10. extra_cmds = "& something"
英文:

Having in input a command, i would like to extract using python in a separate way the command, the path involved, the args and the extra commands concatenated, so that i can work with every single piece if needed, and then build them back all together

so, for example:

  1. dir /a c:\users\ | find "stuff"
  1. cmd1 = "dir"
  2. args1 = "/a"
  3. path = "c:\\users\\"
  4. cmd2 = '| find "stuff"'

this would be an easy task if the commands would be always the same or with the same patterns, but how can I do it if they are always different and with a different syntax?

some more examples:

  1. more c:\windows\win.ini | findstr "fonts"
  2. certutil.exe -encode \dir\file & something

答案1

得分: 1

这是有趣的!这里是我的尝试:

  1. import re
  2. examples = [
  3. 'dir /a c:\\users\\ | find "stuff"',
  4. 'more c:\\windows\\win.ini | findstr "fonts"',
  5. 'certutil.exe -encode \\dir\\file & something',
  6. 'command -option1 -option2 -option3',
  7. 'command "path with spaces" -option',
  8. 'command > output.txt',
  9. 'command "path with \'nested\' quotes" [optional argument]',
  10. 'echo "This is a test" | find /v "exclude"',
  11. 'python script.py arg1 arg2 --option',
  12. 'echo $HOME',
  13. 'dir /s /b | findstr /r "\.txt$"',
  14. 'command1 | command2 | command3',
  15. 'command1 && command2 || command3',
  16. 'command1 | command2 > output.txt 2>&1',
  17. 'command arg1^&arg2 arg3',
  18. 'echo ^<output^>',
  19. 'command1 arg1 | command2 arg2',
  20. 'command -option="value"',
  21. 'command --long-option arg1 arg2',
  22. 'command [optional argument] -option=value',
  23. 'command arg1 ^& command2 arg2',
  24. 'command "path with spaces" | find "search phrase"',
  25. 'python -c "print(2 + 2)"',
  26. 'curl -X POST -d @data.txt http://example.com/api',
  27. 'grep -r "pattern" /path/to/search --exclude-dir=dir',
  28. 'ls -l | awk \'{print $1, $2}\'',
  29. 'command < input.txt',
  30. 'command arg1 -f file.txt',
  31. 'command "string with $special$ chars"',
  32. 'command --verbose --debug',
  33. 'command arg1 ^> output.txt',
  34. 'command [optional argument1] --flag --option=value [optional argument2]',
  35. ]
  36. import re
  37. def process_command(command):
  38. command = re.split(r" \| | & |&&|\|\|", command)
  39. print(command)
  40. for com in command:
  41. com = com.split(" ")
  42. print(f"Command: {com[0]}")
  43. string_parts = []
  44. i = 1
  45. in_long_string = False
  46. quote_mark = None
  47. arg_number = 0
  48. option_number = 0
  49. while i < len(com):
  50. if in_long_string:
  51. if quote_mark == com[i][-1]:
  52. string_parts.append(com[i])
  53. print(f"String: {' '.join(string_parts)}")
  54. string_parts = []
  55. in_long_string = False
  56. else:
  57. string_parts.append(com[i])
  58. elif re.match(r"^[a-zA-Z]:\\|^\\", com[i]): # if it starts with a letter then :// or starts with \, it's a path
  59. print(f"Path: {com[i]}")
  60. elif re.match(r"^/|-", com[i]): # if it starts with / or -, it's an option
  61. option_number += 1
  62. print(f"Option {option_number}: {com[i]}")
  63. elif re.match(r"^('|\").+('|\")$", com[i]): # if it's surrounded by quotes, it's a string
  64. print(f"String: {com[i]}")
  65. elif re.match(r"^('|\").*$", com[i]): # if it starts with a quote mark, it's the start of a long string
  66. string_parts.append(com[i])
  67. quote_mark = com[i][0]
  68. in_long_string = True
  69. elif re.match(r">$", com[i]): # if it ends in a >, it's an output thing
  70. print(f"Output: {com[i+1]}")
  71. i += 1
  72. elif re.match(r"<$", com[i]): # likewise with inputs
  73. print(f"Input: {com[i+1]}")
  74. else: # otherwise it's some other kind of argument
  75. arg_number += 1
  76. print(f"Argument {arg_number}: {com[i]}")
  77. i += 1
  78. [process_command(command) for command in examples]

如果有任何问题,请附带更多示例的注释。

英文:

This was fun! Here is my attempt:

  1. import re
  2. examples = [
  3. &#39;dir /a c:\\users\\ | find &quot;stuff&quot;&#39;,
  4. &#39;more c:\\windows\\win.ini | findstr &quot;fonts&quot;&#39;,
  5. &#39;certutil.exe -encode \\dir\\file &amp; something&#39;,
  6. &#39;command -option1 -option2 -option3&#39;,
  7. &#39;command &quot;path with spaces&quot; -option&#39;,
  8. &#39;command &gt; output.txt&#39;,
  9. &#39;command &quot;path with \&#39;nested\&#39; quotes&quot; [optional argument]&#39;,
  10. &#39;echo &quot;This is a test&quot; | find /v &quot;exclude&quot;&#39;,
  11. &#39;python script.py arg1 arg2 --option&#39;,
  12. &#39;echo $HOME&#39;,
  13. &#39;dir /s /b | findstr /r &quot;\.txt$&quot;&#39;,
  14. &#39;command1 | command2 | command3&#39;,
  15. &#39;command1 &amp;&amp; command2 || command3&#39;,
  16. &#39;command1 | command2 &gt; output.txt 2&gt;&amp;1&#39;,
  17. &#39;command arg1^&amp;arg2 arg3&#39;,
  18. &#39;echo ^&lt;output^&gt;&#39;,
  19. &#39;command1 arg1 ^| command2 arg2&#39;,
  20. &#39;command -option=&quot;value&quot;&#39;,
  21. &#39;command --long-option arg1 arg2&#39;,
  22. &#39;command [optional argument] -option=value&#39;,
  23. &#39;command arg1 ^&amp; command2 arg2&#39;,
  24. &#39;command &quot;path with spaces&quot; ^| find &quot;search phrase&quot;&#39;,
  25. &#39;python -c &quot;print(2 + 2)&quot;&#39;,
  26. &#39;curl -X POST -d @data.txt http://example.com/api&#39;,
  27. &#39;grep -r &quot;pattern&quot; /path/to/search --exclude-dir=dir&#39;,
  28. &#39;ls -l | awk \&#39;{print $1, $2}\&#39;&#39;,
  29. &#39;command &lt; input.txt&#39;,
  30. &#39;command arg1 -f file.txt&#39;,
  31. &#39;command &quot;string with $special$ chars&quot;&#39;,
  32. &#39;command --verbose --debug&#39;,
  33. &#39;command arg1 ^&gt; output.txt&#39;,
  34. &#39;command [optional argument1] --flag --option=value [optional argument2]&#39;,
  35. ]
  36. import re
  37. def process_command(command):
  38. command = re.split(r&quot; \| | &amp; |&amp;&amp;|\|\|&quot;, command)
  39. print(command)
  40. for com in command:
  41. com = com.split(&quot; &quot;)
  42. print(f&quot;Command: {com[0]}&quot;)
  43. string_parts = []
  44. i = 1
  45. in_long_string = False
  46. quote_mark = None
  47. arg_number = 0
  48. option_number = 0
  49. while i &lt; len(com):
  50. if in_long_string:
  51. if quote_mark == com[i][-1]:
  52. string_parts.append(com[i])
  53. print(f&quot;String: {&#39; &#39;.join(string_parts)}&quot;)
  54. string_parts = []
  55. in_long_string = False
  56. else:
  57. string_parts.append(com[i])
  58. elif re.match(r&quot;^[a-zA-Z]:\\|^\\&quot;, com[i]): # if it starts with a letter then :// or starts with \, it&#39;s a path
  59. print(f&quot;Path: {com[i]}&quot;)
  60. elif re.match(r&quot;^/|-&quot;, com[i]): # if it starts with / or -, it&#39;s an option
  61. option_number += 1
  62. print(f&quot;Option {option_number}: {com[i]}&quot;)
  63. elif re.match(r&quot;^(\&quot;|\&#39;).+(\&quot;|\&#39;)$&quot;, com[i]): # if it&#39;s surrounded by quotes, it&#39;s a string
  64. print(f&quot;String: {com[i]}&quot;)
  65. elif re.match(r&quot;^(\&quot;|\&#39;).*$&quot;, com[i]): # if it starts with a quote mark, it&#39;s the start of a long string
  66. string_parts.append(com[i])
  67. quote_mark = com[i][0]
  68. in_long_string = True
  69. elif re.match(r&quot;&gt;$&quot;, com[i]): # if it ends in a &gt;, it&#39;s an output thing
  70. print(f&quot;Output: {com[i+1]}&quot;)
  71. i += 1
  72. elif re.match(r&quot;&lt;$&quot;, com[i]): # likewise with inputs
  73. print(f&quot;Input: {com[i+1]}&quot;)
  74. else: # otherwise it&#39;s some other kind of argument
  75. arg_number += 1
  76. print(f&quot;Argument {arg_number}: {com[i]}&quot;)
  77. i += 1
  78. [process_command(command) for command in examples]

Comment with some more examples if it doesn't work for anything

huangapple
  • 本文由 发表于 2023年7月4日 21:06:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定