0

Ive to run awk from the python. When I run the script from the terminal, gives the desired output but showing error when executing from inside the python.

runAwk = '''awk '{printf $1}{for(i=2;i<=NF;i++)printf "|"$i}{printf "\n"}' final.txt'''

os.system(runAwk)

gives the error:

awk: line 1: runaway string constant " ...

when I surfed from the web, I found that awk can not be used with os module and there are not much contents. I am confused how to proceed ahead.

nancy li
  • 33
  • 3
  • possible duplicate of [What exactly do "u" and "r" string flags do in Python, and what are raw string literals?](http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l) – simonzack Sep 20 '14 at 10:05
  • 1
    @simonzack although the error was caused by `\n` being treated specially by Python, that's some way from being a duplicate. – Zero Piraeus Sep 20 '14 at 10:08
  • @simonzack: how could you say that my post is duplicate? If I've known that the error was caused due to absence of "r" string, I didn't have post this question. How could you've searched for the solution if you were a newbie and faced the same problem. Please correct your bogus statement. – nancy li Sep 20 '14 at 12:23
  • @ZeroPiraeus Judging by your rep, you probably have seen this question be posted dozens of times on stackoverflow. Do you really want every question like this be answered by 10 competing answers instead of being marked as duplicate? – simonzack Sep 20 '14 at 15:50
  • @nancyli I wanted to point you in the right direction, and you meet me with aggression. Fix your attitude problem and read a programming book. – simonzack Sep 20 '14 at 15:52
  • @simonzack I hadn't seen this question here before, no - and although it's obvious what's happening if you already know, it struck me as something that would be hard to google if you didn't. I'm quite active in closing genuine dupes; I don't think this is one. – Zero Piraeus Sep 20 '14 at 16:32
  • @simonzack: I was not aggressive with you. I just wanted to change the attitudes of the every programmers who find more enjoyment in giving **negative points and tagging posts as duplicates** rather than giving suggestions. Are they born with programming skills? **you are good programmer does not mean every puzzle is under your knee. remember this and often you also make silly things**. – nancy li Sep 21 '14 at 03:28
  • @ZeroPiraeus: Can you answer me one thing, **why in this forum people find more enjoyment in marking negative points to a problem rather than giving suggestions**? Do they have the solution to every problem they face? – nancy li Sep 21 '14 at 03:32

1 Answers1

1

The \n in your runAwk string is being interpreted by Python as a literal newline character, rather than being passed through to awk as the two characters \ and n. If you use a raw string instead, by preceding the opening triple-quotes with an r:

runAwk = r'''awk '{printf $1}{for(i=2;i<=NF;i++)printf "|"$i}{printf "\n"}' final.txt'''

... then Python won't treat \n as meaning "newline", and awk will see the string you intended.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • thanks for the prompt answer. Can you please elaborate this "raw string" concept you mention here. How does this help in executing the awk script. – nancy li Sep 20 '14 at 09:59
  • 1
    @nancyli: see [What exactly do "u" and "r" string flags do in Python, and what are raw string literals?](http://stackoverflow.com/q/2081640) – Martijn Pieters Sep 20 '14 at 10:02
  • @nancyli the link provided by Martijn is worth reading, as is the link in my answer. I've also expanded my answer a little. – Zero Piraeus Sep 20 '14 at 10:06
  • @ZeroPiraeus,@Martjin Pieters thank you everyone for the help. – nancy li Sep 20 '14 at 12:25