YannickN.fr
YannickN.fr

Reputation: 360

How to avoid the return of system command with awk?

When I use awk with system command like this :

awk 'BEGIN{ if ( system("wc -l file_1") == 0 ) {print "something"} }' text.txt >> file_1

the result of system command is writen in my file file_1 :

0 file_1
something

How to avoid that? or just to redirect the output?

Upvotes: 1

Views: 337

Answers (1)

ghoti
ghoti

Reputation: 46856

You appear to be under the impression that the output of the system() function includes the stdout of the command it runs. It does not.

If you want to test only for the existence of a non-zero-sized file, you might do it using the test command (on POSIX systems):

awk '
  BEGIN{
    if ( system("test -s file_1") ) {     # a return value of 0 is "false" to awk
      print "something"
    }
  }' text.txt >> file_1

Upvotes: 1

Related Questions