Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 12: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, 01: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 01: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 01:54 AM
[SOLVED] awk print to file question takayama Programming 3 02-05-2011 04:21 PM
Text file manipulation: selecting specific lines/columns using awk and print CHARL0TTE Linux - Newbie 2 02-27-2010 02: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 07:37 AM
awk syntax to print particular record of xyz file? johnpaulodonnell Linux - Newbie 4 06-14-2007 07:47 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 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