LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 07-17-2008, 12:33 PM   #1
ApacheRoseXbones
LQ Newbie
 
Registered: Jul 2008
Posts: 9

Rep: Reputation: 0
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
 
Old 07-17-2008, 12:36 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 07-17-2008, 01:43 PM   #3
ApacheRoseXbones
LQ Newbie
 
Registered: Jul 2008
Posts: 9

Original Poster
Rep: Reputation: 0
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 \
 
Old 07-17-2008, 01:55 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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.
 
Old 07-18-2008, 11:26 AM   #5
ApacheRoseXbones
LQ Newbie
 
Registered: Jul 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks for your help, the script works perfectly now
 
Old 07-18-2008, 12:06 PM   #6
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Glad I could help.

Forrest
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to print an apostrophe (') in a shell script using awk? skuz_ball Programming 11 03-10-2012 08:26 AM
Shell Script / awk / perl Question cmontr Programming 11 06-25-2008 01:00 PM
Shell Script / Awk help for a challenge cmontr Programming 30 06-03-2008 06:11 PM
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM


All times are GMT -5. The time now is 10:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration