Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-15-2011, 01:25 PM   #1
Registered: Sep 2009
Posts: 97

Rep: Reputation: 0
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
Old 05-15-2011, 02:31 PM   #2
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Try this:
awk -v datum="$DATUM" -v workingdir="$workingdir" '
    { sub("\\[.*","",$5)
      file=workingdir "/" datum "/" $5
      sub(/\/+[^\/]*$/, "", cmd)
      cmd="mkdir -p \047" cmd "\047"
      print | cmd
      print > file }' "$1"
Had to edit the command for a few times

Last edited by Nominal Animal; 05-15-2011 at 02:46 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
awk: how can I print out a message to the screen when redirecting the output to file. quanba Programming 8 07-13-2015 02:54 AM
[SOLVED] awk print to file question takayama Programming 3 02-05-2011 05:21 PM
Text file manipulation: selecting specific lines/columns using awk and print CHARL0TTE Linux - Newbie 2 02-27-2010 03:40 AM
Can not capture no at end of file store it and print it using sed and awk amuthiga Programming 10 11-26-2008 08:37 AM
awk syntax to print particular record of xyz file? johnpaulodonnell Linux - Newbie 4 06-14-2007 08:47 AM

All times are GMT -5. The time now is 09:07 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration