How to run a .awk file?

后端 未结 3 1869
轮回少年
轮回少年 2021-02-03 23:19

I am converting a CSV file into a table format, and I wrote an AWK script and saved it as my.awk. Here is the my script:

#AWK for test
awk -F , \'
    BEGIN {
           


        
相关标签:
3条回答
  • 2021-02-03 23:47

    The file you give is a shell script, not an awk program. So, try sh my.awk.

    If you want to use awk -f my.awk life.csv > life_out.cs, then remove awk -F , ' and the last line from the file and add FS="," in BEGIN.

    0 讨论(0)
  • 2021-02-04 00:04

    If you put #!/bin/awk -f on the first line of your AWK script it is easier. Plus editors like Vim and ... will recognize the file as an AWK script and you can colorize. :)

    #!/bin/awk -f
    BEGIN {}  # Begin section
    {}        # Loop section
    END{}     # End section
    

    Change the file to be executable by running:

    chmod ugo+x ./awk-script
    

    and you can then call your AWK script like this:

    `$ echo "something" | ./awk-script`
    
    0 讨论(0)
  • 2021-02-04 00:07

    Put the part from BEGIN....END{} inside a file and name it like my.awk.

    And then execute it like below:

    awk -f my.awk life.csv >output.txt
    

    Also I see a field separator as ,. You can add that in the begin block of the .awk file as FS=","

    0 讨论(0)
提交回复
热议问题