LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-15-2011, 12:25 PM   #1
takayama
Member
 
Registered: Sep 2009
Posts: 93

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: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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

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


Reply


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
[SOLVED] awk print to file question takayama Programming 3 02-05-2011 04:21 PM
awk: how can I print out a message to the screen when redirecting the output to file. quanba Programming 5 03-28-2010 05:25 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


All times are GMT -5. The time now is 11:44 PM.

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