Computing.Net > Forums > Unix > How to use AWK command

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to use AWK command

Reply to Message Icon

Name: Neeraja
Date: January 27, 2005 at 03:35:18 Pacific
OS: Unix
CPU/Ram: 256MB
Comment:

Hello,
Please find the script below:
the functionality of the script is , it picks up files like A.B.C.D.E where D->UNCRYPTED/ZIP from $FILETEMP1 directory and then removes the D.E and moves A.B.C to a specified directory.

My requirement now is how to replace the cut command with AWK.

for fic in `cat $UEXTMP/$FILETEMP1| grep "\.UNENCRYPTED"|grep -v \.ZIP`
do
if [ -s $UEXDATA_IN/$FOLDER_T04/$fic ]
then
NAME=`echo $fic|cut -f 1-3 -d "."`
mv $UEXDATA_IN/$FOLDER_T04/$fic $UEXDATA_IN/$FOLDER_T04/$FOLDER_UNZIP/$NAME
fi
done

Thanks in advance for your help.



Sponsored Link
Ads by Google

Response Number 1
Name: cdac1000
Date: January 27, 2005 at 03:54:51 Pacific
Reply:

NAME=`echo $fic | awk -F"." '{print $1 $2 $3}'

awk -F"." uses . as field seperator. But if cut is doing all that you want, what;s the big point in using awk ?


0

Response Number 2
Name: Neeraja
Date: January 27, 2005 at 04:33:29 Pacific
Reply:

Hi ,

Thanks for the info.

Would like to know which is more feasible, cut or awk command?

Regards,
Neeraja


0

Response Number 3
Name: Jim Boothe
Date: January 27, 2005 at 12:08:35 Pacific
Reply:

The main difference in the two would be in resources consumed.

I prefer to use the simplest command possible.  awk can do so much more that cut, so one would expect cut to be "leaner and meaner".  When used in a loop, you are creating and running these processes over and over, so a difference in the two can really add up.

I timed a 1000-iteration loop with both cut and awk, and I sent the output to /dev/null so as not to consume any time going to the screen:

k=1
while [ $k -le 1000 ]
do
echo abcdef | cut -c2,3 > /dev/null
((k=k+1))
done

k=1
while [ $k -le 1000 ]
do
echo abcdef | awk '{print substr($0,2,2)}' > /dev/null
((k=k+1))
done

$ time ./cut.sh
real        8.6
user        2.8
sys         7.3

$ time ./awk.sh
real       11.3
user        3.9
sys         9.0

I ran these timings several times to make sure I was getting consistent results.  As you can see, with 1000 executions, cut was more efficient on my HP-UX by a significant but not overwhelming margin.

However, this type of test would seem to put a lot of emphasis on process creation (1000 of them) and very little on each process execution.  So I decided to time cut and awk on a single long task.  I timed the following two commands on a file with 370,000 lines:

cut -c2,3 bigfile > /dev/null

awk '{print substr($0,2,2)}' bigfile > /dev/null

time cut.sh
real        3.9
user        3.5
sys         0.3

$ time awk.sh
real       25.5
user       25.0
sys         0.4

I ran these multiple times and continued to get similar results. You could have won some money off of me - I would have bet that these times would have been much closer together.


0

Response Number 4
Name: Wolfbone
Date: January 27, 2005 at 22:15:43 Pacific
Reply:

Awk is very slow but if you can't do something easily (or at all) with pipelines of simpler tools, it's still very useful.


0

Response Number 5
Name: Neeraja
Date: January 27, 2005 at 22:53:55 Pacific
Reply:

Hello Jim,

Thanks a lot for your time and the information you have provided.

Thanks Wolfbone..

Regards,
Neeraja


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to use AWK command

How do i use DD command www.computing.net/answers/unix/how-do-i-use-dd-command/2031.html

How to use vtl www.computing.net/answers/unix/how-to-use-vtl/7717.html

How to use sed editor in a shell script www.computing.net/answers/unix/how-to-use-sed-editor-in-a-shell-script/8509.html