Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy way to print to a file in a subdirectory? 2

Status
Not open for further replies.

Czar24

Technical User
Mar 22, 2004
16
US
In my awk script I would like to print a file to a subdirectory created by variables.

instead of the command {print >> file} to print to a file in the current directory I'd like to send it to:

/subdirectory1/subdirectory2/file

where subdirectory1 and subdirectory2 are previously defined variables and existing folders.

I've tried a several things and no dice and as always I appreciate any help that may point be back in the right direction!


Scott
 
print >> ( "/" subdirectory1 "/" subdirectory2 "/" filefile)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Can you post your script and tell where you're stuck ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I thought I must be close. I tried what vlad had, exept for the ()s. I'll try that when I'm back at the office and let ya'll know how it works... I don't feel too bad since I was close on my first try at it, but alas, close only counts in horseshoes, hand grenades, and slow dancing -- but not coding! This stuff is still new to me and I appreciate the assistance!

I'll let ya'll know how it works in the morning!
 
Okay, still stuck...

Here's the file I'm processing with my awk/nawk code:

11111111111111 dont want
11111111111112 start.*101-01
11111111111113 file 1 text
11111111111114 stop
11111111111115 dont want
.
.
.

What the code is supposed to do is create a file called
/test101/test101-01/test101-01.11111111111112

which contains the text:

11111111111112 start.*101-01
11111111111113 file 1 text
11111111111114 stop

and so on for subsequent entries. My code is currently:



nawk '
/start/{{
if (file) close(file)
date = substr( $0, 1 , 14 )
testnum = substr ( $0, length($0)-5 , 3)
TEST = "test" testnum
runnum = substr ( $0, length($0)-2 , 3)
file = TEST runnum "." date }
{if(system("test -d " TEST) != 0) {system("mkdir " TEST)}}
{if(system("test -d " TEST "/" TEST runnum) != 0) {system("mkdir " TEST "/" TEST runnum)}}}
/start/,/stop/ {print >> ("/" TEST "/" TEST runnum "/" file)}
/stop/ { if (file != "") close(file)}
' inputfile



When I run this, the code sucessfully creates the directory structure for all the entries in the input file. When I only have it create the files in the current directory, it creates all the files and their contents as desired. However, when I tell it to create the files and place them in their indended directory, I get the following error:

nawk: can't open file /test101/test101-01/test101-01.11111111111112
input record number 2, file inputfile
source line number 11

even when I only run it for the first file only (short test input file) and tell it the directory I want it to go to which already exhists, I still get the same error.

any ideas?
 
Are you suer your file system accept file name longer than 14 characters ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I can create the file and long name in the current directory without issue. It only gives me greif when I try to place it into a subdirectory.
 
replace

{if(system("test -d " TEST) != 0) {system("mkdir " TEST)}}
{if(system("test -d " TEST "/" TEST runnum) != 0) {system("mkdir " TEST "/" TEST runnum)}}}

with:
Code:
system("mkdir -p " TEST "/" TEST runnum "2> /dev/null")

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I think that you are trying to make the new directory in the root directory. To fix: remove highlighted code...

/start/,/stop/ {print >> ([highlight]"/"[/highlight] TEST "/" TEST runnum "/" file)}


 
Ygor, that worked! Thank you very much.

Vlad, thanks for the idea for streamlining the code, I'll working on that now too! Having to account for all my "{}"s


Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top