0

I'm quite new at Awk. I have a folder with an awk script and a file named MODELE1. I am trying to run my script, more specifically the lines:

awk '($1 != "GRID")     {print $0}'  MODELE1 > MAIL_SANS_GRID
awk "($1 == "GRID")     {print $0}"  MODELE1 | sort -n -k 2,2 > GRID_REP_COQ

But I get the error
^ invalid char ''' in expression For the '...'

I've tried to change it to ... and " ..." . In the first case, I get the same error message and in the second, I have a syntax error for the "sort".

Does anyone know how to solve it?

Thanks in advance.


EDIT: for clarity, here is the whole script, called grid_sorting:

#!/bin/awk -f

# Model and GRID cards extraction
awk '($1 != "GRID")     {print $0}'  MODELE1 > MAIL_SANS_GRID
# GRID sorting
awk '($1 == "GRID")     {print $0}'  MODELE1 | sort -n -k 2,2 > GRID_REP_COQ
# Model and sorted GRID cards concatenation
cat MAIL_SANS_GRID GRID_REP_COQ > MODELE

I then run the script in a terminal using the command

./grid_sorting

I get the errors:

awk: ./grid_sorting:4: awk '($1 != "GRID")     {print $0}'  MODELE1 > MAIL_SANS_GRID
awk: ./grid_sorting:4:     ^ invalid char ''' in expression
awk: ./grid_sorting:4: awk '($1 != "GRID")     {print $0}'  MODELE1 > MAIL_SANS_GRID
awk: ./grid_sorting:4:     ^ syntax error

If I switch to double quotes :

#!/bin/awk -f

# Model and GRID cards extraction
awk "($1 != "GRID")     {print $0}"  MODELE1 > MAIL_SANS_GRID
# GRID sorting
awk "($1 == "GRID")     {print $0}"  MODELE1 | sort -n -k 2,2 > GRID_REP_COQ
# Model and sorted GRID cards concatenation
cat MAIL_SANS_GRID GRID_REP_COQ > MODELE

I get the error:

awk: ./grid_sorting:6: awk "($1 == "GRID")     {print $0}"  MODELE1 | sort -n -k 2,2 > GRID_REP_COQ
awk: ./grid_sorting:6:                                                ^ syntax error

And the intermediate file MAIL_SANS_GRID is created but is empty.

elle.delle
  • 328
  • 3
  • 15
  • `But I get the error` Please post the whole error verbatim. Is there no `awk: something: something:` in front? The error is exactly `^ invalid char ''' in the expression For the '...'` ? `awk` prints `awk:` in front of errors. So you noticed that the first line is `awk '` and the second is `awk "`? – KamilCuk Jun 23 '21 at 15:36
  • Regarding `more specifically the line:` - what followed that statement is 2 lines, not 1. Which of those 2 lines is giving you the syntax error? – Ed Morton Jun 23 '21 at 18:05
  • Hi! Thank you all for your comments Ed Morton , it was a spelling mistake on my behalf, I meant lines rowboat , I am running the command ./grid_sorting (the name of the script) in a terminal @KamilCuk here is the full error statement with the original script (so with ' ... '): molx1801: ./grid_sorting awk: ./grid_sorting:4: awk '($1 != "GRID") {print $0}' MODELE1 > MAIL_SANS_GRID awk: ./grid_sorting:4: ^ invalid char ''' in expression awk: ./grid_sorting:4: awk '($1 != "GRID") {print $0}' MODELE1 > MAIL_SANS_GRID awk: ./grid_sorting:4: ^ syntax error – elle.delle Jun 24 '21 at 07:27
  • Does this answer your question? [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – tripleee Jun 24 '21 at 08:16
  • 1
    @elle.delle please don't add information in comments where it can't be formatted and can easily be missed. [edit] your question to include all relevant information. Also, for my specific question I wasn't looking for you to just change "1 line" to "the lines" in the text, I was asking you to **tell us which line** produces the error message you show below the 2 lines. – Ed Morton Jun 24 '21 at 11:59

2 Answers2

3

Change the first line #!/bin/awk -f into #!/bin/bash or any equivalent shell:

#!/bin/bash

# Model and GRID cards extraction
awk '($1 != "GRID")     {print $0}'  MODELE1 > MAIL_SANS_GRID
# GRID sorting
awk '($1 == "GRID")     {print $0}'  MODELE1 | sort -n -k 2,2 > GRID_REP_COQ
# Model and sorted GRID cards concatenation
cat MAIL_SANS_GRID GRID_REP_COQ > MODELE

The first line is called Shebang. It indicates which program is going to decode your script. See: Shebang.

If you put a path with awk there (which is generally not recommended), your script must contain pure awk statements. But if you call awk from the command line, including some filename and some redirection to sort, as in your example, your script must be interpreted from a shell command interpreter, like bash.

With other words, it makes no sense to call awk from inside awk as you did in your script having the wrong shebang.

Pierre François
  • 5,850
  • 1
  • 17
  • 38
0

This is not a problem related to awk directly, but a problem related to bash or sh. You should use single quotes in the second line as you do in the first one:

awk '($1 != "GRID")     {print $0}'  MODELE1 > MAIL_SANS_GRID
awk '($1 == "GRID")     {print $0}'  MODELE1 | sort -n -k 2,2 > GRID_REP_COQ

Otherwise your shell substitutes the $0 and $1 with some values.

Doj
  • 1,244
  • 6
  • 13
  • 19
  • "it didn't work" is the worst possible problem statement as it tells us nothing about the problem that we could use to try to fix the problem. Imagine feeling sick and just telling your doctor "I don't work" or taking your car to a mechanic to fix a problem and just saying "it doesn't work" instead of describing any symptoms. – Ed Morton Jun 24 '21 at 11:58