Need help to write a simple awk script 
Author Message
 Need help to write a simple awk script

Here is the problem ...
I have an ascii file foo.txt with the following data:
FOO1
FOO2
FOO3
FOO3
........
What i am trying to do is to run an awk script and replace the FOO1, FOO2,
etc with the string CASE in my test.txt file.

My test.txt has the following data:
***************************
This is a test CASE.
***************************

After running the script, it becomes:
**************************
This is a test FOO1.
This is a test FOO2.
This is a test FOO3.
......
etc...
*************************

Thanks in advance.

-Alex



Mon, 20 Dec 1999 03:00:00 GMT  
 Need help to write a simple awk script

Unless I'm missing some of the generality of your question, this sounds
like a job for foreach:

Quote:
> foreach CASE (`cat foo.txt`)

foreach? echo "This is a test $CASE"
foreach? end
This is a test FOO1
This is a test FOO2
This is a test FOO3
This is a test FOO3

I'm confused as to what your test.txt would actually look like. Would it
really be one line, that you wanted to repeat with different endings?
If so, the previous foreach should work perfectly. If it's actually
different on each line, and you want a line for line mapping between the
two files, it sounds like you want paste.

For an awk script, I would operate on the foo.txt file, like this:

awk -f test.txt foo.txt

test.txt:
{ print "This is a test", $1 }

foo.txt:
FOO1
FOO2
FOO3
FOO3

Nick Russo

Quote:

> Here is the problem ...
> I have an ascii file foo.txt with the following data:
> FOO1
> FOO2
> FOO3
> FOO3
> ........
> What i am trying to do is to run an awk script and replace the FOO1, FOO2,
> etc with the string CASE in my test.txt file.

> My test.txt has the following data:
> ***************************
> This is a test CASE.
> ***************************

> After running the script, it becomes:
> **************************
> This is a test FOO1.
> This is a test FOO2.
> This is a test FOO3.
> ......
> etc...
> *************************

> Thanks in advance.

> -Alex



Wed, 22 Dec 1999 03:00:00 GMT  
 Need help to write a simple awk script


% My test.txt has the following data:
% ***************************
% This is a test CASE.
% ***************************
%
% After running the script, it becomes:
% **************************
% This is a test FOO1.
% This is a test FOO2.
% This is a test FOO3.
% ......
% etc...
% *************************

How about
  BEGIN { getline testline < "test.txt" }
  { outline=testline
    gsub("CASE", $1, outline)
    print outline
  }

You can extend this so that if your input file looked like
 FOO1 1 2
 FOO2 2 3
 ...
and test.txt looked like
 Test CASE: results RES1 and RES2.
you would get
 Test FOO1: results 1 and 2.
simply by adding more gsubs. You have to be careful that your variables
don't match your input data, though (for instance, calling them FOO1 and
FOO2 instead of RES1 and RES2 wouldn't work in this case).
--

Patrick TJ McPhee
East York  Canada



Thu, 23 Dec 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need help with simple awk script

2. Help with A simple awk script.

3. Need help with awk script

4. need help on awk script

5. Need little help with a awk script

6. need some help writing a awk command

7. writing a simple server in tcl - need help

8. Need help with simple regexp in tcl script

9. A simple question regarding shell scripts and awk

10. Problem with simple AWK script.

11. Simple awk command within a perl script

12. Simple awk script does not work?

 

 
Powered by phpBB® Forum Software