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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic file names

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am trying to pipe output to a filename which will change depending on the input filename
.i.e

nawk '{
if ok print inputfile.done
else print inputfile.dis
}' inputfile.txt


thanks in advance
 

NiallMc,

if I quite understand your problem you can do this with good old awk:

{
...

if (ok)
print > "inputfile.done"
or
print > "inputfile.dis"

...
}

You can also append

print >> "inputfile.dis"

or even pipe output into another precess

print || "mail admin"
.

I hope this helps.

Bye!

KP.
 
if ( FNR == 1) fileDone=FILENAME ".done" ;
print "ok" >> fileDone
 

Oops,

pipe sign is "|":

print | "mail admin"
.

Bye again!

KP.
 
Hi.
Example using your filename format with the period
extension separator.

awk ' {
if (test) {
nind = match(FILENAME,/\./)
name = substr(FILENAME,1,nind - 1)
pat = "/home/user/"name
print $0 > pat
}
}' file.txt

That should work....
 
The question was how to make the output file DYNAMICLY named based on the name of the impit file

vlad
 
awk '
BEGIN {
DoneFile = FILENAME ".done" ;
DisFile = FILENAME ".dis" ;
}
{
. . . .
if (ok)
print $0 >> DoneFile
else
print $0 >> DisFile
} ' inputfile

Jean Pierre.
 
The "BEGIN" block gets executed ONCE before _any_ record is processed. "FILENAME" is NOT set and "BEGIN" doesn't get reevaluated for EVERY specified file to be processed.


>Variable initializations that appear before the first file on >the command line are performed immediately after the BEGIN >action. Initializations appearing after the last file are >performed immediately before the END action. For more >information on BEGIN and END, see Patterns.

vlad
 
vgersh,
You could use any extension you wanted after you extract the basename.

So Niall Mc, using a true 'dynamic" might look
something like this:

awk '
function basename(FILENAME, nind,name) {
nind = match(FILENAME,/\./)
name = substr(FILENAME,1,nind - 1)
return name
}

BEGIN {
printf "Extension to use for matches: "
getline ext1 < &quot;-&quot;
printf &quot;Extension to use for no-matches: &quot;
getline ext2 < &quot;-&quot;
}
{
if (test) {
basename()
pat = name&quot;.&quot;ext1
print $0 > pat
exit
} else {
basename()
pat = name&quot;.&quot;ext2
print $0 > pat
exit
}' file.txt

Not tested but it looks right.
 
Oops, FILENAME needs to be fed to the function.
basename(FILENAME).

bye.
 
marsd,

that was _exactly_ my point - to make it a&quot;trully&quot; dynamic, you can NOT set the output filename in the BEGIN block - you have to reset it for every NEW file to be processed.

vlad
 
for xx in $PWD/*
do


awk -v a=$xx '
function basename(FILENAME, nind,name) {
nind = match(FILENAME,/\./)
name = substr(FILENAME,1,nind - 1)
return name
}

BEGIN {
printf &quot;Extension to use for a: &quot;
getline ext1 < &quot;-&quot;
printf &quot;Extension to use for no-matches on a: &quot;
getline ext2 < &quot;-&quot;
}

{
if (test) {
basename(FILENAME)
pat = name&quot;.&quot;ext1
print $0 > pat
exit
} else {
basename(FILENAME)
pat = name&quot;.&quot;ext2
print $0 > pat
exit
}' $xx

done

Does this work?
 
Thanks for the help guys. I wasn't aware of the FILENAME variable. vlad's piece of code works fine.
if ( FNR == 1) fileDone=FILENAME &quot;.done&quot; ;
print &quot;ok&quot; >> fileDone
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top