62393

Escape awk special character in Python

Question:

I have a Fabric task as follows:

@task def getCrons(): timeStampNowServer = sudo("date +%s%3N", pty=False) cronLogFiles = sudo( "find /home/logs/cron/ -maxdepth 2 -type f -mtime -1 -name '*.log'", pty=False) cronLogFiles = cronLogFiles.splitlines(True) for cronLog in cronLogFiles: info = sudo( "awk '/END$/ {prev=$0; next}; /^#RETURN/ && $2>0 {cur=$0; pr=1; next}; pr {printf \" % s\n % s\n % s\n\", prev, cur, $0; pr=0}'{0}".format(cronLog), pty=False) print(info)

I am having the following traceback:

Traceback (most recent call last): File "/usr/lib/python2.7/site-packages/fabric/main.py", line 743, in main *args, **kwargs File "/usr/lib/python2.7/site-packages/fabric/tasks.py", line 379, in execute multiprocessing File "/usr/lib/python2.7/site-packages/fabric/tasks.py", line 274, in _execute return task.run(*args, **kwargs) File "/usr/lib/python2.7/site-packages/fabric/tasks.py", line 174, in run return self.wrapped(*args, **kwargs) File "/home/lbn/k.sewnundun/fabfile/kse/test.py", line 18, in getCrons "awk '/END$/ {prev=$0; next}; /^#RETURN/ && $2>0 {cur=$0; pr=1; next}; pr {printf \"%s\n%s\n%s\n\", prev, cur, $0; pr=0}'{0}".format(cronLog), pty=False) KeyError: 'prev=$0; next'

The command I want to execute on the server is:

awk '/END$/ {prev=$0; next}; /^#RETURN/ && $2>0 {cur=$0; pr=1; next}; pr {printf "%s\n%s\n%s\n", prev, cur, $0; pr=0}' mylog.LOG

However I am unable to escape the characters in the line:

info = sudo( "awk '/END$/ {prev=$0; next}; /^#RETURN/ && $2>0 {cur=$0; pr=1; next}; pr {printf \" % s\n % s\n % s\n\", prev, cur, $0; pr=0}'{0}".format(cronLog), pty=False)

How do I make it run correctly?

Answer1:

The issue was solved by escaping the { and the awk new lines:

info = sudo("awk '/END$/ {{prev=$0; next}}; /^#RETURN/ && $2>0 {{cur=$0; pr=1; next}}; pr {{printf \"%s\\n%s\\n%s\\n\", prev, cur, $0; pr=0}}' {0}".format(cronLog), pty=False)

<a href="https://docs.python.org/2/library/string.html#format-string-syntax" rel="nofollow">https://docs.python.org/2/library/string.html#format-string-syntax</a>

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.

Recommend

  • XMLHttpRequest ReadyState always undefined
  • CKEditor 4 - how to add font family and font size controls to the toolbar
  • Copy APK file from build location to some other location in gradle 4.4
  • DBCP Connection properties
  • Why TrayIcon doesn't appear?
  • Proper implementation of producer-consumer scenario and “graceful” termination of thread pool
  • celery for different timezones
  • Update Stripe Credit Card with Coffeescript
  • Get a list of who has what access to git repositories
  • Decompress string in java from compressed string in C#
  • Auto-incrementation with HSQLDB (2.2.8) + DDLUtils
  • The output is not displayed in its entirety [8086 assembly]
  • Covert RFC3339 DateTime to Date in java [duplicate]
  • I have a problem with the Fetch API using Internet Explorer 11
  • Read specific column from Parquet without using Spark
  • Slice assignment to tensorflow variable
  • ASP.NET MVC razor view, post to a different model from the original model binding?
  • Year over Year Stats from a Crossfilter Dataset
  • How can i move Clearcase dyamic/snapshot views to another host (Linux)
  • How to compile gSoap with ssl enabled on windows?
  • xpath assertion failure with dynamic xpath
  • how to run a different select statement based on condition in Hive SQL
  • Ruby regex for matching simpliest Ruby's regexes
  • Create an average of multiple excel chart without the data source
  • Why is ordered choice in pyparsing failing for my use case?
  • how to run ejabberd with Erlang on Heroku?
  • How to use Streams api peek() function and make it work?
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Google App Engine Datastore: Dealing with eventual consistency
  • Debug `Unexpected end of JSON input Error` on content script
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Grails - How to implement a foreign key relationship not using an id column?
  • Using Service Component Runtime
  • Firebase: How to read from external DB?
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • convert json to excel in java
  • How to handle a codeigniter PDF generator