basic awk questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting basic awk questions
# 1  
Old 08-24-2011
basic awk questions

I find an script with awk sitting around. I went through some online manuals, but I can't figure out exactly how it works. I can't post the whole program. Not allowed.

This is the line that is confusing me. I get when else is in the script
Code:
grep -v "^REM " $1| grep -v "JUNK;" | awk -F" " 'BEGIN{c=0;} { i=1; while ( i <= NF ) { f=$i; if ( f == "YUCK" ) printf("\n\nPROMPT %s \n",$0);

1. grep -v search line ,but ignore REM and JUNK
2. pipe to awk -F. -F does parsing of one line at a time

questions:
3. 'BEGIN {c=0}
Why do that? I don't see a 'c' variable anywhere else. When I googled this, you seem to do something like this when you add:

'BEGIN{for(c=0;c<50;c++)

4. { i=1; while ( i <= NF )
This a loop. that runs until the end of the line.

5. { f=$i;
didn't they just set i=1, and then increment it.
if f=$i, shouldn't that be a number instead, it appears it is what is actually in that field?

6. if ( f == "CREATE" )
condifitional logic

7. printf("\n\nPROMPT %s \n",$0);
short hand of the "then" part of conditional logic

\n\n I think means skip to lines right?
-- PROMPT? Does the word PROMPT get written? I did not see PROMPT as a key word?
-- %s? I know that printf has alot of variables you can use for formats. I did not find one for %s. what does that mean? Does anyone have a link for all the formats that work with printf?
-- /n $0)
-- another new line
-- print the enter line?

so if there is a CREATE, then print the whole line right?

After that I have a series of additional if then conditions, but I get most of it.


8. I see a -o, is that an "or" ? This is from a new line.

if ( ( f == "HERE" ) \
-o ( f == "THERE" ) \

-- if here or there? right?
-- why do we need "\", I know that means continue on a new line ,but why is that necessary here?


9. here is something else I don't get:

-o ( substr(f,length(f)-6,7) == "START" ) \

part of an if with an or
-- i know what substr does
-- f, is the variable that is being searched.
-- next it is check the length of f
-- so substr from length(f)-6 to 7.
-- then checking to see if it equals start




but the c variable is not used anywhere else?
# 2  
Old 08-24-2011
Let me try and answer your questions. If you are new to awk and would like to learn more, here is a good book that I can recommend: sed & awk, Second Edition - O'Reilly Media

Quote:
questions:
3. 'BEGIN {c=0}
Why do that? I don't see a 'c' variable anywhere else. When I googled this, you seem to do something like this when you add:
I see no big reason to do this, in this case. A BEGIN block executes (as the name implies) right at the beginning, before anything else is done. All that it is doing is assigning a value to 0 to the var c - which is not even being used in your code.

Quote:
4. { i=1; while ( i <= NF )
This a loop. that runs until the end of the line.

5. { f=$i;
didn't they just set i=1, and then increment it.
if f=$i, shouldn't that be a number instead, it appears it is what is actually in that field?
The script is iterating (in the while loop) over each of the fields in the input that you pass to your script. So, $i the first time will refer to field 1 and $i the second time will refer to field 2 and so on - until NF, the total number of fields.

Quote:
6. if ( f == "CREATE" )
condifitional logic

7. printf("\n\nPROMPT %s \n",$0);
short hand of the "then" part of conditional logic
You are correct. If one of the fields has a value of CREATE, it prints the whole line preceded by PROMPT. The %s argument to printf if a format specifier which tell it print the arg as string.

Quote:
8. I see a -o, is that an "or" ? This is from a new line.

if ( ( f == "HERE" ) \
-o ( f == "THERE" ) \

-- if here or there? right?
-- why do we need "\", I know that means continue on a new line ,but why is that necessary here?
You are correct about the -o. The "\" is to indicate that the current statement continue on the next line - basically, it is an escape for the newline character.

Quote:
-o ( substr(f,length(f)-6,7) == "START" ) \
Bit unclear about this myself. It seems to be looking at the last 7 characters of the field f, but comparing it with the string START. I guess the intent is to see if the field ends with START.

Hope this helps!!

-GP
This User Gave Thanks to g.pi For This Post:
# 3  
Old 09-01-2011
Thank you for your help and the tip on the book. I'll check it out. I googled around and found some intro pages. I have used a link awk in my scripts such as print a certain tab, but that is it.

the c=0, through me off since I couldn't figure out why it was done. It looks like its unnecessary.

There still one part that is unclear to me.

Quote:

4. { i=1; while ( i <= NF )
This a loop. that runs until the end of the line.

5. { f=$i;
didn't they just set i=1, and then increment it.
if f=$i, shouldn't that be a number instead, it appears it is what is actually in that field?

-- your answer:
The script is iterating (in the while loop) over each of the fields in the input that you pass to your script.
So, $i the first time will refer to field 1 and $i the second time will refer to field 2 and so on - until NF, the total number of fields.

-GP
1. This is odd to me. It looks like, i is basically an index value in an array correct. So when I set i=1, this is short hand for go to the first index value of the array and the array is broken up by the spaces in my data file. Or should I look at $i as some kind of de-ref (old C terminology. had some C when I was an undergrad). so 'i' is basically a pointer. Awk syntax is a little different than what I am used to.

2. awk seems pretty useful for parsing files. I need to parse a sql output file. I was going to use the standard unix READ LINE, but I think awk will be easier (if I can figure out how to do it. ). The ability to pipe a unix function such as grep (to go line by line) into awk to go across the same line is very useful. Its look setting up a nested loop, but with simpler syntax.

for i in loop ...
for j in loop


thank you for your help. Your response was great. I was worried that this would be hard to follow.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

solaris 10 and a few basic questions

hello, first, I'm quite new to solaris. I've installed solaris 10 basic (item 4 on the install-menue). now I had to realize that I don't have any option for connecting the machine from remote. ssh isn't even installed although I've coosed 'yes' for remote access. no matter what solaris is... (10 Replies)
Discussion started by: fourty2
10 Replies

2. UNIX for Dummies Questions & Answers

hp-ux basic questions

I have multiple questions How to list or find only Nov month's files? How can I get state of process like running, stop, or sleep etc? How can I check dependences of processes? plz ans any if u can thx regards, Mazhar Hussain (3 Replies)
Discussion started by: mazhar99
3 Replies

3. UNIX for Dummies Questions & Answers

Basic variable questions

when you see something like this export SOMEDATA=.:/somedir/files what does the ".:" mean? I think the the "." alone would mean current directory but the ":" together is kind of new to me. (6 Replies)
Discussion started by: NycUnxer
6 Replies

4. UNIX for Dummies Questions & Answers

Basic security questions

Hey guys, I've seen this posted a few times when i searched but I kinda want to know the cleanest way of doing it. On Solaris 8 and Solaris 9 What is the best way to disable telnet ssh1 and remote root login premanently? I've seen posts that say edit /etc/services edit this edit that... (3 Replies)
Discussion started by: kingdbag
3 Replies

5. UNIX for Dummies Questions & Answers

Some basic questions

Hi- Newbie here with some basic questions: 1) I can't get alias to work. I tried alias ll='ls -al', but it doesn't work. When just typing 'alias', the new definition doesn't appear. I'm in a bash shell -- is that the problem. I tried switching to csh, but that didn't seem to help. This... (5 Replies)
Discussion started by: Aworstell
5 Replies

6. UNIX for Dummies Questions & Answers

Basic Unix Questions

OK, here's a question from a true UNIX newb: How does one make a 20 line file? I'm lost. :confused: OK, I figured it out. :D (0 Replies)
Discussion started by: Kitchen Zinc
0 Replies

7. UNIX for Dummies Questions & Answers

Basic SFTP questions

I'm trying to find out what all is involved with setting up SFTP? 1) Do Solaris machines come with a SFTP server and client already installed. 2) If so would I have to install SFTP clients on windows, if I want to transfer files to a Solaris box? 3) If SFTP doesn't come prepackaged would I... (1 Reply)
Discussion started by: JohnRodey
1 Replies

8. UNIX for Dummies Questions & Answers

basic UNIX questions

Can somebody please tell me a little about UNIX OS. For instance, is it ideal for digital media or how easy is it to get help and support? (3 Replies)
Discussion started by: buk5d
3 Replies
Login or Register to Ask a Question