Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-17-2008, 12:33 PM
|
#1
|
LQ Newbie
Registered: Jul 2008
Posts: 9
Rep:
|
awk shell script error
hey I have a small shell script (below), and I'm trying to use it to make a variable out of a number thats in another file. The reason I have the "for" statement is because I want to have it do this for multiple files.
shell script (to pull the number 2 from the 5th line of the example file below)
Code:
#!/bin/bash
for i in `ls *.log`
do
a="`awk '/The following ModRedundant/ {getline; print $2}'`"
echo $a
done
example file data being pulled from
Code:
D15 177.16243
D16 -64.62156
The following ModRedundant input section has been read:
A 2 11 13 116.97 S 19 0.0100
Iteration 1 RMS(Cart)= 0.00295973 RMS(Int)= 0.00108084
Iteration 2 RMS(Cart)= 0.00001517 RMS(Int)= 0.00108072
My trouble is that it works if i execute it as shellscriptname.sh < filename but not if I execute it as ./shellscriptname. (the command prompt locks up) I'm thinking it has something to do with the fact that it is a for statement but I'm not sure. Any ideas?
Last edited by ApacheRoseXbones; 07-17-2008 at 12:34 PM.
Reason: typo
|
|
|
07-17-2008, 12:36 PM
|
#2
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
You are never specifying a file to awk. Try:
Code:
#!/bin/bash
for i in `ls *.log`
do
a="`awk '/The following ModRedundant/ {getline; print $2}' $i`"
echo $a
done
HTH
Forrest
|
|
|
07-17-2008, 01:43 PM
|
#3
|
LQ Newbie
Registered: Jul 2008
Posts: 9
Original Poster
Rep:
|
Another question!
Ah thanks that worked!
Now I have another issue: I want to use awk to do a substitution with $a in file $ANG. With the way my script is now, it echoes what is in $ANG rather than printing to it. Any idea what to do there?
Code:
#!/bin/bash
for i in `ls *.log`
do
a="`awk '/The following ModRedundant/ {getline; print $2}' $i`"
ANG=${i/log/sh}
awk '{sub(/x/,$a);print}' $ANG
done
what is in $ANG:
Code:
scanf=\$1
\awk -v mine=\$mine 'BEGIN {found=0; ene=0; count=0; printf \"%2s %8s %12s\n\", \"#\", \"Ang1\", \"E\";}\ "
/E\(RHF\)/ {ene=\$5; found=0;} \ "
/Stationary point found/ {found=1; count++} \
(found==1 && \$0 ~ /Optimized Parameters/) {found=2} \
(found==2 && \$0 ~ /A\(x,y,z\)/) {found=3; Ang1=\$4} \
(found==3) {found=4; printf \"%-2d %8.3f %12.6f\n\", count, Ang1, ene }' \$1 \
|
|
|
07-17-2008, 01:55 PM
|
#4
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
If you want something to be parsed by the shell, you use double quotes (""). If you do not want it to be parsed, you use single quotes (''). You are using single quotes to wrap your awk script, so the shell isn't parsing it. Replace those with double quotes, and things should work properly. However, you won't be able to do this if you wanted to do something like print $1 as the shell would replace $1 with the first parameter to the shell or NULL if you didn't give one.
Code:
#!/bin/bash
for i in `ls *.log`
do
a="`awk '/The following ModRedundant/ {getline; print $2}' $i`"
ANG=${i/log/sh}
awk "{sub(/x/,$a);print}" $ANG <--------------- Note the double quotes
done
Someone with a bit more bash/awk skills than I have may be able to suggest a way to do both (I'd have to look it up).
HTH
Forrest
Last edited by forrestt; 07-17-2008 at 01:57 PM.
|
|
|
07-18-2008, 11:26 AM
|
#5
|
LQ Newbie
Registered: Jul 2008
Posts: 9
Original Poster
Rep:
|
Thanks for your help, the script works perfectly now 
|
|
|
07-18-2008, 12:06 PM
|
#6
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
Glad I could help.
Forrest
|
|
|
All times are GMT -5. The time now is 10:46 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|