The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
create folder in windows from unix while FTP Metalero de Oz UNIX for Dummies Questions & Answers 0 05-14-2008 01:22 PM
Pack current folder WebWatch UNIX for Dummies Questions & Answers 3 12-17-2007 05:46 AM
AIX - create folder in hdisk1 instead lwy2020 AIX 3 01-16-2007 04:56 AM
How can I create a file with current time - 60 minutes DaveyTN Shell Programming and Scripting 4 10-05-2005 08:17 PM
create directory named current date flowrats UNIX for Dummies Questions & Answers 2 07-30-2002 11:26 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-27-2008
ccox85 ccox85 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 27
Use awk to create new folder in current directory

Alright, I am sure this is a laughable question, but I don't know so I am going to ask anyway.

I have a little script I am writing to take information from one source, recode it in a certain way, and print to files for each subject I have data for. This all works perfectly. I just want to put a little icing on the cake if you will and make is so no matter where I run the script, or on what computer, it will always send all of the output to a new folder in the current directory.

To be more specific. Lets say I am running the script in /home/research/.
Instead of sending the output to the current directory proper, I want to create a new folder to stuff it all in, so it is not intermingled with my other files. I travel a lot, so if I hardcode " > "iat_exp/" et "_" sn ".out", I get an error if the folder iat_exp does not exist yet.

So, I want my awk script to create that folder when it starts running.

I hope this is enough detail... I am an inexperienced scripter and am just using awk in cygwin on my xp laptop, although I am setting up my first Ubuntu box, so I am pretty excited about that.

Thanks all,
Chris
  #2 (permalink)  
Old 01-27-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
Code:
ROOT="iat_exp"
CMDmkdir="mkdir -p " ROOT " 2>/dev/null"
system(CMDmkdir)
close(CMDmkdir)
....
file=ROOT "/" et "_" sn ".out"
print "foo" > file
....
  #3 (permalink)  
Old 01-27-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 741
Code:
awk '{"mkdir iat_exp" | getline; print stuff}' inputfile > iat_exp/outputfile
  #4 (permalink)  
Old 01-27-2008
ccox85 ccox85 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 27
Thanks, but...

thanks so much guys!

Last edited by ccox85; 01-27-2008 at 05:31 PM..
  #5 (permalink)  
Old 01-27-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,859
You can also make the directory in the BEGIN block

Code:
BEGIN {
    "mkdir iat_exp" | getline
}
{
   print ....
}
  #6 (permalink)  
Old 01-27-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 741
Quote:
Originally Posted by ccox85 View Post
thanks so much guys!

A better version that sends errors to /dev/null if the directory "iat_exp" already exists.

Code:
awk '{"mkdir iat_exp 2>&-" | getline;print stuff}' inputfile > iat_exp/outputfile
  #7 (permalink)  
Old 01-28-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Cochin/Singapore
Posts: 515
One more approch...by calling the system command

Code:
echo $a |awk '{system("mkdir directory");}'
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0