Use awk to create new folder in current directory


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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  
Code:
ROOT="iat_exp"
CMDmkdir="mkdir -p " ROOT " 2>/dev/null"
system(CMDmkdir)
close(CMDmkdir)
....
file=ROOT "/" et "_" sn ".out"
print "foo" > file
....

# 3  
Code:
awk '{"mkdir iat_exp" | getline; print stuff}' inputfile > iat_exp/outputfile

# 4  
Thanks, but...

thanks so much guys!

Last edited by ccox85; 01-27-2008 at 05:31 PM..
# 5  
You can also make the directory in the BEGIN block

Code:
BEGIN {
    "mkdir iat_exp" | getline
}
{
   print ....
}

# 6  
Quote:
Originally Posted by ccox85
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  
One more approch...by calling the system command

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

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create directory and sub-directory with awk and bash

In the below I am trying to create a parent directory using the R_2019 line from f1 if what above it is not empty. I then create sub-directories under each parent if there is a match between $2 of f1 and $2. Inside each sub-folder the matching paths in $3 and $4 in f2are printed. If there is no... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to create link, download, and extract in sub-directory

The awk below will create sub-directories in a directory (which is always the last line of file1, each block separated by an empty line), if the number in line 2 (always the first 6 digits in the format xx-xxxx) of file2 is found in $2 of file1. This is the current awk output. If there is a... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

4. Shell Programming and Scripting

Create a folder under different user directory

Hello All, I have to write a shell script and use it in informatica. The script has to perform below actions: The script gets executed from edw user. Through the script, a DT folder has to be created under edw_sca user. Is this scenario possible through a SHELL script or not. ... (2 Replies)
Discussion started by: bghosh
2 Replies

5. UNIX for Advanced & Expert Users

current directory in awk

Hello, I want to use the string with the current directory in my awk command. I tried: 'pwd=system("pwd")' but it doesn't work. can please help somebody? (2 Replies)
Discussion started by: daWonderer
2 Replies

6. Shell Programming and Scripting

Create a directory named as the current date

I am preparing a shell script to backup a few config files on a daily basis, with a retention of 30 days. Need some help with syntax/examples: The shell script (running as cron) would require the following: 1. create a sub-directory within a specified (backup) directory, in the format... (3 Replies)
Discussion started by: FeNiCrC_Neil
3 Replies

7. UNIX for Dummies Questions & Answers

Zip recursive content of folder when (not current directory=

Hi, Is there a way to zip the content (recursively) of a folder other then the current directory, and keep the directory structure intact? Example: /var/tmp/myfolder ----------------- file1 ----------------- file2 ----------------- folder1 ------------------------ file3 Now I want... (3 Replies)
Discussion started by: jimih
3 Replies

8. UNIX for Dummies Questions & Answers

Create a directory using current date

Hi, I have a question, is there any way I can, when i create a directory, put the current date on it so that the directory name will be "name-current date"? just curious (3 Replies)
Discussion started by: aric87
3 Replies

9. Shell Programming and Scripting

Finding files in current directory when 100,000's files in current directory

Hi All I was wondering what is the most efficient way to find files in the current directory(that may contain 100,000's files), that meets a certain specified file type and of a certain age. I have experimented with the find command in unix but it also searches all sub directories. I have... (2 Replies)
Discussion started by: kewong007
2 Replies

10. UNIX for Dummies Questions & Answers

create directory named current date

Since this site solved my problems before, I am back for more (solutions) I down load via a script every day a file that has the same name as the file of the day before. I want to move that file to its own directory like: /archive/jul30 How do I capture the systems date in a script an... (2 Replies)
Discussion started by: flowrats
2 Replies

Featured Tech Videos