1

We know that we can run commands from vim using :!command <args>. For example:

:%!awk '{ print $2,$1,$3 }' FS=',' OFS=','
:%!sort

I am having trouble running an awk script within vim. I can run the script from another terminal like this:

awk -f denorm.awk processed_l1.csv > processed_l2.csv

I get the following error for running (:!awk -f denorm.awk) from vim:

awk: fatal: can't open source file `denorm.awk' for reading (No such file or directory)
shell returned 2

And when I give absolute path to the file denorm.awk. Then empty screen pops up, no errors!

So how can I run this awk script from vim?

I would prefer to continue in the existing csv file. This will allow me to revert back in case I make any mistakes.

3
  • 4
    Welcome to Vi and Vim! Have you checked :pwd? When using a relative path, awk assumes the path is relative to the current directory. Also, as far as keeping the existing csv, you should use the filter syntax (:%!awk -f script)
    – D. Ben Knoble
    Jan 10, 2021 at 15:25
  • Yes, both of your suggestions are right. If I click open a file in vim, then the path will be set as /home/user/ ; and I unknowingly missed the % symbol in :%!awk . That's why after processing, the results are not shown on screen.
    – Mithun B
    Jan 10, 2021 at 15:50
  • 2
    You could explain that in an answer, and then "accept" the answer to show that this question has been answered :)
    – alec
    Jan 10, 2021 at 18:08

1 Answer 1

3

When using a relative path, awk assumes the path is relative to the current directory. In the comments, you note that :pwd gives /home/user; that means you need to give either

  • an absolute path to denorm.awk, or
  • a path relative to /home/user (in general, relative to the :pwd)

As far as keeping the existing csv, you should use the filter syntax (:%!awk -f denorm.awk) rather than the external-command syntax (:!awk …).

:{range}![!]{filter} [!][arg]               *:range!*
            Filter {range} lines through the external program
            {filter}.  Vim replaces the optional bangs with the
            latest given command and appends the optional [arg].
            Vim saves the output of the filter command in a
            temporary file and then reads the file into the buffer
            |tempfile|.  Vim uses the 'shellredir' option to
            redirect the filter output to the temporary file.
            However, if the 'shelltemp' option is off then pipes
            are used when possible (on Unix).
            When the 'R' flag is included in 'cpoptions' marks in
            the filtered lines are deleted, unless the
            |:keepmarks| command is used.  Example: >
                :keepmarks '<,'>!sort
<           When the number of lines after filtering is less than
            before, marks in the missing lines are deleted anyway.

The filter syntax implicitly feeds the given range (% is the range for the whole file) as standard-in to a "filter" program; the output is captured and replaces the corresponding range.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.