1

I want to remove extra headings from my file and am using the following:

awk '{ print $1}' test.txt > output.txt

And then I get the next few lines back in response:

    File "<ipython-input-23-aa9db656210f>", line 1
    awk '{ print $1}' test.txt > output.txt
                    ^
SyntaxError: invalid syntax

Not really sure where I am going wrong, is there something I am missing about the awk command? Do I need to install some extra package? I am using Spyder IDE...

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
Jasmine
  • 11
  • 1
  • 3
    Awk is a bash command, not a python command. Bash and python are different languages so you cant use bash commands when coding in python. – gbtimmon Jun 28 '18 at 18:32
  • 2
    @gbtimmon It's not part of Bash, though. It's a separate executable that you can call from any shell. – Benjamin W. Jun 28 '18 at 19:04
  • 5
    You can run bash commands in IPython by prefixing them with `!`. In this case this corresponds to `!awk '{ print $1}' test.txt > output.txt` – Carlos Cordoba Jun 28 '18 at 21:21
  • Even if you do not use IPython there are ways to access your external shell in Python: https://stackoverflow.com/questions/89228/calling-an-external-command-in-python – Hielke Walinga Jun 29 '18 at 09:30
  • @BenjaminW. good clarification -- I was trying to keep my response on a level that someone making this mistake would understand so I intentionally left that complication out. – gbtimmon Jun 29 '18 at 15:01
  • Are you trying to remove a single header row from a file? If so you can use `!awk '{if (NR!=1) {print}}' test.txt > output.txt` – scmz Jul 07 '18 at 23:32

1 Answers1

1

From Carlos Cordoba's comment:

You can run bash commands in IPython by prefixing them with !. In this case this corresponds to !awk '{ print $1}' test.txt > output.txt

wjandrea
  • 28,235
  • 9
  • 60
  • 81