LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Awk+print, create file if not exixt (http://www.linuxquestions.org/questions/programming-9/awk-print-create-file-if-not-exixt-880855/)

takayama 05-15-2011 12:25 PM

Awk+print, create file if not exixt
 
I having the following awk line in a script, is it possible to modify the script so that it create a the directory if it dosnt exist (similar to mkdir -p)

awk -v datum="$DATUM" -v workingdir="$workingdir" '{ sub("\\[.*","",$5); print > workingdir"/"datum "/" $5;}' $1

Nominal Animal 05-15-2011 01:31 PM

Try this:
Code:

awk -v datum="$DATUM" -v workingdir="$workingdir" '
    { sub("\\[.*","",$5)
      file=workingdir "/" datum "/" $5
      cmd=file
      sub(/\/+[^\/]*$/, "", cmd)
      cmd="mkdir -p \047" cmd "\047"
      print | cmd
      close(cmd)
      print > file }' "$1"

Had to edit the command for a few times ;)


All times are GMT -5. The time now is 03:08 AM.