It is currently Tue, 07 Dec 2021 01:39:46 GMT



 
Author Message
 sh script using awk, argument issue?
Hi,

I manipulated a text file in shell command window, making use a few
commands such as sed, awk, paste, etc. Now I am going to joint all the
commands in a script, but the arguments for awk must clash with the
script arguments. How can I find a way out?

for example, I used following commands,
%awk '{print(NR, $0)}' File1 > tp
%paste tp File2 > tp2
%awk '{if ($2==$3) print $0>"out"; else printf("%s %s--%s",$1,$2,$3)

and I would like to combine them into one script 'CmpEle' making use
the syntax
%CmpEle File1 File2

What should I do? Thanks for your advice.

Gozojev

Sent via Deja.com
http://www.**-**.com/



 Mon, 02 Jun 2003 11:21:31 GMT   
 sh script using awk, argument issue?

My understanding is that your use of '' below causes the $0, $1, etc. to
be given literally to awk, and not expanded to the script's parameters.
If you'd used "", then you'd run into the above problem.

I'm no expert, but I would convert it into the following.  I haven't
changed the behavior of your program, but there's surely a simpler
way to write it (and I'd clean up tp and tp2 if they're temporary
when you're done):

    #!/bin/sh -f                                                        
    if [ $# -ne 2 ] ; then
        echo usage: $0 File1 File2
        exit
    fi

    for i in $1 $2 ; do
        if [ ! -r $i ] ; then
            echo $0: $i must be a readable file
            exit
        fi
    done

    awk '{print(NR, $0)}' $1 > tp
    paste tp $2 > tp2
    awk '{if ($2==$3) print $0>"out"; else printf("%s %s--%s",$1,$2,$3)
    "out"}' tp2

And I'd run it with sh CmpEle or ./CmpEle, since . isn't in my path and
this program wouldn't be going in any directory which is :-)

I hope that if there are any errors here, one of the resident experts
will correct me.

--
naisb...@enteract.com



 Mon, 02 Jun 2003 15:39:27 GMT   
 sh script using awk, argument issue?
 > I manipulated a text file in shell command window, making use a few
 > commands such as sed, awk, paste, etc. Now I am going to joint all the
 > commands in a script, but the arguments for awk must clash with the
 > script arguments. How can I find a way out?
[...]

The following article should help you:

        http://www.oase-shareware.org/shell/goodcoding/awkinvoke.html

Heiner
--
 ___ _
/ __| |_ _____ _____ _ _     Heiner STEVEN <heiner.ste...@odn.de>
\__ \  _/ -_) V / -_) ' \    SHELLdorado for Shell Script Programmers:
|___/\__\___|\_/\___|_||_|   http://www.oase-shareware.org/shell/



 Thu, 05 Jun 2003 06:37:54 GMT   
 
   [ 3 post ] 

Similar Threads

1. newbie: (command and its arguments) as a sh script argument

2. /bin/sh passing arguments to sourced script

3. passing arguments from awk to another script

4. awk processing in sh script

5. awk and sh variables in a shell script

6. awk and sh variables in a shell script

7. Passing a string Varible from AWK to a sh Script

8. sh vars in awk scripts??

9. passing script argument as password after useradd command issued

10. Using awk and sh variables


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software