How to use input parameters to create new file? 
Author Message
 How to use input parameters to create new file?

I want to make awk program "dostuff" which takes filename "infile.log" as
parameter from command line and then does the stuff and prints the results
in "infileX.log". Notice that the new file must contain the same beginning
as the original file given in command parameter.

So when I give the command:

dostuff infile.log

a new file called infileX.log is created.

Best regards,
inox



Mon, 18 Apr 2005 15:58:12 GMT  
 How to use input parameters to create new file?

Quote:

> I want to make awk program "dostuff" which takes filename "infile.log" as
> parameter from command line and then does the stuff and prints the results
> in "infileX.log". Notice that the new file must contain the same beginning
> as the original file given in command parameter.

> So when I give the command:

> dostuff infile.log

> a new file called infileX.log is created.

     The easiest way is to wrap it in a shell script:

         logfile=infile.log
         outfile=${logfile%.log}X.log
         dostuff $logfile > $outfile

     That assumes a POSIX-type shell; for a Bourne shell:

         outfile=`expr "$logfile" : '\(.*\).log'`X.log

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



Mon, 18 Apr 2005 16:43:16 GMT  
 How to use input parameters to create new file?
Thanks. I actually managed to do almost what I wanted by following (prints
fields from 18 onwards to a new file beginning with x_

awk 'NF > 17 { for (i = 18; i <= NF; i++) {printf "%s ",$i};  }' "$1" >
x_"$1"

But why is this not working if I add "#!/bin/awk -f" to the start of file?
It works fine without but adding ordinary comments starting with "#" cause
unwanted behaviour too. Why?

-inox



Mon, 18 Apr 2005 17:43:07 GMT  
 How to use input parameters to create new file?

Quote:

> Thanks. I actually managed to do almost what I wanted by following (prints
> fields from 18 onwards to a new file beginning with x_

> awk 'NF > 17 { for (i = 18; i <= NF; i++) {printf "%s ",$i};  }' "$1" >
> x_"$1"

> But why is this not working if I add "#!/bin/awk -f" to the start of file?
> It works fine without but adding ordinary comments starting with "#" cause
> unwanted behaviour too. Why?

     Because your file contains a shell script, not an awk program.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



Mon, 18 Apr 2005 18:06:12 GMT  
 How to use input parameters to create new file?

hola.. an easy one!

if you are using gawk (maybe the other awks, but I don't have
references at home...)
in "dostuff" just use
FNR==1 {split(FILENAME,tmp,"\.");outfile=tmp[1] "X." tmp[2]}
{print $0 > outfile}

there you go :)
jen

Quote:

> I want to make awk program "dostuff" which takes filename "infile.log" as
> parameter from command line and then does the stuff and prints the results
> in "infileX.log". Notice that the new file must contain the same beginning
> as the original file given in command parameter.

> So when I give the command:

> dostuff infile.log

> a new file called infileX.log is created.

> Best regards,
> inox



Wed, 20 Apr 2005 23:33:44 GMT  
 How to use input parameters to create new file?
Hello,

Quote:

> FNR==1 {split(FILENAME,tmp,"\.");outfile=tmp[1] "X." tmp[2]}
> {print $0 > outfile}

yes, it works.  But maybe the explanation is a bit different from
what someone could think.
(It was probably only a typo on Jen's side, but I'll take the opportunity
to present a lecture ;-)

The trick is in the split():

1) split(s, a, ".")

uses the one-char separator dot.  It has no special meaning, as
it is one-char field separator, not regexp.

2) split(s, a, /\./)

uses regexp separator, the regexp matches a dot.  /[.]/ would be
another alternative.

3) split(s, a, "\\.")

is equivalent to the previous one: the third argument is a two chars long
string.  The first character is backslash, second is dot.

4) split(s, a, "\.")

is thus equivalent to 1), as "\." is just another way to type ".".
GNU awk 3.1.1 issues a warning:
gawk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'

OK, when I have made so much fuss about one typo, I can humbly suggest an
alternative:

FNR==1 {outfile=FILENAME; sub(/\./,"X.",outfile)}
{print $0 > outfile}

Have a nice day,
        Stepan



Fri, 22 Apr 2005 15:10:52 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Call a SQL stored procedure using input and output parameters

2. How to replace or create a file using the open/create/replace.vi

3. Need help on file parameter input

4. flexible input parameter file

5. storing multiple parameter sets in an input file

6. Tcl_EvalFile & file input parameters

7. C4 - Fourth parameter using Create

8. Parse input file and create widgets

9. New serial input device: using smalltalk/vw polling system

10. language rules question: using a (passed in) parameter to define a parameter

11. Using extracted text to create a new filename

12. Using API to create new controls that are not available in CW

 

 
Powered by phpBB® Forum Software